diff --git a/crc/scripts/user_data_get.py b/crc/scripts/user_data_get.py new file mode 100644 index 00000000..98d8b8b0 --- /dev/null +++ b/crc/scripts/user_data_get.py @@ -0,0 +1,28 @@ +import requests + +from crc.scripts.script import Script, DataStoreBase +from crc import session +from crc.models.data_store import DataStoreModel +from flask import g + +class UserDataGet(Script,DataStoreBase): + def get_description(self): + return """Gets user data from the data store.""" + + + def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs): + self.do_task(task, study_id, workflow_id, *args, **kwargs) + + def do_task(self, task, study_id, workflow_id, *args, **kwargs): + self.check_args(args,2) + study = session.query(DataStoreModel).filter_by(study_id=None,user_id=g.user.uid,key=args[0]).first() + if study: + return study.value + else: + return args[1] + + def check_args(self, args, maxlen=1): + if len(args) < 1 or len(args) > maxlen : + raise ApiError(code="missing_argument", + message="The study_data_get script takes either one or two arguments, starting with the key and an " + \ + "optional default") diff --git a/crc/scripts/user_data_set.py b/crc/scripts/user_data_set.py new file mode 100644 index 00000000..080ff76a --- /dev/null +++ b/crc/scripts/user_data_set.py @@ -0,0 +1,50 @@ +import requests + +from crc.scripts.script import Script, DataStoreBase +from crc import session +from crc.models.workflow import WorkflowModel +from crc.models.data_store import DataStoreModel + +from flask import g + +class UserDataSet(Script,DataStoreBase): + def get_description(self): + return """Sets user data to the data store.""" + + + def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs): + self.check_args(args) + workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first() + self.get_prev_value(study_id,args[0]) + + + def do_task(self, task, study_id, workflow_id, *args, **kwargs): + self.check_args(args) + study = self.get_prev_value(study_id,args[0]) + workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first() + if study is not None: + prev_value = study.key + else: + prev_value = None + study = DataStoreModel(key=args[0],value=args[1], + study_id=None, + task_id=task.id, + user_id=g.user.uid, # Make this available to any study, but only one user + workflow_id= workflow_id, + spec_id=workflow.workflow_spec_id) + + overwritten = self.overwritten(study.value,prev_value) + session.add(study) + return (study.value, prev_value, overwritten) + + + def get_prev_value(self,study_id,key): + study = session.query(DataStoreModel).filter_by(study_id=None,user_id=g.user.uid,key=key).first() + return study + + + def check_args(self, args): + if len(args) != 2: + raise ApiError(code="missing_argument", + message="The study_data_set script takes two arguments, starting with the key and a " +\ + "value for the key") diff --git a/tests/data/study_sponsors_data_store/study_sponsors_data_store.bpmn b/tests/data/study_sponsors_data_store/study_sponsors_data_store.bpmn index 66112583..9ecae660 100644 --- a/tests/data/study_sponsors_data_store/study_sponsors_data_store.bpmn +++ b/tests/data/study_sponsors_data_store/study_sponsors_data_store.bpmn @@ -12,7 +12,7 @@ - Flow_0g9waf3 + Flow_05136ua @@ -26,12 +26,18 @@ Flow_1oeqjuy out = study_data_get('testme','bogus') - + Flow_1oeqjuy Flow_0g9waf3 study_data_set('testme','badval') + + Flow_0g9waf3 + Flow_05136ua + empty = user_data_get('testme','empty') + + @@ -50,7 +56,7 @@ - + @@ -67,12 +73,19 @@ - - + + + + + + + + + diff --git a/tests/study/test_study_data_store_script.py b/tests/study/test_study_data_store_script.py index b0a2a1e8..8e4ec51d 100644 --- a/tests/study/test_study_data_store_script.py +++ b/tests/study/test_study_data_store_script.py @@ -1,9 +1,12 @@ from unittest.mock import patch +import flask + from tests.base_test import BaseTest from crc import session, app from crc.models.study import StudyModel +from crc.models.user import UserModel from crc.services.study_service import StudyService from crc.services.workflow_processor import WorkflowProcessor from crc.services.workflow_service import WorkflowService @@ -27,6 +30,7 @@ class TestSudySponsorsScript(BaseTest): mock_get.return_value.ok = True mock_get.return_value.text = self.protocol_builder_response('sponsors.json') + flask.g.user = UserModel(uid='dhf8r') app.config['PB_ENABLED'] = True self.load_example_data() @@ -41,5 +45,6 @@ class TestSudySponsorsScript(BaseTest): data = processor.next_task().data self.assertIn('sponsors', data) self.assertIn('out', data) + self.assertEquals('empty', data['empty']) self.assertEquals('newval', data['out']) self.assertEquals(3, len(data['sponsors']))