diff --git a/crc/models/data_store.py b/crc/models/data_store.py index f219e888..fe34a456 100644 --- a/crc/models/data_store.py +++ b/crc/models/data_store.py @@ -16,6 +16,7 @@ class DataStoreModel(db.Model): task_id = db.Column(db.String) spec_id = db.Column(db.String) user_id = db.Column(db.String, nullable=True) + file_id = db.Column(db.Integer, db.ForeignKey('file.id'), nullable=True) value = db.Column(db.String) diff --git a/crc/models/file.py b/crc/models/file.py index b1269f78..d8c4b824 100644 --- a/crc/models/file.py +++ b/crc/models/file.py @@ -6,7 +6,8 @@ from marshmallow_enum import EnumField from marshmallow_sqlalchemy import SQLAlchemyAutoSchema from sqlalchemy import func, Index from sqlalchemy.dialects.postgresql import UUID -from sqlalchemy.orm import deferred +from sqlalchemy.orm import deferred, relationship +from crc.models.data_store import DataStoreModel # this is needed by the relationship from crc import db, ma @@ -87,6 +88,7 @@ class FileModel(db.Model): # it instead, hide it in the interface. is_review = db.Column(db.Boolean, default=False, nullable=True) archived = db.Column(db.Boolean, default=False, nullable=False) + tags = relationship("DataStoreModel", cascade="all,delete", backref="file") class File(object): @classmethod diff --git a/crc/scripts/data_store_base.py b/crc/scripts/data_store_base.py index 98fd030a..4a81988d 100644 --- a/crc/scripts/data_store_base.py +++ b/crc/scripts/data_store_base.py @@ -38,14 +38,29 @@ class DataStoreBase(object): message=f"The {script_name} script takes two arguments, starting with the key and a " "value for the key") - def get_prev_value(self, study_id, user_id, key): - study = session.query(DataStoreModel).filter_by(study_id=study_id, user_id=user_id, key=key).first() + def get_prev_value(self, study_id, user_id, key, file_id): + study = session.query(DataStoreModel).filter_by(study_id=study_id, + user_id=user_id, + file_id=file_id, + key=key).first() return study - def set_data_common(self, task_id, study_id, user_id, workflow_id, workflow_spec_id, script_name, *args, **kwargs): + def set_data_common(self, + task_id, + study_id, + user_id, + workflow_id, + workflow_spec_id, + script_name, + file_id, + *args, + **kwargs): self.check_args_2(args, script_name=script_name) - study = self.get_prev_value(study_id=study_id, user_id=user_id, key=args[0]) + study = self.get_prev_value(study_id=study_id, + user_id=user_id, + file_id=file_id, + key=args[0]) if workflow_spec_id is None and workflow_id is not None: workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first() workflow_spec_id = workflow.workflow_spec_id @@ -57,6 +72,7 @@ class DataStoreBase(object): study_id=study_id, task_id=task_id, user_id=user_id, # Make this available to any User + file_id=file_id, workflow_id=workflow_id, spec_id=workflow_spec_id) study.value = args[1] @@ -68,14 +84,20 @@ class DataStoreBase(object): 'old_value': prev_value, 'overwritten': overwritten} - def get_data_common(self, study_id, user_id, script_name, *args): + def get_data_common(self, study_id, user_id, script_name, file_id=None, *args): self.check_args(args, 2, script_name) - study = session.query(DataStoreModel).filter_by(study_id=study_id, user_id=user_id, key=args[0]).first() + study = session.query(DataStoreModel).filter_by(study_id=study_id, + user_id=user_id, + file_id=file_id, + key=args[ + 0]).first() if study: return study.value else: return args[1] - def get_multi_common(self, study_id, user_id): - study = session.query(DataStoreModel).filter_by(study_id=study_id, user_id=user_id) + def get_multi_common(self, study_id, user_id, file_id=None): + study = session.query(DataStoreModel).filter_by(study_id=study_id, + user_id=user_id, + file_id=file_id) return study diff --git a/crc/scripts/study_data_get.py b/crc/scripts/study_data_get.py index 4b109dff..6ef6be4d 100644 --- a/crc/scripts/study_data_get.py +++ b/crc/scripts/study_data_get.py @@ -13,5 +13,6 @@ class StudyDataGet(Script,DataStoreBase): return self.get_data_common(study_id, None, 'study_data_get', + None, *args) diff --git a/crc/scripts/user_data_get.py b/crc/scripts/user_data_get.py index 4bbff767..4f181c13 100644 --- a/crc/scripts/user_data_get.py +++ b/crc/scripts/user_data_get.py @@ -15,4 +15,5 @@ class UserDataGet(Script, DataStoreBase): return self.get_data_common(None, g.user.uid, 'user_data_get', + None, *args)