Commit 2c1ce944 by Carpoon

Group import/export add rerror handling

parent d7c3c84c
...@@ -453,24 +453,34 @@ class GroupImportView(LoginRequiredMixin, TemplateView): ...@@ -453,24 +453,34 @@ class GroupImportView(LoginRequiredMixin, TemplateView):
if form.is_valid(): if form.is_valid():
group_path = form.cleaned_data["group_path"] group_path = form.cleaned_data["group_path"]
url = Store(request.user).request_download(group_path) url = Store(request.user).request_download(group_path)
json_str = requests.get(url).content try:
profile = GroupProfile.create_from_json(request.user, json_str) response = requests.get(url, verify=False)
if profile is None: #response = requests.get(url)
raise SuspiciousOperation() if response.status_code == 200:
json_str = response.content
success_message = _("Group successfully imported.") profile = GroupProfile.create_from_json(request.user, json_str)
if request.is_ajax(): if profile is None:
response = { raise SuspiciousOperation()
'message': success_message,
'redirect': profile.get_absolute_url() success_message = _("Group successfully imported.")
} if request.is_ajax():
return HttpResponse( response = {
json.dumps(response), 'message': success_message,
content_type="application/json" 'redirect': profile.get_absolute_url()
) }
else: return HttpResponse(
messages.success(request, success_message) json.dumps(response),
return redirect(profile.get_absolute_url()) content_type="application/json"
)
else:
messages.success(request, success_message)
return redirect(profile.get_absolute_url())
else:
messages.error(request, response.reason)
return self.get(request, form, *args, **kwargs)
except requests.exceptions.RequestException as e:
messages.error(e)
return self.get(request, form, *args, **kwargs)
else: else:
return self.get(request, form, *args, **kwargs) return self.get(request, form, *args, **kwargs)
...@@ -529,21 +539,40 @@ class GroupExportView(LoginRequiredMixin, SingleObjectMixin, TemplateView): ...@@ -529,21 +539,40 @@ class GroupExportView(LoginRequiredMixin, SingleObjectMixin, TemplateView):
url = store.request_upload("/") url = store.request_upload("/")
data = {'data': (name + '.group', group_json)} data = {'data': (name + '.group', group_json)}
requests.post(url, files=data) try:
response = requests.post(url, files=data, verify=False)
success_message = _("Group successfully exported.") #response = requests.post(url, files=data)
if request.is_ajax(): if response.status_code == 200:
response = { success_message = _("Group successfully exported.")
'message': success_message, if request.is_ajax():
'redirect': group.profile.get_absolute_url() ajax_response = {
} 'message': success_message,
return HttpResponse( 'redirect': group.profile.get_absolute_url()
json.dumps(response), }
content_type="application/json" return HttpResponse(
) json.dumps(ajax_response),
else: content_type="application/json"
messages.success(request, success_message) )
return redirect(group.profile.get_absolute_url()) else:
messages.success(request, success_message)
return redirect(group.profile.get_absolute_url())
else:
if request.is_ajax():
ajax_response = {
'message': response.reason,
'redirect': group.profile.get_absolute_url()
}
return HttpResponse(
json.dumps(ajax_response),
content_type="application/json"
)
else:
messages.error(request, response.reason)
return self.get(request, form, *args, **kwargs)
# This catches all requests Exceptions as this is the base class
except requests.exceptions.RequestException as e:
messages.error(e)
return self.get(request, form, *args, **kwargs)
else: else:
return self.get(request, form, *args, **kwargs) return self.get(request, form, *args, **kwargs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment