From f537fd7fbfc83af20574725b0ec434a46f3559e5 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Fri, 7 Jan 2022 15:58:23 -0500 Subject: [PATCH] *** WIP *** --- crc/api/file.py | 10 +++++----- crc/services/spec_file_service.py | 5 +++-- tests/base_test.py | 2 +- tests/files/test_file_service.py | 4 ++-- tests/test_lookup_service.py | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crc/api/file.py b/crc/api/file.py index e1da61f5..8fe0e28d 100644 --- a/crc/api/file.py +++ b/crc/api/file.py @@ -19,7 +19,7 @@ def to_file_api(file_model): if file_model.workflow_spec_id is not None: file_data_model = SpecFileService().get_spec_file_data(file_model.id) else: - file_data_model = FileService.get_file_data(file_model.id).data + file_data_model = FileService.get_file_data(file_model.id) return File.from_models(file_model, file_data_model, DocumentService.get_dictionary()) @@ -137,13 +137,13 @@ def get_file_data_by_hash(md5_hash): def get_file_data(file_id, version=None): file_model = session.query(FileModel).filter(FileModel.id==file_id).first() if file_model.workflow_spec_id is not None: - file_data = SpecFileService().get_spec_file_data(file_id) + file_data_model = SpecFileService().get_spec_file_data(file_id) else: - file_data = FileService.get_file_data(file_id, version).data - if file_data is None: + file_data_model = FileService.get_file_data(file_id, version) + if file_data_model is None: raise ApiError('no_such_file', f'The file id you provided ({file_id}) does not exist') return send_file( - io.BytesIO(file_data), + io.BytesIO(file_data_model.data), attachment_filename=file_model.name, mimetype=file_model.content_type, cache_timeout=-1 # Don't cache these files on the browser. diff --git a/crc/services/spec_file_service.py b/crc/services/spec_file_service.py index 3bb78e6a..d23e0843 100644 --- a/crc/services/spec_file_service.py +++ b/crc/services/spec_file_service.py @@ -1,5 +1,6 @@ import hashlib import json +import datetime import os from crc import app, session @@ -212,7 +213,7 @@ class SpecFileService(object): category_name = self.get_spec_file_category_name(spec_model) sync_file_root = self.get_sync_file_root() file_path = os.path.join(sync_file_root, category_name, spec_model.display_name, file_model.name) - stats = os.stat(file_path) + mtime = os.path.getmtime(file_path) with open(file_path, 'rb') as f_handle: spec_file_data = f_handle.read() size = len(spec_file_data) @@ -221,7 +222,7 @@ class SpecFileService(object): file_data_model = FileDataModel(data=spec_file_data, md5_hash=md5_checksum, size=size, - date_created=stats.st_mtime, + date_created=datetime.datetime.fromtimestamp(mtime), file_model_id=file_id) return file_data_model else: diff --git a/tests/base_test.py b/tests/base_test.py index 689ee030..20a3bb6c 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -187,7 +187,7 @@ class BaseTest(unittest.TestCase): self.assertGreater(len(files), 0) for file in files: # file_data = session.query(FileDataModel).filter_by(file_model_id=file.id).all() - file_data = SpecFileService().get_spec_file_data(file.id) + file_data = SpecFileService().get_spec_file_data(file.id).data self.assertIsNotNone(file_data) self.assertGreater(len(file_data), 0) diff --git a/tests/files/test_file_service.py b/tests/files/test_file_service.py index 180f55bc..bac45720 100644 --- a/tests/files/test_file_service.py +++ b/tests/files/test_file_service.py @@ -244,14 +244,14 @@ class TestFileService(BaseTest): file_model = SpecFileService().add_workflow_spec_file(spec, file_name, content_type, file_data) # This reads from a file on the filesystem - spec_file_data = SpecFileService().get_spec_file_data(file_model.id) + spec_file_data = SpecFileService().get_spec_file_data(file_model.id).data self.assertEqual(file_data, spec_file_data) def test_delete_workflow_spec_file(self): self.load_example_data() file_model = session.query(FileModel).filter(column('workflow_spec_id').isnot(None)).first() - file_data_before = SpecFileService().get_spec_file_data(file_model.id) + file_data_before = SpecFileService().get_spec_file_data(file_model.id).data self.assertGreater(len(file_data_before), 0) SpecFileService().delete_spec_file(file_model.id) diff --git a/tests/test_lookup_service.py b/tests/test_lookup_service.py index 3da30732..2bb6088e 100644 --- a/tests/test_lookup_service.py +++ b/tests/test_lookup_service.py @@ -183,7 +183,7 @@ class TestLookupService(BaseTest): # Using an old xls file should raise an error file_model_xls = session.query(FileModel).filter(FileModel.name == 'sponsors.xls').first() - file_data_xls = SpecFileService().get_spec_file_data(file_model_xls.id) + file_data_xls = SpecFileService().get_spec_file_data(file_model_xls.id).data # file_data_model_xls = session.query(FileDataModel).filter(FileDataModel.file_model_id == file_model_xls.id).first() with self.assertRaises(ApiError) as ae: LookupService.build_lookup_table(file_model_xls.id, 'sponsors.xls', file_data_xls, 'CUSTOMER_NUMBER', 'CUSTOMER_NAME')