From acc278de9c31074d43156fcf77be42378cf618b0 Mon Sep 17 00:00:00 2001 From: scmmishra Date: Mon, 18 Mar 2019 18:38:18 +0530 Subject: [PATCH] feat: Added validations for question doctype - A question must have more than two options - A question must have at least one correct option --- erpnext/education/doctype/question/question.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/erpnext/education/doctype/question/question.py b/erpnext/education/doctype/question/question.py index 28d3ecdf8c..57d8de4d65 100644 --- a/erpnext/education/doctype/question/question.py +++ b/erpnext/education/doctype/question/question.py @@ -4,10 +4,28 @@ from __future__ import unicode_literals import frappe +from frappe import _ from frappe.model.document import Document class Question(Document): + def validate(self): + self.check_at_least_one_option() + self.check_minimum_one_correct_answer() + + def check_at_least_one_option(self): + if len(self.get_all_children()) <= 1: + frappe.throw(_("A question must have more than one options")) + else: + pass + + def check_minimum_one_correct_answer(self): + correct_options = [question.is_correct for question in self.get_all_children()] + if bool(sum(correct_options)): + pass + else: + frappe.throw(_("A qustion must have at least one correct options")) + def get_answer(self): options = self.get_all_children() answers = [item.name for item in options if item.is_correct == True]