Skip to content
Snippets Groups Projects
Commit 3004f066 authored by Joseph Weston's avatar Joseph Weston
Browse files

create a single source of truth for paths to PDF uploads and DB

parent 7a893db1
No related branches found
No related tags found
No related merge requests found
......@@ -30,10 +30,12 @@ def setup():
auth.init_app(app)
data_dir = app.config['DATA_DIRECTORY']
app.config['PDF_DIRECTORY'] = os.path.join(data_dir, 'pdfs')
app.config['DB_PATH'] = os.path.join(data_dir, 'course.sqlite')
os.makedirs(data_dir, exist_ok=True)
os.makedirs(os.path.join(data_dir, 'pdfs'), exist_ok=True)
os.makedirs(app.config['PDF_DIRECTORY'], exist_ok=True)
db.bind('sqlite', f"{app.config['DATA_DIRECTORY']}/course.sqlite", create_db=True)
db.bind('sqlite', app.config['DB_PATH'], create_db=True)
db.generate_mapping(create_tables=True)
......
......@@ -59,14 +59,18 @@ class Pdfs(Resource):
if args['pdf'].mimetype != 'application/pdf':
return dict(message='Uploaded file is not a PDF'), 400
pdf_path = os.path.join(app.config['DATA_DIRECTORY'], 'pdfs', f'{pdf.id}.pdf')
with orm.db_session:
pdf = PDF(exam=Exam[exam_id], name=args['pdf'].filename,
status='processing', message='importing PDF')
# if we fail to save the PDF then we rollback the DB transaction
args['pdf'].save(pdf_path)
# TODO fire off subprocess
with orm.db_session:
try:
path = os.path.join(app.config['PDF_DIRECTORY'], f'{pdf.id}.pdf')
args['pdf'].save(path)
except Exception:
pdf.delete()
raise
return {
'id': pdf.id,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment