2020-11-18 20:34:50 +00:00
|
|
|
import json
|
2020-11-11 14:44:58 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from crc import session
|
2020-11-18 20:34:50 +00:00
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc.models.data_store import DataStoreModel, DataStoreSchema
|
2021-06-02 14:00:18 +00:00
|
|
|
from crc.services.data_store_service import DataStoreBase
|
2021-06-01 20:34:21 +00:00
|
|
|
|
2020-11-11 14:44:58 +00:00
|
|
|
|
2020-11-13 14:58:21 +00:00
|
|
|
def study_multi_get(study_id):
|
|
|
|
"""Get all data_store values for a given study_id study"""
|
|
|
|
if study_id is None:
|
|
|
|
raise ApiError('unknown_study', 'Please provide a valid Study ID.')
|
|
|
|
|
|
|
|
dsb = DataStoreBase()
|
2020-11-18 20:34:50 +00:00
|
|
|
retval = dsb.get_multi_common(study_id, None)
|
2020-11-13 14:58:21 +00:00
|
|
|
results = DataStoreSchema(many=True).dump(retval)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def user_multi_get(user_id):
|
|
|
|
"""Get all data values in the data_store for a userid"""
|
|
|
|
if user_id is None:
|
|
|
|
raise ApiError('unknown_study', 'Please provide a valid UserID.')
|
|
|
|
|
|
|
|
dsb = DataStoreBase()
|
2020-11-18 20:34:50 +00:00
|
|
|
retval = dsb.get_multi_common(None,
|
|
|
|
user_id)
|
2020-11-13 14:58:21 +00:00
|
|
|
results = DataStoreSchema(many=True).dump(retval)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2021-06-01 17:50:04 +00:00
|
|
|
def file_multi_get(file_id):
|
|
|
|
"""Get all data values in the data store for a file_id"""
|
|
|
|
if file_id is None:
|
|
|
|
raise ApiError(code='unknown_file', message='Please provide a valid file id.')
|
|
|
|
dsb = DataStoreBase()
|
|
|
|
retval = dsb.get_multi_common(None, None, file_id=file_id)
|
|
|
|
results = DataStoreSchema(many=True).dump(retval)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2020-11-13 17:03:29 +00:00
|
|
|
def datastore_del(id):
|
2021-04-26 16:31:22 +00:00
|
|
|
"""Delete a data store item for a key"""
|
2020-11-13 17:03:29 +00:00
|
|
|
session.query(DataStoreModel).filter_by(id=id).delete()
|
|
|
|
session.commit()
|
2020-11-13 14:58:21 +00:00
|
|
|
json_value = json.dumps('deleted', ensure_ascii=False, indent=2)
|
|
|
|
return json_value
|
|
|
|
|
2020-11-18 20:34:50 +00:00
|
|
|
|
2020-11-13 17:03:29 +00:00
|
|
|
def datastore_get(id):
|
2021-04-26 16:31:22 +00:00
|
|
|
"""retrieve a data store item by a key"""
|
2020-11-13 17:03:29 +00:00
|
|
|
item = session.query(DataStoreModel).filter_by(id=id).first()
|
|
|
|
results = DataStoreSchema(many=False).dump(item)
|
|
|
|
return results
|
|
|
|
|
2020-11-18 20:34:50 +00:00
|
|
|
|
2020-11-13 17:03:29 +00:00
|
|
|
def update_datastore(id, body):
|
|
|
|
"""allow a modification to a datastore item """
|
|
|
|
if id is None:
|
|
|
|
raise ApiError('unknown_id', 'Please provide a valid ID.')
|
|
|
|
|
|
|
|
item = session.query(DataStoreModel).filter_by(id=id).first()
|
|
|
|
if item is None:
|
|
|
|
raise ApiError('unknown_item', 'The item "' + id + '" is not recognized.')
|
2021-04-26 16:31:22 +00:00
|
|
|
|
|
|
|
DataStoreSchema().load(body, instance=item, session=session)
|
2021-04-29 14:25:28 +00:00
|
|
|
item.last_updated = datetime.utcnow()
|
2020-11-13 17:03:29 +00:00
|
|
|
session.add(item)
|
|
|
|
session.commit()
|
|
|
|
return DataStoreSchema().dump(item)
|
|
|
|
|
|
|
|
|
|
|
|
def add_datastore(body):
|
|
|
|
""" add a new datastore item """
|
|
|
|
|
|
|
|
print(body)
|
2020-11-18 20:34:50 +00:00
|
|
|
if body.get(id, None):
|
2020-11-13 17:03:29 +00:00
|
|
|
raise ApiError('id_specified', 'You may not specify an id for a new datastore item')
|
|
|
|
|
|
|
|
if 'key' not in body:
|
|
|
|
raise ApiError('no_key', 'You need to specify a key to add a datastore item')
|
|
|
|
|
|
|
|
if 'value' not in body:
|
|
|
|
raise ApiError('no_value', 'You need to specify a value to add a datastore item')
|
|
|
|
|
2021-04-26 13:41:14 +00:00
|
|
|
if ('user_id' not in body) and ('study_id' not in body) and ('file_id' not in body):
|
|
|
|
raise ApiError('conflicting_values', 'A datastore item should have either a study_id, user_id or file_id ')
|
2020-11-13 17:03:29 +00:00
|
|
|
|
2021-04-26 16:31:22 +00:00
|
|
|
|
2021-04-26 13:41:14 +00:00
|
|
|
present = 0
|
|
|
|
for field in ['user_id','study_id','file_id']:
|
|
|
|
if field in body:
|
|
|
|
present = present+1
|
|
|
|
if present > 1:
|
|
|
|
raise ApiError('conflicting_values', 'A datastore item should have one of a study_id, user_id or a file_id '
|
|
|
|
'but not more than one of these')
|
2020-11-13 17:03:29 +00:00
|
|
|
|
2021-04-26 16:31:22 +00:00
|
|
|
item = DataStoreSchema().load(body)
|
2021-04-29 14:25:28 +00:00
|
|
|
item.last_updated = datetime.utcnow()
|
2020-11-13 17:03:29 +00:00
|
|
|
session.add(item)
|
|
|
|
session.commit()
|
|
|
|
return DataStoreSchema().dump(item)
|