Merge pull request #337 from sartography/add-by-user-61

Add by user #61
This commit is contained in:
Dan Funk 2021-07-07 11:25:45 -04:00 committed by GitHub
commit 8cb7cbc0ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 9 deletions

View File

@ -68,6 +68,7 @@ class FileDataModel(db.Model):
date_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) date_created = db.Column(db.DateTime(timezone=True), server_default=func.now())
file_model_id = db.Column(db.Integer, db.ForeignKey('file.id')) file_model_id = db.Column(db.Integer, db.ForeignKey('file.id'))
file_model = db.relationship("FileModel", foreign_keys=[file_model_id]) file_model = db.relationship("FileModel", foreign_keys=[file_model_id])
user_uid = db.Column(db.String, db.ForeignKey('user.uid'), nullable=True)
class FileModel(db.Model): class FileModel(db.Model):
@ -114,6 +115,7 @@ class File(object):
instance.last_modified = data_model.date_created instance.last_modified = data_model.date_created
instance.latest_version = data_model.version instance.latest_version = data_model.version
instance.size = data_model.size instance.size = data_model.size
instance.user_uid = data_model.user_uid
else: else:
instance.last_modified = None instance.last_modified = None
instance.latest_version = None instance.latest_version = None
@ -141,7 +143,7 @@ class FileSchema(Schema):
fields = ["id", "name", "is_status", "is_reference", "content_type", fields = ["id", "name", "is_status", "is_reference", "content_type",
"primary", "primary_process_id", "workflow_spec_id", "workflow_id", "primary", "primary_process_id", "workflow_spec_id", "workflow_id",
"irb_doc_code", "last_modified", "latest_version", "type", "size", "data_store", "irb_doc_code", "last_modified", "latest_version", "type", "size", "data_store",
"document"] "document", "user_uid"]
unknown = INCLUDE unknown = INCLUDE
type = EnumField(FileType) type = EnumField(FileType)

View File

@ -19,6 +19,7 @@ from crc.models.data_store import DataStoreModel
from crc.models.file import FileType, FileDataModel, FileModel, LookupFileModel, LookupDataModel from crc.models.file import FileType, FileDataModel, FileModel, LookupFileModel, LookupDataModel
from crc.models.workflow import WorkflowSpecModel, WorkflowModel, WorkflowSpecDependencyFile from crc.models.workflow import WorkflowSpecModel, WorkflowModel, WorkflowSpecDependencyFile
from crc.services.cache_service import cache from crc.services.cache_service import cache
from crc.services.user_service import UserService
import re import re
@ -168,10 +169,14 @@ class FileService(object):
except XMLSyntaxError as xse: except XMLSyntaxError as xse:
raise ApiError("invalid_xml", "Failed to parse xml: " + str(xse), file_name=file_model.name) raise ApiError("invalid_xml", "Failed to parse xml: " + str(xse), file_name=file_model.name)
try:
user_uid = UserService.current_user().uid
except ApiError as ae:
user_uid = None
new_file_data_model = FileDataModel( new_file_data_model = FileDataModel(
data=binary_data, file_model_id=file_model.id, file_model=file_model, data=binary_data, file_model_id=file_model.id, file_model=file_model,
version=version, md5_hash=md5_checksum, date_created=datetime.utcnow(), version=version, md5_hash=md5_checksum, date_created=datetime.utcnow(),
size=size size=size, user_uid=user_uid
) )
session.add_all([file_model, new_file_data_model]) session.add_all([file_model, new_file_data_model])
session.commit() session.commit()

View File

@ -0,0 +1,27 @@
"""add user_uid column to file_data table
Revision ID: 30e017a03948
Revises: bbf064082623
Create Date: 2021-07-06 10:39:04.661704
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '30e017a03948'
down_revision = 'bbf064082623'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('file_data', sa.Column('user_uid', sa.String(), nullable=True))
op.create_foreign_key(None, 'file_data', 'user', ['user_uid'], ['uid'])
def downgrade():
# op.drop_constraint('file_data_user_uid_fkey', 'file_data', type_='foreignkey')
# op.execute("update file_data set user_uid = NULL WHERE user_uid IS NOT NULL")
op.drop_column('file_data', 'user_uid')

View File

@ -148,13 +148,6 @@ class BaseTest(unittest.TestCase):
otherwise it depends on a small setup for running tests.""" otherwise it depends on a small setup for running tests."""
from example_data import ExampleDataLoader from example_data import ExampleDataLoader
ExampleDataLoader.clean_db() ExampleDataLoader.clean_db()
if use_crc_data:
ExampleDataLoader().load_all()
elif use_rrt_data:
ExampleDataLoader().load_rrt()
else:
ExampleDataLoader().load_test_data()
# If in production mode, only add the first user. # If in production mode, only add the first user.
if app.config['PRODUCTION']: if app.config['PRODUCTION']:
session.add(UserModel(**self.users[0])) session.add(UserModel(**self.users[0]))
@ -162,6 +155,13 @@ class BaseTest(unittest.TestCase):
for user_json in self.users: for user_json in self.users:
session.add(UserModel(**user_json)) session.add(UserModel(**user_json))
if use_crc_data:
ExampleDataLoader().load_all()
elif use_rrt_data:
ExampleDataLoader().load_rrt()
else:
ExampleDataLoader().load_test_data()
session.commit() session.commit()
for study_json in self.studies: for study_json in self.studies:
study_model = StudyModel(**study_json) study_model = StudyModel(**study_json)

View File

@ -126,6 +126,7 @@ class TestFilesApi(BaseTest):
self.assertEqual(FileType.xlsx, file.type) self.assertEqual(FileType.xlsx, file.type)
self.assertTrue(file.is_reference) self.assertTrue(file.is_reference)
self.assertEqual("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.content_type) self.assertEqual("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", file.content_type)
self.assertEqual('dhf8r', json_data['user_uid'])
def test_set_reference_file_bad_extension(self): def test_set_reference_file_bad_extension(self):
file_name = DocumentService.DOCUMENT_LIST file_name = DocumentService.DOCUMENT_LIST