Example #1
0
 def upload_json(self, request):
     try:
         if request.method == "POST":
             data = json.loads(request.FILES["json_file"].read())
             result = Profile.import_customer_data(data)
             response = JsonResponse(result)
             response["Content-Disposition"] = "attachment; filename=export.json"
             return response
         else:
             form = ImportProfilesFromJsonForm()
             return render(
                 request, "admin/profiles/upload_json.html", {"form": form}
             )
     except Exception as err:
         messages.error(request, err)
         form = ImportProfilesFromJsonForm()
         return render(request, "admin/profiles/upload_json.html", {"form": form})
Example #2
0
    def upload_json(self, request):
        try:
            if request.method == "POST":
                form = ImportProfilesFromJsonForm(request.POST, request.FILES)
                if form.is_valid():
                    data = json.loads(request.FILES["json_file"].read())
                    service = form.cleaned_data["service"]
                    result = Profile.import_customer_data(data, service)
                    response = JsonResponse(result)
                    response[
                        "Content-Disposition"] = "attachment; filename=export.json"

                    return response
                else:
                    raise ValidationError(form.errors.as_text())
            else:
                form = ImportProfilesFromJsonForm()
                return render(request, "admin/profiles/upload_json.html",
                              {"form": form})
        except Exception as err:
            messages.error(request, err)
            form = ImportProfilesFromJsonForm()
            return render(request, "admin/profiles/upload_json.html",
                          {"form": form})