publishers_7.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. def write_plupload(request, pub_name):
  2. """file upload for plupload"""
  3. logger.info("Starting write plupload")
  4. pub = models.Publisher.objects.get(name=pub_name)
  5. meta = request.POST.get("meta", "{}")
  6. logger.debug("%s" % meta)
  7. if request.method == "POST":
  8. name = request.REQUEST.get("name", "")
  9. uploaded_file = request.FILES["file"]
  10. if not name:
  11. name = uploaded_file.name
  12. logger.debug("plupload name = '%s'" % name)
  13. try:
  14. labelsContent = labels.TargetRegionsContent # default case
  15. if RepresentsJSON(meta):
  16. metaJson = json.loads(meta)
  17. labelsContent = (
  18. labels.HotspotsContent
  19. if metaJson and metaJson["hotspot"]
  20. else labels.TargetRegionsContent
  21. )
  22. file_name = validate_plupload(request, pub_name, name, labelsContent)
  23. except Exception as err:
  24. return HttpResponseBadRequest(unicode(err))
  25. upload_dir = "/results/referenceLibrary/temp"
  26. if not os.path.exists(upload_dir):
  27. return render_to_json({"error": validation.missing_error(upload_dir)})
  28. dest_path = os.path.join(upload_dir, file_name)
  29. logger.debug("plupload destination = '%s'" % dest_path)
  30. chunk = request.REQUEST.get("chunk", "0")
  31. chunks = request.REQUEST.get("chunks", "0")
  32. logger.debug(
  33. "plupload chunk %s %s of %s" % (str(type(chunk)), str(chunk), str(chunks))
  34. )
  35. debug = [chunk, chunks]
  36. with open(dest_path, ("wb" if chunk == "0" else "ab")) as f:
  37. for content in uploaded_file.chunks():
  38. logger.debug("content chunk = '%d'" % len(content))
  39. f.write(content)
  40. my_contentupload_id = None
  41. if int(chunk) + 1 >= int(chunks):
  42. try:
  43. meta = json.loads(meta)
  44. meta["username"] = request.user.username
  45. upload = move_upload(pub, dest_path, file_name, json.dumps(meta))
  46. async_upload = run_pub_scripts.delay(pub, upload)
  47. my_contentupload_id = upload.id
  48. except Exception as err:
  49. logger.exception(
  50. "There was a problem during upload of a file for a publisher."
  51. )
  52. else:
  53. logger.info("Successfully pluploaded %s" % name)
  54. logger.debug("plupload done")
  55. return render_to_json(
  56. {"chunk posted": debug, "contentupload_id": my_contentupload_id}
  57. )
  58. else:
  59. return render_to_json(
  60. {
  61. "method": i18n_errors.fatal_unsupported_http_method_expected(
  62. request.method, "POST"
  63. )
  64. }
  65. )