123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import glob
- import os
- from django.conf import settings
- from django.http import HttpResponse
- from multiprocessing import Process
- from .models import AutomaticImport
- from volumes.models import Volume
- from volumes.processing.service import SliceGenerator
- from mongoengine import ValidationError
- import json
- import logging
- import pdb
- logger = logging.getLogger(__name__)
- DEFAULT_SLICE_FOLDER = 'slices_8bit'
- def trigger(request):
- volumes_to_process = []
- paths = sorted(glob.glob(os.path.join(settings.DEFAULT_IMPORT_PATH, '*')))
- # process dirs with dir DEFAULT_SLICE_FOLDER only
- dir_paths = []
- for path in paths:
- if os.path.isdir(path) and DEFAULT_SLICE_FOLDER in os.listdir(path):
- dir_paths.append(path)
- paths = dir_paths
- for path in paths:
- automatic_import = AutomaticImport.objects(path=path).first()
- if(automatic_import is None):
- volume = Volume()
- volume.imageSequence = True
- volume.path = os.path.join(path, DEFAULT_SLICE_FOLDER)
- volume.name = path.replace(settings.DEFAULT_IMPORT_PATH, '')
- volume = volume.save()
- volumes_to_process.append(volume.id)
- automatic_import = AutomaticImport()
- automatic_import.path = path
- automatic_import.volume_id = str(volume.id)
- automatic_import.save()
- # TODO: Why do next line is not work?
- logger.info("Sequence in dir: " + path + " will be processed")
- print("Sequence in dir: " + path + " will be processed")
- if len(volumes_to_process) > 0:
- # TODO: Why do next line is not work?
- logger.info("Processing sequences in dir: " + settings.DEFAULT_IMPORT_PATH + " ...")
- print("Processing sequences in dir: " + settings.DEFAULT_IMPORT_PATH + " ...")
- process = Process(target=SliceGenerator.generateListOfVolumes, args=(volumes_to_process,))
- process.start()
- jsonDump = json.dumps({
- 'triggered': True,
- 'number_of_new_volumes': len(volumes_to_process)
- })
- return HttpResponse(jsonDump, content_type="json")
- def clear(request):
- models = AutomaticImport.objects()
- if len(models) == 0:
- # TODO: Why do next line is not work?
- logger.info("No automatic imported volumes!")
- print("No automatic imported volumes!")
- for automatic_import in models:
- try:
- volume = Volume.objects(id=automatic_import.volume_id)[0]
- except (IndexError, ValidationError) as e:
- return handleException(request, id, e)
- volume.delete()
- automatic_import.delete()
- # TODO: Why do next line is not work?
- logger.info("Automaticly generated volume with id: " + automatic_import.volume_id + " deleted!")
- print("Automaticly generated volume with id: " + automatic_import.volume_id + " deleted!")
- return HttpResponse(json.dumps({'deleted': len(models)}), content_type="json")
|