diff --git a/migrations/versions/b46a2994605b_.py b/migrations/versions/b46a2994605b_.py index eb7b34e72f9898facdb7ff28b8d7b4473983c750..0c7e740429ee92a25f3feef629ead82d9cf409d1 100644 --- a/migrations/versions/b46a2994605b_.py +++ b/migrations/versions/b46a2994605b_.py @@ -21,11 +21,9 @@ def upgrade(): op.create_table('mc_option', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('label', sa.String(), nullable=True), - sa.Column('problem_id', sa.Integer(), nullable=False), sa.Column('feedback_id', sa.Integer(), nullable=False), sa.ForeignKeyConstraint(['feedback_id'], ['feedback_option.id'], ), sa.ForeignKeyConstraint(['id'], ['widget.id'], ), - sa.ForeignKeyConstraint(['problem_id'], ['problem.id'], ), sa.PrimaryKeyConstraint('id') ) # ### end Alembic commands ### diff --git a/zesje/api/exams.py b/zesje/api/exams.py index e2f6eea2d51845fd446d57d1bebb5f4910186e48..bccbeecac2dca2c5279d8c84ffe2c7ab1c07a2e3 100644 --- a/zesje/api/exams.py +++ b/zesje/api/exams.py @@ -157,15 +157,14 @@ class Exams(Resource): 'graded': any([sol.graded_by is not None for sol in prob.solutions]), 'mc_options': [ { - 'id': mc_option.id, - 'label': mc_option.label, - 'problem_id': mc_option.problem_id, - 'feedback_id': mc_option.feedback_id, - 'widget': { - 'name': mc_option.name, - 'x': mc_option.x, - 'y': mc_option.y - } + 'id': mc_option.id, + 'label': mc_option.label, + 'feedback_id': mc_option.feedback_id, + 'widget': { + 'name': mc_option.name, + 'x': mc_option.x, + 'y': mc_option.y + } } for mc_option in prob.mc_options ] } for prob in exam.problems # Sorted by prob.id diff --git a/zesje/api/mult_choice.py b/zesje/api/mult_choice.py index b2f2f4a6522e77fe07e037422b19ad2701069cc5..a8d0817c810f7ddb83076c875b4249cc9294ae8b 100644 --- a/zesje/api/mult_choice.py +++ b/zesje/api/mult_choice.py @@ -60,7 +60,6 @@ class MultipleChoice(Resource): y = args['y'] label = args['label'] problem_id = args['problem_id'] - feedback_id = args['feedback_id'] # TODO: Set type here or add to request? mc_type = 'mcq_widget' @@ -73,7 +72,7 @@ class MultipleChoice(Resource): # Insert new entry into the database mc_entry = MultipleChoiceOption() - set_mc_data(mc_entry, name, x, y, mc_type, feedback_id, label) + set_mc_data(mc_entry, name, x, y, mc_type, new_feedback_option.id, label) db.session.add(mc_entry) db.session.commit() @@ -88,7 +87,7 @@ class MultipleChoice(Resource): 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, name, x, y, mc_type, feedback_id, label) + set_mc_data(mc_entry, name, x, y, mc_type, label) db.session.commit() return dict(status=200, message=f'Multiple choice question with id {id} updated'), 200 diff --git a/zesje/database.py b/zesje/database.py index 157eb9b4f1e1935510b1ccd13631528c0c9298d9..16f005abc40a2464104e6a51f2654f4c0a4cbce9 100644 --- a/zesje/database.py +++ b/zesje/database.py @@ -103,7 +103,7 @@ class Problem(db.Model): @hybrid_property def mc_options(self): - return [mc_option for mc_option in self.feedback_options] + return [feedback_option.mc_option for feedback_option in self.feedback_options if feedback_option.mc_option] class FeedbackOption(db.Model): @@ -114,7 +114,7 @@ class FeedbackOption(db.Model): text = Column(Text, nullable=False) description = Column(Text, nullable=True) score = Column(Integer, nullable=True) - mc_option = db.relationship('MultipleChoiceOption', backref='feedback', lazy=True) + mc_option = db.relationship('MultipleChoiceOption', backref='feedback', uselist=False, lazy=True) # Table for many to many relationship of FeedbackOption and Solution