def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. # handle file upload # write file in chunks to file system file_path = handle_uploaded_file(self.request.FILES['upload_file']) identifier = form.cleaned_data.get('model_name') description = form.cleaned_data.get('description') organisation_uuid = form.cleaned_data.get('organisation') organisation = Organisation.objects.get(unique_id=organisation_uuid) model_type_id = form.cleaned_data.get('model_type') model_type = ModelType.objects.get(pk=model_type_id) model_upload = ModelUpload( uploaded_by=self.request.user, identifier=identifier, description=description, file_path=file_path, organisation=organisation, model_type=model_type) model_upload.save() messages.info(self.request, _("Upload succeeded. Data will be " "processed soon.")) return super(ModelUploadFormView, self).form_valid(form)
def handle(self, *args, **options): # Check whether there are zip files in the # MODEL_DATABANK_FTP_UPLOAD_PATH and put them in the regular upload # directory and create a ModelUpload instance of them, so the can be # processed. ftp_upload_path = getattr(settings, 'MODEL_DATABANK_FTP_UPLOAD_PATH', None) if not ftp_upload_path: sys.stdout.write( "No MODEL_DATABANK_FTP_UPLOAD_PATH in settings.\n") else: # get all zipfiles; return absolute paths to zipfiles # directory the zipfile is in, should be the organisation name # these directories are generated with the # create_organisations_upload_directories command zipfiles = [os.path.join(dirpath, f) for dirpath, dirnames, files in os.walk(ftp_upload_path) for f in files if f.endswith('.zip')] # apply last modified time filter; only process zipfiles that are # not changed for at least 60 seconds zipfiles = [zipfile for zipfile in zipfiles if time.time() > os.path.getmtime(zipfile) + 60] if not zipfiles: sys.stdout.write("No zipfiles found for processing.\n") # filter on last modified date for zipfile in zipfiles: fpath, fn = os.path.split(zipfile) org_name = os.path.split(fpath)[1] try: organisation = Organisation.objects.get(name=org_name) except ObjectDoesNotExist: sys.stdout.write("No organisation found with name %s.\n" % org_name) continue else: sys.stdout.write( "Processing zipfile %s for organisation %s...\n" % ( fn, organisation)) fn_wo_zip = fn.rstrip('.zip').capitalize() # check for duplicate model name try: ModelReference.objects.get(identifier=fn_wo_zip) except ObjectDoesNotExist: pass else: sys.stdout.write( "Model with name %s already exists. Removing " "uploaded zipfile %s." % (fn_wo_zip, fn)) os.remove(zipfile) continue ftp_user = get_ftp_user() now = datetime.datetime.now() file_name = '%s_%s.zip' % ( fn_wo_zip, now.strftime('%Y%m%d%H%M%S%f')) file_path = os.path.join( settings.MODEL_DATABANK_UPLOAD_PATH, file_name) # For now, assume a model uploaded by FTP is a 3Di model; # this can be changed via the ModelReference admin page. default_model_type = ModelType.objects.get(slug='3di') try: shutil.move(zipfile, file_path) model_upload = ModelUpload( uploaded_by=ftp_user, identifier=fn_wo_zip, description='', file_path=file_path, organisation=organisation, model_type=default_model_type) model_upload.save() except Exception, err: sys.stdout.write( "An error occurred trying to prepare a " "ModelUpload instance. Error: %s.\n" % err)