Skip to content
Snippets Groups Projects
Commit 77702495 authored by Ruben Young On's avatar Ruben Young On
Browse files

Page is now no longer an attribute of mc_option. PUT requests without an id...

Page is now no longer an attribute of mc_option. PUT requests without an id paramter are now possible and will be used as insertion
parent 2458238e
No related branches found
No related tags found
1 merge request!10Several multiple choice option fixes
Pipeline #17525 passed
...@@ -20,7 +20,6 @@ def upgrade(): ...@@ -20,7 +20,6 @@ def upgrade():
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('x', sa.Integer(), nullable=False), sa.Column('x', sa.Integer(), nullable=False),
sa.Column('y', sa.Integer(), nullable=False), sa.Column('y', sa.Integer(), nullable=False),
sa.Column('page', sa.Integer(), nullable=False),
sa.Column('label', sa.String(), nullable=True), sa.Column('label', sa.String(), nullable=True),
sa.Column('problem_id', sa.Integer(), nullable=False), sa.Column('problem_id', sa.Integer(), nullable=False),
sa.Column('feedback_id', sa.Integer(), nullable=True), sa.Column('feedback_id', sa.Integer(), nullable=True),
......
...@@ -49,7 +49,9 @@ api.add_resource(RenderedEmailTemplate, ...@@ -49,7 +49,9 @@ api.add_resource(RenderedEmailTemplate,
api.add_resource(Email, api.add_resource(Email,
'/email/<int:exam_id>', '/email/<int:exam_id>',
'/email/<int:exam_id>/<int:student_id>') '/email/<int:exam_id>/<int:student_id>')
api.add_resource(MultipleChoice, '/mult-choice/<int:id>') api.add_resource(MultipleChoice,
'/mult-choice/<int:id>',
'/mult-choice/')
# Other resources that don't return JSON # Other resources that don't return JSON
......
...@@ -3,6 +3,25 @@ from flask_restful import Resource, reqparse ...@@ -3,6 +3,25 @@ from flask_restful import Resource, reqparse
from ..database import db, MultipleChoiceOption from ..database import db, MultipleChoiceOption
def set_mc_data(mc_entry, x, y, problem_id, feedback_id, label):
"""Sets the data of a MultipleChoiceOption ORM object.
Parameters:
-----------
mc_entry: The MultipleChoiceOption object
x: the x-position of the MultipleChoiceOption object.
y: the y-position of the MultipleChoiceOption object.
problem_id: the problem the MultipleChoiceOption refers to
feedback_id: the feedback the MultipleChoiceOption refers to
label: label for the checkbox that this MultipleChoiceOption represents
"""
mc_entry.x = x
mc_entry.y = y
mc_entry.problem_id = problem_id
mc_entry.feedback_id = feedback_id
mc_entry.label = label
class MultipleChoice(Resource): class MultipleChoice(Resource):
put_parser = reqparse.RequestParser() put_parser = reqparse.RequestParser()
...@@ -10,15 +29,15 @@ class MultipleChoice(Resource): ...@@ -10,15 +29,15 @@ class MultipleChoice(Resource):
# Arguments that have to be supplied in the request body # Arguments that have to be supplied in the request body
put_parser.add_argument('x', type=int, required=True) put_parser.add_argument('x', type=int, required=True)
put_parser.add_argument('y', type=int, required=True) put_parser.add_argument('y', type=int, required=True)
put_parser.add_argument('page', type=int, required=True)
put_parser.add_argument('label', type=str, required=False) put_parser.add_argument('label', type=str, required=False)
put_parser.add_argument('problem_id', type=int, required=True) put_parser.add_argument('problem_id', type=int, required=True)
put_parser.add_argument('feedback_id', type=int, required=True) put_parser.add_argument('feedback_id', type=int, required=True)
def put(self, id): def put(self, id=None):
"""Puts a multiple choice option to the database """Adds or updates a multiple choice option to the database
If the specified ID is already present, the current option will be updated. If the parameter id is not present, a new multiple choice question
will be inserted with the data provided in the request body.
Parameters Parameters
---------- ----------
...@@ -27,43 +46,33 @@ class MultipleChoice(Resource): ...@@ -27,43 +46,33 @@ class MultipleChoice(Resource):
args = self.put_parser.parse_args() args = self.put_parser.parse_args()
# Get request arguments
x = args['x'] x = args['x']
y = args['y'] y = args['y']
label = args['label'] label = args['label']
problem_id = args['problem_id'] problem_id = args['problem_id']
feedback_id = args['feedback_id'] feedback_id = args['feedback_id']
page = args['page']
mc_entry = MultipleChoiceOption.query.get(id)
# If entry is not present insert if not id:
if not mc_entry: # Insert new entry into the database
mc_entry = MultipleChoiceOption( mc_entry = MultipleChoiceOption()
id=id, set_mc_data(mc_entry, x, y, problem_id, feedback_id, label)
x=x,
y=y,
page=page,
label=label,
problem_id=problem_id,
feedback_id=feedback_id
)
db.session.add(mc_entry) db.session.add(mc_entry)
db.session.commit() db.session.commit()
return dict(status=200, message='Multiple choice question inserted'), 200 return dict(status=200, message=f'New multiple choice question with id {mc_entry.id} inserted'), 200
# Update existing entry otherwise
mc_entry = MultipleChoiceOption.query.get(id)
# Otherwise, update current entry if not mc_entry:
mc_entry.x = x return dict(status=404, message=f"Multiple choice question with id {id} does not exist"), 404
mc_entry.y = y
mc_entry.label = label
mc_entry.problem_id = problem_id
mc_entry.feedback_id = feedback_id
mc_entry.page = page
set_mc_data(mc_entry, x, y, problem_id, feedback_id, label)
db.session.commit() db.session.commit()
return dict(status=200, message='Multiple choice question updated'), 200 return dict(status=200, message=f'Multiple choice question with id {id} updated'), 200
def get(self, id): def get(self, id):
"""Fetches multiple choice option from the database """Fetches multiple choice option from the database
......
...@@ -124,7 +124,7 @@ class Solution(db.Model): ...@@ -124,7 +124,7 @@ class Solution(db.Model):
submission_id = Column(Integer, ForeignKey('submission.id'), nullable=False) # backref submission submission_id = Column(Integer, ForeignKey('submission.id'), nullable=False) # backref submission
problem_id = Column(Integer, ForeignKey('problem.id'), nullable=False) # backref problem problem_id = Column(Integer, ForeignKey('problem.id'), nullable=False) # backref problem
# if grader_id, and thus graded_by, is null, this has not yet been graded # if grader_id, and thus graded_by, is null, this has not yet been graded
grader_id = Column(Integer, ForeignKey('grader.id'), nullable=True) # backref graded_by f_id = Column(Integer, ForeignKey('grader.id'), nullable=True) # backref graded_by
graded_at = Column(DateTime, nullable=True) graded_at = Column(DateTime, nullable=True)
feedback = db.relationship('FeedbackOption', secondary=solution_feedback, backref='solutions', lazy='subquery') feedback = db.relationship('FeedbackOption', secondary=solution_feedback, backref='solutions', lazy='subquery')
remarks = Column(Text) remarks = Column(Text)
...@@ -166,12 +166,10 @@ class MultipleChoiceOption(db.Model): ...@@ -166,12 +166,10 @@ class MultipleChoiceOption(db.Model):
x = Column(Integer, nullable=False) x = Column(Integer, nullable=False)
y = Column(Integer, nullable=False) y = Column(Integer, nullable=False)
page = Column(Integer, nullable=False)
label = Column(String, nullable=True) label = Column(String, nullable=True)
problem_id = Column(Integer, ForeignKey('solution.id'), nullable=False) problem_id = Column(Integer, ForeignKey('problem.id'), nullable=False)
feedback_id = Column(Integer, ForeignKey('feedback_option.id'), nullable=True) feedback_id = Column(Integer, ForeignKey('feedback_option.id'), nullable=False)
  • Contributor

    feedback_id should not have been changed to nullable=False

    • Change to nullable=True in migrations.py
    Edited by Hidde Leistra
  • Contributor

    Fixed in 707f922e

  • Please register or sign in to reply
class ExamWidget(Widget): class ExamWidget(Widget):
......
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