Commit 11d33ef4 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added meta data to unstage operation.

parent 03d405b0
......@@ -89,8 +89,25 @@ upload_with_calcs_model = api.inherit('UploadWithPaginatedCalculations', upload_
}))
})
meta_data_model = api.model('MetaData', {
'with_embargo': fields.Boolean(default=False, description='Data with embargo is only visible to the upload until the embargo period ended.'),
'comments': fields.List(fields.String, description='Comments are shown in the repository for each calculation.'),
'references': fields.List(fields.String, descriptions='References allow to link calculations to external source, e.g. URLs.'),
'coauthors': fields.List(fields.String, description='A list of co-authors given by user_id.'),
'share_with': fields.List(fields.String, description='A list of users to share calculations with given by user_id.')
})
calc_meta_data_model = api.inherit('CalcMetaData', meta_data_model, {
'mainfile': fields.String(description='The calculation main output file is used to identify the calculation in the upload.')
})
upload_meta_data_model = api.inherit('UploadMetaData', meta_data_model, {
'calculations': fields.List(fields.Nested(model=calc_meta_data_model), description='Specific per calculation data that will override the upload data.')
})
upload_operation_model = api.model('UploadOperation', {
'operation': fields.String(description='Currently unstage is the only operation.')
'operation': fields.String(description='Currently unstage is the only operation.'),
'metadata': fields.Nested(model=upload_meta_data_model, description='Additional upload and calculation meta data that should be considered for the operation')
})
......@@ -278,8 +295,11 @@ class Upload(Resource):
"""
Execute an upload operation. Available operations: ``unstage``
Untage changes the visibility of the upload. Clients can specify, if the calcs
should be restricted.
Unstage accepts further meta data that allows to provide coauthors, comments,
external references, etc.
Unstage changes the visibility of the upload. Clients can specify the visibility
via meta data.
"""
try:
upload = UploadProc.get(upload_id)
......@@ -294,12 +314,13 @@ class Upload(Resource):
json_data = {}
operation = json_data.get('operation')
meta_data = json_data.get('meta_data', {})
if operation == 'unstage':
if not upload.in_staging:
abort(400, message='Operation not allowed, upload is not in staging.')
try:
upload.unstage()
upload.unstage(meta_data)
except NotAllowedDuringProcessing:
abort(400, message='You must not unstage an upload during processing.')
......
......@@ -49,15 +49,34 @@ from nomad.repo import RepoCalc
Base = declarative_base()
def add_upload(upload, restricted: bool) -> int:
class UploadMetaData:
def __init__(self, user_meta_data_dict):
self._upload_data = user_meta_data_dict
self._calc_data = {
calc['mainfile']: calc
for calc in self._upload_data.get('calculations', [])}
def get(self, mainfile):
return self._calc_data.get(mainfile, self._upload_data)
def add_upload(upload, user_meta_data: dict = {}) -> int:
"""
Add the processed upload to the NOMAD-coe repository db. It creates an
uploads-entry, respective calculation and property entries. Everything in one
transaction. Triggers an updates the NOMAD-coe repository elastic search index after
success.
transaction.
Triggers and updates the NOMAD-coe repository elastic search index after
success (TODO).
Arguments:
upload: The upload to add
upload_meta_data: A dictionary with additional meta data (e.g. user meta data)
that should be added to upload and calculations.
TODO deal with the restricted parameter
TODO deal with user_meta_data
"""
upload_meta_data = UploadMetaData(user_meta_data)
repo_db = infrastructure.repository_db
repo_db.begin()
......@@ -81,7 +100,8 @@ def add_upload(upload, restricted: bool) -> int:
has_calcs = False
for repo_calc in RepoCalc.upload_calcs(upload.upload_id):
has_calcs = True
add_calculation(upload, coe_upload, repo_calc, restricted)
add_calculation(
upload, coe_upload, repo_calc, upload_meta_data.get(repo_calc.mainfile))
# commit
if has_calcs:
......@@ -101,7 +121,7 @@ def add_upload(upload, restricted: bool) -> int:
return result
def add_calculation(upload, coe_upload, calc: RepoCalc, restricted: bool) -> None:
def add_calculation(upload, coe_upload, calc: RepoCalc, calc_meta_data: dict) -> None:
repo_db = infrastructure.repository_db
# table based properties
......@@ -133,7 +153,7 @@ def add_calculation(upload, coe_upload, calc: RepoCalc, restricted: bool) -> Non
user_metadata = UserMetaData(
calc=coe_calc,
permission=0 if not restricted else 1)
permission=1 if calc_meta_data.get('restricted', False) else 0)
repo_db.add(user_metadata)
spacegroup = Spacegroup(
......
......@@ -420,7 +420,7 @@ class Upload(Chord):
return self
def unstage(self):
def unstage(self, meta_data):
self.get_logger().info('unstage')
if not (self.completed or self.current_task == 'uploading'):
......@@ -428,7 +428,7 @@ class Upload(Chord):
self.in_staging = False
RepoCalc.unstage(upload_id=self.upload_id)
coe_repo.add_upload(self, restricted=False) # TODO allow users to choose restricted
coe_repo.add_upload(self, meta_data)
self.save()
@process
......
......@@ -48,10 +48,10 @@ def assert_coe_upload(upload_hash, repository_db, empty=False):
@pytest.mark.timeout(10)
def test_add_upload(repository_db, processed_upload):
coe_upload_id = add_upload(processed_upload, restricted=False)
coe_upload_id = add_upload(processed_upload)
if coe_upload_id:
assert_coe_upload(processed_upload.upload_hash, repository_db)
coe_upload_id = add_upload(processed_upload, restricted=False)
coe_upload_id = add_upload(processed_upload)
if coe_upload_id:
assert_coe_upload(processed_upload.upload_hash, repository_db)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment