def submit_audit(self): completed_audit = CompletedAuditBuilder() completed_audit.with_title(self.audit_title) # The object returned from the .kv is a TextField, with a member text completed_audit.with_auditor(self.auditor_name.text) completed_audit.with_datetime(datetime.utcnow()) for a in self.questions: if a.other_comments.text: temp_answer = Answer(text=a.question.text, severity=self.get_question_severity(a), response=a.response, comment=a.other_comments.text) else: temp_answer = Answer(text=a.question.text, severity=self.get_question_severity(a), response=a.response) completed_audit.with_answer(temp_answer) # Save audit completed_audit.build().save() # Update audit locked status AuditTemplate.objects().filter(title=self.audit_title).update( upsert=False, multi=True, locked=True)
def test_title_max_length(self): character_maximum = "" too_many_characters = "a" for _ in range(0, TITLE_MAX_LENGTH): character_maximum += "a" too_many_characters += "a" self.assertEqual(None, AuditTemplate(title=character_maximum, questions=[Question(text="Text")]).validate()) self.assertRaises(ValidationError, AuditTemplate(title=too_many_characters, questions=[Question(text="Text")]).validate)
def test_each_question_is_validated(self): self.assertEqual(None, AuditTemplate(title="Title", questions=[Question(text="Text")]).validate()) self.assertRaises(ValidationError, AuditTemplate( title="Title", questions=[ Question(text="Title1"), Question(text="Title2"), Question(), Question(text="Title2"), ] ).validate)
def is_filled_out(self): error_message = "" q_missing = False q_long = False if self.audit_title.text == "": error_message += "- Please enter a title for the audit.\n" elif len(self.audit_title.text) > TITLE_MAX_LENGTH: error_message += "- The audit title is too long.\n" elif AuditTemplate.objects(title=self.audit_title.text): error_message += "- That audit template title already exists.\n" if self.question_list == {}: error_message += "- An audit template must have one question.\n" else: for question in self.question_list.values(): if question.question_text.text == "" and not q_missing: error_message += "- Please enter question text for every question.\n" q_missing = True elif len(question.question_text.text ) > TEXT_MAX_LENGTH and not q_long: error_message += "- A question's text is too long.\n" q_long = True if q_long and q_missing: break return error_message
def populate_content(self): self.templates_list.clear_widgets() for audit in list(AuditTemplate.objects()): if audit.locked is True: self.templates_list.add_widget(InactiveAuditButton(text=audit.title)) else: self.templates_list.add_widget(ActiveAuditButton(text=audit.title))
def get_audit_templates(self, screen_manager): self.templates_list.clear_widgets() self.screen_manager = screen_manager titles = list( map(lambda template: template.title, AuditTemplate.objects().only('title'))) for title in titles: self.templates_list.add_widget( AuditButton(text=title, screen_manager=self.screen_manager))
def populate_page(self, audit_title): self.audit_title = audit_title completed_audit = AuditTemplate.objects().filter( title=self.audit_title).first() for question in completed_audit.questions: a_temp = AnswerModule() a_temp.question = question a_temp.question_text = question.text self.stack_list.add_widget(a_temp) self.questions.append(a_temp) self.stack_list.height += ANSWER_MODULE_DISPLACEMENT self.scrolling_panel.scroll_y = 1 self.auditor_name.text = ''
def test_storage_and_retrieval(self): title = "Test Storage And Retrieval" q0_text = "Question 0" q1_text = "Question 1" q2_text = "Question 2" question0 = Question(text=q0_text) question1 = Question(text=q1_text, yes=Severity.red()) question2 = Question(text=q2_text, no=Severity.red(), other=Severity.yellow()) AuditTemplateBuilder() \ .with_title(title) \ .with_question(question0) \ .with_question(question1) \ .with_question(question2) \ .build() \ .save() templates = AuditTemplate.objects(title=title) self.assertEqual(title, templates[0].title) self.assertEqual(1, len(templates)) self.assertEqual(3, len(templates[0].questions)) template = templates[0] self.assertEqual(q0_text, template.questions[0].text) self.assertEqual(Severity.green(), template.questions[0].yes) self.assertEqual(Severity.green(), template.questions[0].no) self.assertEqual(Severity.green(), template.questions[0].other) self.assertEqual(q1_text, template.questions[1].text) self.assertEqual(Severity.red(), template.questions[1].yes) self.assertEqual(Severity.green(), template.questions[1].no) self.assertEqual(Severity.green(), template.questions[1].other) self.assertEqual(q2_text, template.questions[2].text) self.assertEqual(Severity.green(), template.questions[2].yes) self.assertEqual(Severity.red(), template.questions[2].no) self.assertEqual(Severity.yellow(), template.questions[2].other)
def populate_audit(self, audit_name): target = audit_name try: template = AuditTemplate.objects().filter( title=target).first() # for now, while there can be duplicates except AttributeError: # TO DO - SOMETHING pass self.audit_title = template.title for question in template.questions: self.stack_list.height += 200 a_temp = AnswerModule() a_temp.question = question a_temp.question_text = question.text self.stack_list.add_widget(a_temp) self.questions.append(a_temp) # https://kivy.org/doc/stable/api-kivy.uix.scrollview.html Y scrolling value, between 0 and 1. If 0, # the content’s bottom side will touch the bottom side of the ScrollView. If 1, the content’s top side will # touch the top side. self.scrolling_panel.scroll_y = 1 # Reset the auditor name self.auditor_name.text = ''
def test_title_is_required(self): self.assertEqual(None, AuditTemplate(title="Title", questions=[Question(text="Text")]).validate()) self.assertRaises(ValidationError, AuditTemplate(questions=[Question(text="Text")]).validate)
def clear_all_audit_locks(callback): confirmation = TemplatesUnlockedPop() confirmation.yes.bind(on_release=lambda _: AuditTemplate.objects().update( upsert=False, multi=True, locked=False)) confirmation.yes.bind(on_release=lambda _: confirmation.dismiss()) confirmation.open()
def load_audit_templates(self): self.audit_templates = list(AuditTemplate.objects().only("title", "questions"))
def __init__(self, **kw): super().__init__(**kw) self.completed_audits = [] self.audit_templates = [] self.ca = CompletedAudit self.at = AuditTemplate()