def handle_uploaded_file(upload, uploaded_file): """Store a single uploaded file on disk and in the database. :param upload: the :py:class:`geonode.mtapi.models.Upload` instance associated with this upload. :param uploaded_file: an uploaded file from the POST request :returns: the resulting :py:class:`geonode.mtapi.models.Input` instance """ size = 0 chunk_counter = 0 input_type = None path = "%s/%s" % (upload.path, uploaded_file.name) destination = open(path, "wb+") for chunk in uploaded_file.chunks(): destination.write(chunk) size += len(chunk) chunk_counter += 1 if chunk_counter == 1: input_type = detect_input_type(chunk) destination.close() source = Input(upload=upload, owner=upload.owner, size=size, path=path, input_type=input_type) source.save() print(source) return source
def setup_upload(self, dbkey=None): """Create an upload with associated inputs. :param integer dbkey: if set use the upload record with given db key. :returns: a :py:class:`geonode.mtapi.models.Upload` instance """ if dbkey: [upload] = Upload.objects.filter(id=dbkey) return upload files = [ ("gmpe_logic_tree.xml", "lt_gmpe"), ("small_exposure.xml", "exposure"), ("source_model1.xml", "source"), ("source_model2.xml", "source"), ("source_model_logic_tree.xml", "lt_source"), ("vulnerability.xml", "vulnerability")] upload = view_utils.prepare_upload() for file, type in files: path = os.path.join(upload.path, file) # This is equivalent to what the touch command does. open(path, "w+").close() input = Input(path=path, owner=upload.owner, input_type=type, upload=upload) input.save() return upload
def setup_upload(self, dbkey=None): """Create an upload with associated inputs. :param integer dbkey: if set use the upload record with given db key. :returns: a :py:class:`geonode.mtapi.models.Upload` instance """ if dbkey: [upload] = Upload.objects.filter(id=dbkey) return upload files = [("gmpe_logic_tree.xml", "lt_gmpe"), ("small_exposure.xml", "exposure"), ("source_model1.xml", "source"), ("source_model2.xml", "source"), ("source_model_logic_tree.xml", "lt_source"), ("vulnerability.xml", "vulnerability")] upload = view_utils.prepare_upload() for file, type in files: path = os.path.join(upload.path, file) # This is equivalent to what the touch command does. open(path, "w+").close() input = Input(path=path, owner=upload.owner, input_type=type, upload=upload) input.save() return upload