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

Merge branch 'mcq-fixes' into 'develop'

Several multiple choice option fixes

See merge request !10
parents 2458238e 7b69173a
No related branches found
No related tags found
6 merge requests!181Pregrading and multiple choice questions,!169Pregrading and creation of multiple choice options on the exam,!165Remove data folder,!155Fix exam widget,!145WIP: Add test coverage tools,!144Fix/delete mc option
......@@ -20,7 +20,6 @@ def upgrade():
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('x', 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('problem_id', sa.Integer(), nullable=False),
sa.Column('feedback_id', sa.Integer(), nullable=True),
......
......@@ -16,7 +16,8 @@
"analyze": "webpack --config webpack.prod.js --profile --json > stats.json; webpack-bundle-analyzer stats.json zesje/static",
"migrate:dev": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db upgrade",
"migrate": "FLASK_APP=zesje/__init__.py flask db upgrade",
"prepare-migration": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db migrate"
"prepare-migration": "ZESJE_SETTINGS=$(pwd)/zesje.dev.cfg FLASK_APP=zesje/__init__.py flask db migrate",
"migrate-down": "FLASK_APP=zesje/__init__.py flask db downgrade"
},
"standard": {
"parser": "babel-eslint",
......
......@@ -49,7 +49,9 @@ api.add_resource(RenderedEmailTemplate,
api.add_resource(Email,
'/email/<int:exam_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
......
......@@ -3,6 +3,25 @@ from flask_restful import Resource, reqparse
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):
put_parser = reqparse.RequestParser()
......@@ -10,15 +29,15 @@ class MultipleChoice(Resource):
# Arguments that have to be supplied in the request body
put_parser.add_argument('x', 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('problem_id', type=int, required=True)
put_parser.add_argument('feedback_id', type=int, required=True)
def put(self, id):
"""Puts a multiple choice option to the database
def put(self, id=None):
"""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
----------
......@@ -27,43 +46,33 @@ class MultipleChoice(Resource):
args = self.put_parser.parse_args()
# Get request arguments
x = args['x']
y = args['y']
label = args['label']
problem_id = args['problem_id']
feedback_id = args['feedback_id']
page = args['page']
mc_entry = MultipleChoiceOption.query.get(id)
# If entry is not present insert
if not mc_entry:
mc_entry = MultipleChoiceOption(
id=id,
x=x,
y=y,
page=page,
label=label,
problem_id=problem_id,
feedback_id=feedback_id
)
if not id:
# Insert new entry into the database
mc_entry = MultipleChoiceOption()
set_mc_data(mc_entry, x, y, problem_id, feedback_id, label)
db.session.add(mc_entry)
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
mc_entry.x = x
mc_entry.y = y
mc_entry.label = label
mc_entry.problem_id = problem_id
mc_entry.feedback_id = feedback_id
mc_entry.page = page
if not mc_entry:
return dict(status=404, message=f"Multiple choice question with id {id} does not exist"), 404
set_mc_data(mc_entry, x, y, problem_id, feedback_id, label)
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):
"""Fetches multiple choice option from the database
......
......@@ -166,11 +166,9 @@ class MultipleChoiceOption(db.Model):
x = Column(Integer, nullable=False)
y = Column(Integer, nullable=False)
page = Column(Integer, nullable=False)
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)
......
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