diff --git a/nomad/api.py b/nomad/api.py index 1875a067f1c6d4214c46a72e2e903d47ef5080ae..82eedcb6df60066badca2bb36ec7d334d3f8c48e 100644 --- a/nomad/api.py +++ b/nomad/api.py @@ -180,6 +180,9 @@ class UploadsRes(Resource): } :jsonparam string name: An optional name for the upload. + :jsonparem string local_path: An optional path the a file that is already on the server. + In this case, uploading a file won't be possible, the local file is processed + immediatly as if it was uploaded. :reqheader Content-Type: application/json :resheader Content-Type: application/json :status 200: upload successfully created @@ -189,7 +192,11 @@ class UploadsRes(Resource): if json_data is None: json_data = {} - upload = Upload.create(user=g.user, name=json_data.get('name')) + upload = Upload.create( + user=g.user, + name=json_data.get('name'), + local_path=json_data.get('local_path')) + return upload.json_dict, 200 diff --git a/nomad/processing/data.py b/nomad/processing/data.py index 80dfe6279b1b437d8f06ae13d7cd25d9b3acf2fd..a716ab6bd32e2b7bd0f11cbe38a4db2aaf837729 100644 --- a/nomad/processing/data.py +++ b/nomad/processing/data.py @@ -232,6 +232,7 @@ class Upload(Chord): Attributes: name: optional user provided upload name + local_path: optional local path, e.g. for files that are already somewhere on the server additional_metadata: optional user provided additional meta data upload_id: the upload id generated by the database in_staging: true if the upload is still in staging and can be edited by the uploader @@ -245,6 +246,7 @@ class Upload(Chord): upload_id = StringField(primary_key=True) name = StringField(default=None) + local_path = StringField(default=None) additional_metadata = DictField(default=None) in_staging = BooleanField(default=True) @@ -291,7 +293,7 @@ class Upload(Chord): with lnr(logger, 'delete upload file'): try: - UploadFile(self.upload_id).delete() + UploadFile(self.upload_id, local_path=self.local_path).delete() except KeyError: if self.current_task == 'uploading': logger.debug( @@ -365,6 +367,7 @@ class Upload(Chord): """ A json serializable dictionary representation. """ data = { 'name': self.name, + 'local_path': self.local_path, 'additional_metadata': self.additional_metadata, 'upload_id': self.upload_id, 'upload_url': self.upload_url, @@ -388,7 +391,7 @@ class Upload(Chord): def extracting(self): logger = self.get_logger() try: - self._upload = UploadFile(self.upload_id) + self._upload = UploadFile(self.upload_id, local_path=self.local_path) self._upload.extract() logger.debug('opened upload') except KeyError as e: @@ -436,7 +439,7 @@ class Upload(Chord): @task def cleanup(self): try: - upload = UploadFile(self.upload_id) + upload = UploadFile(self.upload_id, local_path=self.local_path) except KeyError as e: self.fail('Upload does not exist', exc_info=e) return diff --git a/tests/test_api.py b/tests/test_api.py index 918985dc9a33e6744c7ed624e48e44e972f0a33a..4f0cc0837f6f1578a8bf5aa59828f91ead0634c9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -125,12 +125,25 @@ def test_create_upload(client, test_user_auth, no_warn): def test_create_upload_with_name(client, test_user_auth, no_warn): rv = client.post( '/uploads', headers=test_user_auth, - data=json.dumps(dict(name='test_name')), content_type='application/json') + data=json.dumps(dict(name='test_name')), + content_type='application/json') + assert rv.status_code == 200 upload = assert_upload(rv.data) assert upload['name'] == 'test_name' +def test_create_upload_with_local_path(client, test_user_auth, no_warn): + rv = client.post( + '/uploads', headers=test_user_auth, + data=json.dumps(dict(local_path='test_local_path')), + content_type='application/json') + + assert rv.status_code == 200 + upload = assert_upload(rv.data) + assert upload['local_path'] == 'test_local_path' + + def test_delete_empty_upload(client, test_user_auth, no_warn): rv = client.post('/uploads', headers=test_user_auth)