def import_(request): """Generates and accepts GET and POST requests for importing courses If the request is get or an invalid POST, uses the template export/import.html, if valid then redirect to the newly uploaded course. """ ctx = {} if request.method == "GET": # Return an empty form ctx["form"] = ImportForm() return render(request, "export/import.html", ctx) else: with atomic(): # Try to import the course ctx["form"] = ImportForm(request.POST) if not ctx["form"].is_valid(): # Form invalid? Send it right back to the user return render(request, "export/import.html", ctx) try: # Attempt decode data = decode(ctx["form"].cleaned_data["text"].replace("\r\n", "\n")) if data is None: raise RuntimeError("No data could be extracted from the import") new = Course.from_dict(data, ctx["form"].cleaned_data["mode"], ctx["form"].cleaned_data["user_mode"]) return redirect(new.get_absolute_url()) except RuntimeError as e: # Any error? Add it as an error and then show the form again ctx["import_error"] = "Sorry, that failed to import, the error was: {}".format(e) return render(request, "export/import.html", ctx)