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

add a PDF model and implement getting/setting to DB and saving PDF

parent 91d25c59
Branches
Tags
No related merge requests found
......@@ -30,8 +30,8 @@ def setup():
auth.init_app(app)
data_dir = app.config['DATA_DIRECTORY']
if not os.path.exists(data_dir):
os.makedirs(data_dir)
os.makedirs(data_dir, exist_ok=True)
os.makedirs(os.path.join(data_dir, 'pdfs'), exist_ok=True)
db.bind('sqlite', f"{app.config['DATA_DIRECTORY']}/course.sqlite", create_db=True)
db.generate_mapping(create_tables=True)
......
......@@ -28,6 +28,7 @@ class Exam(db.Entity):
yaml_path = Required(str)
submissions = Set('Submission')
problems = Set('Problem')
pdfs = Set('PDF')
class Submission(db.Entity):
......@@ -79,3 +80,10 @@ class Solution(db.Entity):
feedback = Set(FeedbackOption)
remarks = Optional(str)
class PDF(db.Entity):
"""Metadata on uploaded PDFs"""
exam = Required(Exam)
name = Required(str)
status = Required(str)
message = Optional(str)
......@@ -6,7 +6,7 @@ from werkzeug.datastructures import FileStorage
from pony import orm
from ..models import db, Exam
from ..models import db, Exam, PDF
class Pdfs(Resource):
......@@ -28,18 +28,18 @@ class Pdfs(Resource):
"""
return [
{
'id': 1,
'name': 'hello.pdf',
'status': 'processing',
'message': 'extracting images',
'id': pdf.id,
'name': pdf.name,
'status': pdf.status,
'message': pdf.message,
}
for pdf in PDF.select(lambda pdf: pdf.exam.id == exam_id)
]
post_parser = reqparse.RequestParser()
post_parser.add_argument('pdf', type=FileStorage, required=True,
location='files')
@orm.db_session
def post(self, exam_id):
"""Upload a PDF
......@@ -56,9 +56,21 @@ class Pdfs(Resource):
message : str
"""
args = self.post_parser.parse_args()
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
return {
'id': 1,
'name': 'hello.pdf',
'status': 'processing',
'message': 'going',
'id': pdf.id,
'name': pdf.name,
'status': pdf.status,
'message': pdf.message
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment