publishers_13.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. def publisher_upload(request, pub_name):
  2. """Display the publishers upload.html template on a GET of the page.
  3. If the view is POSTed to, the pass the uploaded data to the publisher.
  4. """
  5. pub = models.Publisher.objects.get(name=pub_name)
  6. if request.method == "POST":
  7. meta = request.POST.dict()
  8. files = list(request.FILES.values())
  9. if len(files) == 0:
  10. return render_to_json({"error": "Error: No file selected for upload"})
  11. else:
  12. try:
  13. meta["username"] = request.user.username
  14. upload, async_task = edit_upload(pub, files[0], json.dumps(meta))
  15. return render_to_json({"status": upload.status, "id": upload.id})
  16. except Exception as e:
  17. return render_to_json({"error": str(e)})
  18. else:
  19. action = request.get_full_path()
  20. error = ""
  21. contents = ""
  22. try:
  23. path = os.path.join(pub.path, "upload.html")
  24. with open(path, "r") as f:
  25. contents = f.read()
  26. except Exception:
  27. error = "Error: Unable to read %s" % path
  28. ctx = RequestContext(
  29. request, {"contents": contents, "action": action, "error": error}
  30. )
  31. return render_to_response(
  32. "rundb/configure/modal_publisher_upload.html", context_instance=ctx
  33. )