def update(connection: Connection, data: dict): """ Update a survey (title, questions). You can also add or modify questions here. Note that this creates a new survey (with new submissions, etc), copying everything from the old survey. The old survey's title will be changed to end with "(new version created on <time>)". :param connection: a SQLAlchemy Connection :param data: JSON containing the UUID of the survey and fields to update. """ survey_id = data['survey_id'] email = data['email'] existing_survey = survey_select(connection, survey_id, email=email) if 'survey_metadata' not in data: data['survey_metadata'] = existing_survey.survey_metadata update_time = datetime.datetime.now() with connection.begin(): new_title = '{} (new version created on {})'.format( existing_survey.survey_title, update_time.isoformat()) executable = update_record(survey_table, 'survey_id', survey_id, survey_title=new_title) exc = [('survey_title_survey_owner_key', SurveyAlreadyExistsError(new_title))] execute_with_exceptions(connection, executable, exc) new_survey_id = _create_survey(connection, data) return get_one(connection, new_survey_id, email=email)
def get(self, submission_id: str): submission = submission_api.get_one(self.db, submission_id, self.current_user)['result'] survey = survey_select(self.db, submission['survey_id'], email=self.current_user) self.render('view-submission.html', message=None, survey=survey, submission=submission)
def get(self, survey_id: str): stats = aggregation_api.get_question_stats survey_stats = get_stats(self.db, survey_id, email=self.current_user) question_stats = stats(self.db, survey_id, email=self.current_user) 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)
def testSurveySelect(self): user = connection.execute(auth_user_table.select().where( auth_user_table.c.email == 'test_email')).first() survey = connection.execute(survey_table.select().where( survey_table.c.survey_title == 'test_title')).first() self.assertEqual(survey.survey_title, survey_select(connection, survey.survey_id, auth_user_id=user.auth_user_id) .survey_title) self.assertEqual(survey.survey_title, survey_select(connection, survey.survey_id, email=user.email).survey_title) self.assertRaises(TypeError, survey_select, connection, survey.survey_id, auth_user_id=user.auth_user_id, email=user.email) self.assertRaises(TypeError, survey_select, connection, survey.survey_id)
def get_one(connection: Connection, survey_id: str, auth_user_id: str=None, email: str=None) -> dict: """ Get a JSON representation of a survey. You must supply either the auth_user_id or the email of the user. :param connection: a SQLAlchemy Connection :param survey_id: the UUID of the survey :param auth_user_id: the UUID of the user :param email: the e-mail address of the user :return: the JSON representation. """ survey = survey_select(connection, survey_id, auth_user_id=auth_user_id, email=email) return json_response(_to_json(connection, survey))
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)