diff --git a/zesje/api/mult_choice.py b/zesje/api/mult_choice.py index e5321a0720f657ee683852498cddaf00267f3a98..8a1b2c9ffd72ba1445b18a029efd6c62503626b8 100644 --- a/zesje/api/mult_choice.py +++ b/zesje/api/mult_choice.py @@ -34,7 +34,6 @@ class MultipleChoice(Resource): put_parser.add_argument('x', type=int, required=True) put_parser.add_argument('y', type=int, required=True) put_parser.add_argument('label', type=str, required=False) - put_parser.add_argument('feedback_id', type=int, required=True) put_parser.add_argument('problem_id', type=int, required=True) # Used for FeedbackOption def put(self, id=None): @@ -124,7 +123,11 @@ class MultipleChoice(Resource): return json def delete(self, id): - """Deletes a multiple choice option from the database + """Deletes a multiple choice option from the database. + Also deletes the associated feedback option with this multiple choice option. + + An error will be thrown if the user tries to delete a feedback option + associated with a multiple choice option in a finalized exam. Parameters ---------- @@ -139,7 +142,20 @@ class MultipleChoice(Resource): if not mult_choice: return dict(status=404, message=f'Multiple choice question with id {id} does not exist.'), 404 + if not mult_choice.feedback: + return dict(status=404, message=f'Multiple choice question with id {id}' + + ' is not associated with a feedback option.'), 404 + + # Check if the exam is finalized, do not delete the multiple choice option otherwise + exam = mult_choice.feedback.problem.exam + + if exam.finalized: + return dict(status=401, message='Cannot delete feedback option' + + ' attached to a multiple choice option in a finalized exam.'), 401 + db.session.delete(mult_choice) + db.session.delete(mult_choice.feedback) db.session.commit() - return dict(status=200, message=f'Multiple choice question with id {id} deleted.'), 200 + return dict(status=200, message=f'Multiple choice question with id {id} deleted.' + + f'Feedback option with id {mult_choice.feedback_id} deleted.'), 200 diff --git a/zesje/database.py b/zesje/database.py index 16f005abc40a2464104e6a51f2654f4c0a4cbce9..5f4bc29a061dab409da4bcb6054f62958c95f06d 100644 --- a/zesje/database.py +++ b/zesje/database.py @@ -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', uselist=False, lazy=True) + mc_option = db.relationship('MultipleChoiceOption', backref='feedback', cascade='delete', uselist=False, lazy=True) # Table for many to many relationship of FeedbackOption and Solution