views.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import glob
  2. import os
  3. from django.conf import settings
  4. from django.http import HttpResponse
  5. from multiprocessing import Process
  6. from .models import AutomaticImport
  7. from volumes.models import Volume
  8. from volumes.processing.service import SliceGenerator
  9. from mongoengine import ValidationError
  10. import json
  11. import logging
  12. import pdb
  13. logger = logging.getLogger(__name__)
  14. DEFAULT_SLICE_FOLDER = 'slices_8bit'
  15. def trigger(request):
  16. volumes_to_process = []
  17. paths = sorted(glob.glob(os.path.join(settings.DEFAULT_IMPORT_PATH, '*')))
  18. # process dirs with dir DEFAULT_SLICE_FOLDER only
  19. dir_paths = []
  20. for path in paths:
  21. if os.path.isdir(path) and DEFAULT_SLICE_FOLDER in os.listdir(path):
  22. dir_paths.append(path)
  23. paths = dir_paths
  24. for path in paths:
  25. automatic_import = AutomaticImport.objects(path=path).first()
  26. if automatic_import is None:
  27. volume = Volume()
  28. volume.imageSequence = True
  29. volume.path = os.path.join(path, DEFAULT_SLICE_FOLDER)
  30. volume.name = path.replace(settings.DEFAULT_IMPORT_PATH, '')
  31. volume = volume.save()
  32. volumes_to_process.append(volume.id)
  33. automatic_import = AutomaticImport()
  34. automatic_import.path = path
  35. automatic_import.volume_id = str(volume.id)
  36. automatic_import.save()
  37. logger.info("Sequence in dir: " + path + " will be processed")
  38. if len(volumes_to_process) > 0:
  39. logger.info("Processing sequences in dir: " + settings.DEFAULT_IMPORT_PATH + " ...")
  40. process = Process(target=SliceGenerator.generateListOfVolumes, args=(volumes_to_process,))
  41. process.start()
  42. jsonDump = json.dumps({
  43. 'triggered': True,
  44. 'number_of_new_volumes': len(volumes_to_process)
  45. })
  46. return HttpResponse(jsonDump, content_type="json")
  47. def clear(request):
  48. models = AutomaticImport.objects()
  49. if len(models) == 0:
  50. logger.info("No automatic imported volumes!")
  51. for automatic_import in models:
  52. try:
  53. volume = Volume.objects(id=automatic_import.volume_id)[0]
  54. volume.delete()
  55. except IndexError as e:
  56. logger.info("Volume with id = " + str(automatic_import.volume_id + " which was binded to AutomaticImport for path = " + automatic_import.path + " not found!"))
  57. automatic_import.delete()
  58. logger.info("Automaticly generated volume with id: " + automatic_import.volume_id + " deleted!")
  59. return HttpResponse(json.dumps({'deleted': len(models)}), content_type="json")