*** WIP ***

This commit is contained in:
mike cullerton 2022-01-07 15:58:23 -05:00
parent dc27f795c8
commit f537fd7fbf
5 changed files with 12 additions and 11 deletions

View File

@ -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.

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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')