Verified Commit 4a905dc5 authored by Hugo Kerstens's avatar Hugo Kerstens
Browse files

Move retrieve page method to page class

parent d7bcb4c1
......@@ -144,6 +144,12 @@ class Page(db.Model):
def abs_path(self):
return os.path.join(current_app.config['DATA_DIRECTORY'], self.path)
@classmethod
def retrieve(cls, copy, page_number):
"""Retrieve an existing page or create a new one"""
return (cls.query.filter(cls.copy == copy, cls.number == page_number).one_or_none() or
cls(copy=copy, number=page_number))
"""
Enum for the grading policy of a problem
......
from flask import current_app
from pathlib import Path
from .database import db, Exam, Page, Submission, Solution, Student, Copy
from .database import db, Exam, Submission, Solution, Student, Copy, Page
def process_page(image, page_info, file_info, exam_config, output_directory):
......@@ -24,7 +24,7 @@ def process_page(image, page_info, file_info, exam_config, output_directory):
image_dir = Path(output_directory) / 'submissions' / f'{copy.number}'
image_dir.mkdir(exist_ok=True, parents=True)
page = retrieve_page(copy, page)
page = Page.retrieve(copy, page)
# Delete old image of this page if it exists
if page.path:
......@@ -59,12 +59,6 @@ def retrieve_copy(exam, student_id, copy):
return copy
def retrieve_page(copy, page_number):
"""Returns a page associated with the given copy and page number"""
return (Page.query.filter(Page.copy == copy, Page.number == page_number).one_or_none() or
Page(copy=copy, number=page_number))
def create_submission(exam, student_id, validated):
"""Adds a new submission to the db for the given exam and student"""
sub = Submission(exam=exam, student_id=student_id, validated=validated)
......
......@@ -333,15 +333,22 @@ def add_to_correct_copy(image_path, barcode):
# We may have added this page in previous uploads but we only want a single
# 'Page' entry regardless
if Page.query.filter(Page.copy == copy, Page.number == barcode.page).one_or_none() is None:
rel_path = os.path.relpath(image_path, start=current_app.config['DATA_DIRECTORY'])
db.session.add(Page(path=rel_path, copy=copy, number=barcode.page))
page = Page.retrieve(copy, barcode.page)
if not page.path:
page.path = os.path.relpath(image_path, start=current_app.config['DATA_DIRECTORY'])
db.session.add(page)
db.session.commit()
return copy
def retrieve_page(copy, page_number):
"""Returns a page associated with the given copy and page number"""
return (Page.query.filter(Page.copy == copy, Page.number == page_number).one_or_none() or
Page(copy=copy, number=page_number))
def decode_barcode(image, exam_config):
"""Extract a barcode from a PIL Image."""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment