Just some quick tidying up. Dropping commented out code, and using the SQLAlchemyAutoSchema to handle serializing db models

This commit is contained in:
Dan 2021-04-26 12:31:22 -04:00
parent 40ee20ecca
commit 047c68b2b5
3 changed files with 22 additions and 117 deletions

View File

@ -7,32 +7,6 @@ from crc.models.data_store import DataStoreModel, DataStoreSchema
from crc.scripts.data_store_base import DataStoreBase
# def study_data_set(study_id, key, value):
# """Set a study data value in the data_store, mimic the script endpoint"""
# if study_id is None:
# raise ApiError('unknown_study', 'Please provide a valid Study ID.')
#
# if key is None:
# raise ApiError('invalid_key', 'Please provide a valid key')
# dsb = DataStoreBase()
# retval = dsb.set_data_common('api', study_id, None, None, None, 'api_study_data_set', key, value)
# json_value = json.dumps(retval, ensure_ascii=False, indent=2)
# return json_value
# def study_data_get(study_id, key, default=None):
# """Get a study data value in the data_store, mimic the script endpoint"""
# if study_id is None:
# raise ApiError('unknown_study', 'Please provide a valid Study ID.')
#
# if key is None:
# raise ApiError('invalid_key', 'Please provide a valid key')
# dsb = DataStoreBase()
# retval = dsb.get_data_common(study_id, None, 'api_study_data_get', key, default)
# # json_value = json.dumps(retval, ensure_ascii=False, indent=2) # just return raw text
# return retval
def study_multi_get(study_id):
"""Get all data_store values for a given study_id study"""
if study_id is None:
@ -44,57 +18,6 @@ def study_multi_get(study_id):
return results
# def study_data_del(study_id, key):
# """Delete a study data value in the data store"""
# if study_id is None:
# raise ApiError('unknown_study', 'Please provide a valid Study ID.')
#
# if key is None:
# raise ApiError('invalid_key', 'Please provide a valid key')
# dsb = DataStoreBase()
# dsb.del_data_common(study_id, None, 'api_study_data_get', key)
# json_value = json.dumps('deleted', ensure_ascii=False, indent=2)
# return json_value
# def user_data_set(user_id, key, value):
# """Set a user data value in the data_store, mimic the script endpoint"""
# if user_id is None:
# raise ApiError('unknown_study', 'Please provide a valid UserID.')
#
# if key is None:
# raise ApiError('invalid_key', 'Please provide a valid key')
# dsb = DataStoreBase()
#
# retval = dsb.set_data_common('api',
# None,
# user_id,
# None,
# None,
# 'api_user_data_set',
# key, value)
#
# json_value = json.dumps(retval, ensure_ascii=False, indent=2)
# return json_value
# def user_data_get(user_id, key, default=None):
# """Get a user data value from the data_store, mimic the script endpoint"""
# if user_id is None:
# raise ApiError('unknown_study', 'Please provide a valid UserID.')
#
# if key is None:
# raise ApiError('invalid_key', 'Please provide a valid key')
# dsb = DataStoreBase()
# retval = dsb.get_data_common(None,
# user_id,
# 'api_user_data_get',
# key, default)
#
# # json_value = json.dumps(retval, ensure_ascii=False, indent=2) # just return raw text
# return retval
#
def user_multi_get(user_id):
"""Get all data values in the data_store for a userid"""
if user_id is None:
@ -108,7 +31,7 @@ def user_multi_get(user_id):
def datastore_del(id):
"""Delete a data store item for a user_id and a key"""
"""Delete a data store item for a key"""
session.query(DataStoreModel).filter_by(id=id).delete()
session.commit()
json_value = json.dumps('deleted', ensure_ascii=False, indent=2)
@ -116,7 +39,7 @@ def datastore_del(id):
def datastore_get(id):
"""Delete a data store item for a user_id and a key"""
"""retrieve a data store item by a key"""
item = session.query(DataStoreModel).filter_by(id=id).first()
results = DataStoreSchema(many=False).dump(item)
return results
@ -130,12 +53,8 @@ def update_datastore(id, body):
item = session.query(DataStoreModel).filter_by(id=id).first()
if item is None:
raise ApiError('unknown_item', 'The item "' + id + '" is not recognized.')
#print(body)
# I'm not sure if there is a generic way to use the
# schema to both parse the body and update the SQLAlchemy record
for key in body:
if hasattr(item, key):
setattr(item, key, body[key])
DataStoreSchema().load(body, instance=item, session=session)
item.last_updated = datetime.now()
session.add(item)
session.commit()
@ -158,6 +77,7 @@ def add_datastore(body):
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 ')
present = 0
for field in ['user_id','study_id','file_id']:
if field in body:
@ -166,12 +86,7 @@ def add_datastore(body):
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')
item = DataStoreModel(key=body['key'], value=body['value'])
# I'm not sure if there is a generic way to use the
# schema to both parse the body and update the SQLAlchemy record
for key in body:
if hasattr(item, key):
setattr(item, key, body[key])
item = DataStoreSchema().load(body)
item.last_updated = datetime.now()
session.add(item)
session.commit()

View File

@ -1,5 +1,6 @@
from flask_marshmallow.sqla import SQLAlchemyAutoSchema
from marshmallow import EXCLUDE
from marshmallow_sqlalchemy import ModelSchema
from sqlalchemy import func
import marshmallow
from marshmallow import INCLUDE, fields
@ -20,13 +21,8 @@ class DataStoreModel(db.Model):
value = db.Column(db.String)
class DataStoreSchema(ma.Schema):
id = fields.Integer(required=False)
key = fields.String(required=True)
last_updated = fields.DateTime(server_default=func.now(), onupdate=func.now())
workflow_id = fields.Integer()
study_id = fields.Integer(allow_none=True)
task_id = fields.String()
spec_id = fields.String()
user_id = fields.String(allow_none=True)
value = fields.String()
class DataStoreSchema(SQLAlchemyAutoSchema):
class Meta:
model = DataStoreModel
load_instance = True
sqla_session = db.session

View File

@ -19,8 +19,8 @@ class DataStoreTest(BaseTest):
"task_id": "MyTask",
"spec_id": "My Spec Name",
"value": "Some Value"
}
def add_test_study_data(self):
study_data = DataStoreSchema().dump(self.TEST_STUDY_ITEM)
rv = self.app.post('/v1.0/datastore',
@ -60,11 +60,9 @@ class DataStoreTest(BaseTest):
d = api_response.get_data(as_text=True)
study_data = DataStoreSchema().loads(d)
self.assertEqual(study_data['key'], self.TEST_STUDY_ITEM['key'])
self.assertEqual(study_data['value'], self.TEST_STUDY_ITEM['value'])
self.assertEqual(study_data['user_id'], None)
self.assertEqual(study_data.key, self.TEST_STUDY_ITEM['key'])
self.assertEqual(study_data.value, self.TEST_STUDY_ITEM['value'])
self.assertEqual(study_data.user_id, None)
def test_update_datastore(self):
self.load_example_data()
@ -72,20 +70,18 @@ class DataStoreTest(BaseTest):
new_study = session.query(DataStoreModel).filter_by(id=new_study["id"]).first()
new_study.value = 'MyNewValue'
api_response = self.app.put('/v1.0/datastore/%i' % new_study.id,
data=DataStoreSchema().dump(new_study),
data=DataStoreSchema().dumps(new_study),
headers=self.logged_in_headers(), content_type="application/json")
self.assert_success(api_response)
api_response = self.app.get('/v1.0/datastore/%i' % new_study.id,
headers=self.logged_in_headers(), content_type="application/json")
self.assert_success(api_response)
study_data = DataStoreSchema().loads(api_response.get_data(as_text=True))
self.assertEqual(study_data['key'], self.TEST_STUDY_ITEM['key'])
self.assertEqual(study_data['value'], 'MyNewValue')
self.assertEqual(study_data['user_id'], None)
self.assertEqual(study_data.key, self.TEST_STUDY_ITEM['key'])
self.assertEqual(study_data.value, 'MyNewValue')
self.assertEqual(study_data.user_id, None)
def test_delete_datastore(self):
self.load_example_data()
@ -97,10 +93,8 @@ class DataStoreTest(BaseTest):
studyreponse = session.query(DataStoreModel).filter_by(id=oldid).first()
self.assertEqual(studyreponse,None)
def test_data_crosstalk(self):
"""Test to make sure that data saved for user or study is not acessible from the other method"""
"""Test to make sure that data saved for user or study is not accessible from the other method"""
self.load_example_data()
new_study = self.add_test_study_data()