2020-05-14 21:13:47 +00:00
|
|
|
import glob
|
2019-12-27 18:50:03 +00:00
|
|
|
import os
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-01-14 16:45:12 +00:00
|
|
|
from crc import app, db, session
|
2020-05-14 21:13:47 +00:00
|
|
|
from crc.models.file import CONTENT_TYPES
|
2022-02-02 18:30:54 +00:00
|
|
|
from crc.models.workflow import WorkflowSpecModel
|
2021-07-06 17:10:20 +00:00
|
|
|
from crc.services.document_service import DocumentService
|
2022-01-12 19:37:33 +00:00
|
|
|
from crc.services.reference_file_service import ReferenceFileService
|
2022-01-07 20:34:51 +00:00
|
|
|
from crc.services.spec_file_service import SpecFileService
|
2021-07-06 17:10:20 +00:00
|
|
|
from crc.services.study_service import StudyService
|
2019-12-18 19:02:17 +00:00
|
|
|
|
2020-02-27 15:30:16 +00:00
|
|
|
|
2019-12-18 19:02:17 +00:00
|
|
|
class ExampleDataLoader:
|
2020-03-04 18:40:25 +00:00
|
|
|
@staticmethod
|
|
|
|
def clean_db():
|
|
|
|
session.flush() # Clear out any transactions before deleting it all to avoid spurious errors.
|
2021-08-06 13:50:57 +00:00
|
|
|
engine = session.bind.engine
|
|
|
|
connection = engine.connect()
|
2020-03-04 18:40:25 +00:00
|
|
|
for table in reversed(db.metadata.sorted_tables):
|
2021-08-06 13:50:57 +00:00
|
|
|
if engine.dialect.has_table(connection, table):
|
|
|
|
session.execute(table.delete())
|
2020-05-23 02:04:11 +00:00
|
|
|
session.commit()
|
|
|
|
session.flush()
|
2020-03-04 18:40:25 +00:00
|
|
|
|
2021-10-05 17:36:53 +00:00
|
|
|
def create_spec(self, id, display_name="", description="", filepath=None, master_spec=False,
|
2021-08-03 14:02:22 +00:00
|
|
|
category_id=None, display_order=None, from_tests=False, standalone=False, library=False):
|
2020-01-23 20:32:53 +00:00
|
|
|
"""Assumes that a directory exists in static/bpmn with the same name as the given id.
|
|
|
|
further assumes that the [id].bpmn is the primary file for the workflow.
|
2019-12-31 21:32:47 +00:00
|
|
|
returns an array of data models to be added to the database."""
|
2020-03-05 21:55:46 +00:00
|
|
|
global file
|
2019-12-31 21:32:47 +00:00
|
|
|
spec = WorkflowSpecModel(id=id,
|
|
|
|
display_name=display_name,
|
2020-03-15 19:54:13 +00:00
|
|
|
description=description,
|
2020-03-27 19:32:07 +00:00
|
|
|
is_master_spec=master_spec,
|
2020-04-10 15:13:43 +00:00
|
|
|
category_id=category_id,
|
2021-04-26 12:52:12 +00:00
|
|
|
display_order=display_order,
|
2021-08-03 14:02:22 +00:00
|
|
|
standalone=standalone,
|
|
|
|
library=library)
|
2020-03-04 18:40:25 +00:00
|
|
|
db.session.add(spec)
|
|
|
|
db.session.commit()
|
2020-05-25 16:29:05 +00:00
|
|
|
if not filepath and not from_tests:
|
2020-10-05 21:35:35 +00:00
|
|
|
filepath = os.path.join(app.root_path, 'static', 'bpmn', id, "*.*")
|
2020-05-25 16:29:05 +00:00
|
|
|
if not filepath and from_tests:
|
2020-10-05 21:35:35 +00:00
|
|
|
filepath = os.path.join(app.root_path, '..', 'tests', 'data', id, "*.*")
|
2020-05-25 16:29:05 +00:00
|
|
|
|
2020-01-23 20:32:53 +00:00
|
|
|
files = glob.glob(filepath)
|
|
|
|
for file_path in files:
|
2020-10-05 21:35:35 +00:00
|
|
|
if os.path.isdir(file_path):
|
|
|
|
continue # Don't try to process sub directories
|
|
|
|
|
2020-01-23 20:32:53 +00:00
|
|
|
noise, file_extension = os.path.splitext(file_path)
|
|
|
|
filename = os.path.basename(file_path)
|
2020-03-13 18:57:28 +00:00
|
|
|
is_primary = filename.lower() == id + '.bpmn'
|
2020-05-05 20:15:38 +00:00
|
|
|
file = None
|
2020-01-23 20:32:53 +00:00
|
|
|
try:
|
2020-03-13 18:57:28 +00:00
|
|
|
file = open(file_path, 'rb')
|
2020-02-10 21:19:23 +00:00
|
|
|
data = file.read()
|
2020-03-04 18:40:25 +00:00
|
|
|
content_type = CONTENT_TYPES[file_extension[1:]]
|
2022-02-02 17:59:56 +00:00
|
|
|
SpecFileService.add_file(workflow_spec=spec, file_name=filename, binary_data=data)
|
|
|
|
if is_primary:
|
|
|
|
SpecFileService.set_primary_bpmn(spec, filename)
|
2020-03-05 16:18:20 +00:00
|
|
|
except IsADirectoryError as de:
|
|
|
|
# Ignore sub directories
|
|
|
|
pass
|
2020-01-23 20:32:53 +00:00
|
|
|
finally:
|
2020-03-05 21:55:46 +00:00
|
|
|
if file:
|
|
|
|
file.close()
|
2020-03-04 18:40:25 +00:00
|
|
|
return spec
|
2020-03-20 11:41:21 +00:00
|
|
|
|
|
|
|
def load_reference_documents(self):
|
2022-02-02 17:59:56 +00:00
|
|
|
file_path = os.path.join(app.root_path, 'static', 'reference', 'documents.xlsx')
|
2020-03-20 11:41:21 +00:00
|
|
|
file = open(file_path, "rb")
|
2022-01-12 19:37:33 +00:00
|
|
|
ReferenceFileService.add_reference_file(DocumentService.DOCUMENT_LIST,
|
2022-02-02 17:59:56 +00:00
|
|
|
file.read())
|
2020-05-07 17:57:24 +00:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
file_path = os.path.join(app.root_path, 'static', 'reference', 'investigators.xlsx')
|
|
|
|
file = open(file_path, "rb")
|
2022-01-12 19:37:33 +00:00
|
|
|
ReferenceFileService.add_reference_file(StudyService.INVESTIGATOR_LIST,
|
2022-02-02 17:59:56 +00:00
|
|
|
file.read())
|
2020-03-20 11:41:21 +00:00
|
|
|
file.close()
|
2020-08-27 17:55:27 +00:00
|
|
|
|