def testGetAnswersForQuestion(self): survey_id = connection.execute(survey_table.select().where( survey_table.c.survey_title == 'test_title')).first().survey_id q_where = question_table.select().where( question_table.c.type_constraint_name == 'integer') question = connection.execute(q_where).first() question_id = question.question_id tcn = question.type_constraint_name seq = question.sequence_number mul = question.allow_multiple submission_exec = connection.execute( submission_insert(submitter='test_submitter', submitter_email='*****@*****.**', survey_id=survey_id)) submission_id = submission_exec.inserted_primary_key[0] connection.execute(answer_insert(answer=1, question_id=question_id, answer_metadata={}, submission_id=submission_id, survey_id=survey_id, type_constraint_name=tcn, is_type_exception=False, sequence_number=seq, allow_multiple=mul)) self.assertEqual( get_answers_for_question(connection, question_id).rowcount, 1)
def get(self, survey_id: str): location_questions = [] survey_stats = get_stats(self.db, survey_id, email=self.current_user) question_stats = get_question_stats( self.db, survey_id, email=self.current_user) for result in question_stats['result']: question = result['question'] question_type = question[6] if question_type == "location": question_id = question[0] answers = get_answers_for_question(self.db, question_id) map_data = list(self._get_map_data(answers)) location_questions.append({ "question_id": question_id, "map_data": map_data }) survey = survey_select(self.db, survey_id, email=self.current_user) self.render('view-survey-data.html', message=None, survey=survey, question_stats=question_stats, survey_stats=survey_stats, location_questions=location_questions)
def get(self, question_id: str): question = question_select(self.db, question_id) answers = get_answers_for_question(self.db, question_id) time_data, bar_data, map_data = None, None, None if question.type_constraint_name in {'integer', 'decimal'}: try: data = time_series(self.db, question_id, email=self.current_user) time_data = data['result'] except NoSubmissionsToQuestionError: pass if question.type_constraint_name in {'text', 'integer', 'decimal', 'date', 'time', 'location', 'multiple_choice'}: try: data = bar_graph(self.db, question_id, email=self.current_user) bar_data = data['result'] except NoSubmissionsToQuestionError: pass if question.type_constraint_name in {'location', 'facility'}: map_data = list(self._get_map_data(answers)) self.render('visualization.html', time_data=time_data, bar_data=bar_data, map_data=map_data)
def _create_questions(connection: Connection, questions: list, survey_id: str, submission_map: dict=None) -> Iterator: """ Create the questions of a survey. If this is an update to an existing survey, it will also copy over answers to the questions. :param connection: the SQLAlchemy Connection object for the transaction :param questions: a list of dictionaries, each containing the values associated with a question :param survey_id: the UUID of the survey :param submission_map: a dictionary mapping old submission_id to new :return: an iterable of the resultant question fields """ for number, question in enumerate(questions, start=1): values = question.copy() values['sequence_number'] = number values['survey_id'] = survey_id existing_q_id = values.pop('question_id', None) executable = question_insert(**values) tcn = values['type_constraint_name'] exceptions = [('question_type_constraint_name_fkey', TypeConstraintDoesNotExistError(tcn)), ('minimal_logic', MissingMinimalLogicError(values['logic']))] result = execute_with_exceptions(connection, executable, exceptions) result_ipk = result.inserted_primary_key q_id = result_ipk[0] choices = list(_create_choices(connection, values, q_id, submission_map=submission_map, existing_question_id=existing_q_id)) if existing_q_id is not None: question_fields = {'question_id': q_id, 'sequence_number': result_ipk[1], 'allow_multiple': result_ipk[2], 'type_constraint_name': result_ipk[3], 'survey_id': survey_id} for answer in get_answers_for_question(connection, existing_q_id): new_tcn = result_ipk[3] old_tcn = question_select(connection, existing_q_id).type_constraint_name if new_tcn != old_tcn: continue answer_values = question_fields.copy() answer_values['answer_metadata'] = answer.answer_metadata new_submission_id = submission_map[answer.submission_id] is_type_exception = _get_is_type_exception(answer) answer_values['is_type_exception'] = is_type_exception if is_type_exception: answer_values['answer'] = answer.answer_text else: answer_values['answer'] = answer['answer_' + new_tcn] allow_other = values['logic']['allow_other'] allow_dont_know = values['logic']['allow_dont_know'] with_type_exception = allow_other or allow_dont_know if new_tcn == 'multiple_choice' and not with_type_exception: continue answer_values['submission_id'] = new_submission_id connection.execute(answer_insert(**answer_values)) q_to_seq_number = values['question_to_sequence_number'] yield {'question_id': q_id, 'type_constraint_name': tcn, 'sequence_number': values['sequence_number'], 'allow_multiple': values['allow_multiple'], 'choice_ids': choices, 'question_to_sequence_number': q_to_seq_number}