publishers_8.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. def new_upload(pub, file_name, meta_data=None):
  2. upload_date = dateutil.parser.parse(time.asctime()).isoformat()
  3. # set up meta.json
  4. meta_data_dict = json.loads(meta_data)
  5. meta_data_dict["upload_date"] = upload_date
  6. meta_data = json.dumps(meta_data_dict)
  7. # create ContentUpload
  8. upload = models.ContentUpload()
  9. upload.status = "Saving"
  10. upload.publisher = pub
  11. upload.meta = meta_data
  12. upload.username = meta_data_dict.get("username", "")
  13. upload.source = meta_data_dict.get("source") or meta_data_dict.get("url", "")
  14. upload.upload_date = upload_date
  15. if "upload_type" in meta_data_dict:
  16. upload.upload_type = meta_data_dict["upload_type"]
  17. elif pub.name == "BED" and "hotspot" in meta_data_dict:
  18. upload.upload_type = (
  19. publisher_types.HOTSPOT
  20. if meta_data_dict["hotspot"]
  21. else publisher_types.TARGET
  22. )
  23. else:
  24. upload.upload_type = pub.name
  25. upload.save()
  26. upload_dir = os.path.join("/results/uploads", pub.name, str(upload.pk))
  27. upload.file_path = os.path.join(upload_dir, file_name)
  28. upload.save()
  29. try:
  30. meta_path = os.path.join(upload_dir, "meta.json")
  31. # set both the user and group to read/write to allow the celery tasks to write to this directory
  32. original_umask = os.umask(0)
  33. os.makedirs(upload_dir, 0o0775)
  34. open(meta_path, "w").write(meta_data)
  35. except OSError as err:
  36. logger.exception("File error while saving new %s upload" % pub)
  37. upload.status = "Error: %s" % err
  38. upload.save()
  39. raise
  40. finally:
  41. os.umask(original_umask)
  42. return upload