views.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # TODO: Why do next line is not work?
  38. logger.info("Sequence in dir: " + path + " will be processed")
  39. print("Sequence in dir: " + path + " will be processed")
  40. if len(volumes_to_process) > 0:
  41. # TODO: Why do next line is not work?
  42. logger.info("Processing sequences in dir: " + settings.DEFAULT_IMPORT_PATH + " ...")
  43. print("Processing sequences in dir: " + settings.DEFAULT_IMPORT_PATH + " ...")
  44. process = Process(target=SliceGenerator.generateListOfVolumes, args=(volumes_to_process,))
  45. process.start()
  46. jsonDump = json.dumps({
  47. 'triggered': True,
  48. 'number_of_new_volumes': len(volumes_to_process)
  49. })
  50. return HttpResponse(jsonDump, content_type="json")
  51. def clear(request):
  52. models = AutomaticImport.objects()
  53. if len(models) == 0:
  54. # TODO: Why do next line is not work?
  55. logger.info("No automatic imported volumes!")
  56. print("No automatic imported volumes!")
  57. for automatic_import in models:
  58. try:
  59. volume = Volume.objects(id=automatic_import.volume_id)[0]
  60. except (IndexError, ValidationError) as e:
  61. return handleException(request, id, e)
  62. volume.delete()
  63. automatic_import.delete()
  64. # TODO: Why do next line is not work?
  65. logger.info("Automaticly generated volume with id: " + automatic_import.volume_id + " deleted!")
  66. print("Automaticly generated volume with id: " + automatic_import.volume_id + " deleted!")
  67. return HttpResponse(json.dumps({'deleted': len(models)}), content_type="json")