def seed_static_data(self): self.add_organisation('IndabaX') self.start_time = datetime.now() + timedelta(days=30) self.end_time = datetime.now() + timedelta(days=60) test_event = Event('Test Event', 'Event Description', self.start_time, self.end_time, 'FREAD', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) db.session.add(test_event) db.session.commit() test_form = ApplicationForm(test_event.id, True, self.end_time) db.session.add(test_form) db.session.commit() test_section = Section(test_form.id, 'Test Section', 'Test Description', 1) db.session.add(test_section) db.session.commit() test_question = Question(application_form_id=test_form.id, section_id=test_section.id, headline='Test Question Headline', placeholder='Test question placeholder', order=1, questionType='multi-choice', validation_regex=None, is_required=True, description='Test Question Description', options=None) db.session.add(test_question) db.session.flush()
def _seed_data(self): self.add_organisation('IndabaX') self.start_time = datetime.now() + timedelta(days=30) self.end_time = datetime.now() + timedelta(days=60) self.test_event = self.add_event('Test Event', 'Event Description', self.start_time, self.end_time) self.add_to_db(self.test_event) self.test_form = self.create_application_form(self.test_event.id, True, False) self.add_to_db(self.test_form) self.test_section = Section(self.test_form.id, 'Test Section', 'Test Description', 1) self.add_to_db(self.test_section) self.test_question = Question(application_form_id=self.test_form.id, section_id=self.test_section.id, headline='Test Question Headline', placeholder='Test question placeholder', order=1, questionType='multi-choice', validation_regex=None, is_required=True, description='Test Question Description', options=None) self.add_to_db(self.test_question) # Section 2 self.test_section2 = Section(self.test_form.id, 'Test Section 2', 'Test Description 2', 2) self.add_to_db(self.test_section2) self.test_question2 = Question( application_form_id=self.test_form.id, section_id=self.test_section2.id, headline='Test Question 2 Headline', placeholder='Test question 2 placeholder', order=1, questionType='text', validation_regex=None, is_required=True, description='Test Question 2 Description') self.add_to_db(self.test_question2)
def _seed_data(self): # Add a user test_country = Country('Indaba Land') _add_object_to_db(test_country) test_category = UserCategory('Category1') _add_object_to_db(test_category) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) # Add application form data self.test_event = Event('Test Event', 'Event Description', date( 2019, 2, 24), date(2019, 3, 24)) _add_object_to_db(self.test_event) self.test_form = ApplicationForm( self.test_event.id, True, date(2019, 3, 24)) _add_object_to_db(self.test_form) test_section = Section( self.test_form.id, 'Test Section', 'Test Description', 1) _add_object_to_db(test_section) self.test_question = Question(self.test_form.id, test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) _add_object_to_db(self.test_question) self.test_question2 = Question( self.test_form.id, test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) _add_object_to_db(self.test_question2) self.test_response = Response( self.test_form.id, self.other_user_data['id']) _add_object_to_db(self.test_response) self.test_answer1 = Answer( self.test_response.id, self.test_question.id, 'My Answer') _add_object_to_db(self.test_answer1) db.session.flush()
def _seed_data(self): organisation = self.add_organisation( 'Deep Learning Indaba', 'Baobab', 'blah.png', 'blah_big.png', 'deeplearningindba', 'https://www.deeplearningindaba.com', '*****@*****.**') email_templates = [ EmailTemplate('withdrawal', None, ''), EmailTemplate('confirmation-response', None, '{question_answer_summary}') ] db.session.add_all(email_templates) db.session.commit() # Add a user test_country = Country('Indaba Land') _add_object_to_db(test_country) test_category = UserCategory('Category1') _add_object_to_db(test_category) other_user_data = self.user_data_dict.copy() other_user_data['email'] = '*****@*****.**' response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) # Add application form data self.test_event = Event('Test Event', 'Event Description', date(2019, 2, 24), date(2019, 3, 24), 'NAGSOLVER', 1, '*****@*****.**', 'indaba.deeplearning', datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now(), datetime.now()) _add_object_to_db(self.test_event) self.test_form = ApplicationForm(self.test_event.id, True, date(2019, 3, 24)) _add_object_to_db(self.test_form) test_section = Section(self.test_form.id, 'Test Section', 'Test Description', 1) _add_object_to_db(test_section) self.test_question = Question(self.test_form.id, test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) _add_object_to_db(self.test_question) self.test_question2 = Question(self.test_form.id, test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) _add_object_to_db(self.test_question2) self.test_response = Response(self.test_form.id, self.other_user_data['id']) _add_object_to_db(self.test_response) self.test_answer1 = Answer(self.test_response.id, self.test_question.id, 'My Answer') _add_object_to_db(self.test_answer1) db.session.flush()
def add_section(self, application_form_id, order=1): section = Section(application_form_id, order) db.session.add(section) db.session.commit() return section
def put(self, event_id): req_parser = reqparse.RequestParser() req_parser.add_argument('is_open', type=bool, required=True) req_parser.add_argument('nominations', type=bool, required=True) req_parser.add_argument('id', type=int, required=True) req_parser.add_argument('sections', type=dict, required=True, action='append') args = req_parser.parse_args() user_id = g.current_user['id'] application_form_id = args['id'] app_form = application_form_repository.get_by_id( application_form_id) # type: ApplicationForm if not app_form: return FORM_NOT_FOUND_BY_ID if event_id != app_form.event_id: return UPDATE_CONFLICT app_form.is_open = args['is_open'] app_form.nominations = args['nominations'] current_sections = app_form.sections incoming_sections = args['sections'] # Delete questions in the application form that no longer exist all_question_ids = [ q['id'] for s in incoming_sections for q in s['questions'] if 'id' in q ] print("all_question_ids:", all_question_ids) for question in app_form.questions: if question.id not in all_question_ids: print("DELETING QUESTION ID ", question.id) application_form_repository.delete_question(question) all_section_ids = [s['id'] for s in incoming_sections if 'id' in s] print("all_section_ids:", all_section_ids) for section in app_form.sections: if section.id not in all_section_ids: print("DELETING SECTION ID ", section.id) application_form_repository.delete_section(section) # Keep track of which objects match with which incoming data for populating dependencies later. section_data_map = {} question_data_map = {} question_id_map = {} for section_data in incoming_sections: if 'id' in section_data: # If ID is populated, then update the existing section section = next( (s for s in current_sections if s.id == section_data['id']), None) # type: Section if not section: return SECTION_NOT_FOUND current_translations = section.section_translations # type: Sequence[SectionTranslation] for current_translation in current_translations: current_translation.description = section_data[ 'description'][current_translation.language] current_translation.name = section_data['name'][ current_translation.language] current_translation.show_for_values = section_data[ 'show_for_values'][current_translation.language] section.key = section_data['key'] section.order = section_data['order'] db.session.commit() else: # if not populated, then add new section section = Section(app_form.id, section_data['order'], key=section_data['key']) application_form_repository.add(section) languages = section_data['name'].keys() for language in languages: section_translation = SectionTranslation( section.id, language, section_data['name'][language], section_data['description'][language], section_data['show_for_values'][language]) application_form_repository.add(section_translation) section_data_map[section] = section_data try: question_map, question_ids = _process_questions( section_data['questions'], app_form, section.id) question_data_map.update(question_map) question_id_map.update(question_ids) except ValueError: return QUESTION_NOT_FOUND db.session.commit() # Now that all the questions have been created, we can populate the dependencies for section, section_data in section_data_map.items(): if section_data['depends_on_question_id']: section.depends_on_question_id = question_id_map[ section_data['depends_on_question_id']] for question, question_data in question_data_map.items(): if question_data['depends_on_question_id']: question.depends_on_question_id = question_id_map[ question_data['depends_on_question_id']] app_form = application_form_repository.get_by_id(app_form.id) return app_form, 200
def post(self, event_id): req_parser = reqparse.RequestParser() req_parser.add_argument('is_open', type=bool, required=True) req_parser.add_argument('nominations', type=bool, required=True) req_parser.add_argument('sections', type=dict, required=True, action='append') args = req_parser.parse_args() app_form = application_form_repository.get_by_event_id(event_id) if app_form: return APPLICATION_FORM_EXISTS is_open = args['is_open'] nominations = args['nominations'] app_form = ApplicationForm(event_id, is_open, nominations) application_form_repository.add(app_form) sections_data = args['sections'] # Keep track of which objects match with which incoming data for populating dependencies later. section_data_map = {} question_data_map = {} question_id_map = {} for section_data in sections_data: section = Section(app_form.id, section_data['order'], key=section_data['key']) application_form_repository.add(section) section_data_map[section] = section_data languages = section_data['name'].keys() for language in languages: section_translation = SectionTranslation( section.id, language, section_data['name'][language], section_data['description'][language], section_data['show_for_values'][language]) application_form_repository.add(section_translation) for question_data in section_data['questions']: # application_form_id, section_id, order, questionType, is_required=True question = Question(app_form.id, section.id, question_data['order'], question_data['type'], question_data['is_required']) application_form_repository.add(question) question_data_map[question] = question_data if "surrogate_id" in question_data: question_id_map[ question_data["surrogate_id"]] = question.id for language in languages: question_translation = QuestionTranslation( question_id=question.id, language=language, headline=question_data['headline'][language], description=question_data['description'][language], placeholder=question_data['placeholder'][language], validation_regex=question_data['validation_regex'] [language], validation_text=question_data['validation_text'] [language], options=question_data['options'][language], show_for_values=question_data['show_for_values'] [language]) application_form_repository.add(question_translation) # Now that all the questions have been created, we can populate the dependencies for section, section_data in section_data_map.items(): if section_data['depends_on_question_id']: section.depends_on_question_id = question_id_map[ section_data['depends_on_question_id']] for question, question_data in question_data_map.items(): if question_data['depends_on_question_id']: question.depends_on_question_id = question_id_map[ question_data['depends_on_question_id']] app_form = application_form_repository.get_by_id(app_form.id) return app_form, 201
def _seed_data(self): """Create dummy data for testing""" organisation = self.add_organisation( 'Deep Learning Indaba', 'Baobab', 'blah.png', 'blah_big.png', 'deeplearningindba', 'https://www.deeplearningindaba.com', '*****@*****.**') email_templates = [ EmailTemplate('withdrawal', None, ''), EmailTemplate('confirmation-response', None, '{question_answer_summary}') ] db.session.add_all(email_templates) db.session.commit() # Add country test_country = Country('Indaba Land') self.add_to_db(test_country) # Add category test_category = UserCategory('Category1') self.add_to_db(test_category) # Add users to database other_user_data = self.user_data_dict.copy() other_user_data['email'] = self.other_user_email response = self.app.post('/api/v1/user', data=other_user_data) self.other_user_data = json.loads(response.data) response = self.app.post('/api/v1/user', data=self.user_data_dict) self.user_data = json.loads(response.data) self.add_n_users(self.num_dummy_users) # Add application form data self.test_event = self.add_event('Test Event', 'Event Description', date(2019, 2, 24), date(2019, 3, 24), 'NAGSOLVER') self.test_form = self.create_application_form(self.test_event.id, True) self.test_section = Section(self.test_form.id, 'Test Section', 'Test Description', 1) self.test_section.key = 'test_section' self.add_to_db(self.test_section) self.test_question = Question(self.test_form.id, self.test_section.id, 'Test Question Description', 'Test question placeholder', 1, 'Test Type', None) self.add_to_db(self.test_question) self.test_question2 = Question(self.test_form.id, self.test_section.id, 'Test Question 2', 'Enter something', 2, 'short-text', None) self.add_to_db(self.test_question2) # responses self.test_response = Response(self.test_form.id, self.other_user_data['id']) self.add_to_db(self.test_response) self.test_answer1 = Answer(self.test_response.id, self.test_question.id, 'My Answer') self.add_to_db(self.test_answer1) self.responses = [] for user in self.test_users: response = Response(self.test_form.id, user.id) self.add_to_db(response) answer1 = Answer( response.id, self.test_question.id, "{}'s Answer for question 1".format(user.firstname)) answer2 = Answer( response.id, self.test_question2.id, "{}'s Answer for question 2".format(user.firstname)) self.add_to_db(answer1) self.add_to_db(answer2) self.responses.append(response) # add nomination application form self.test_nomination_form = self.create_application_form( self.test_event.id, True, True) self.test_nomination_response = Response(self.test_nomination_form.id, self.other_user_data['id']) self.add_to_db(self.test_nomination_response) db.session.flush()
def _seed_data(self): self.add_organisation('Deep Learning Indaba', 'blah.png', 'blah_big.png') self.first_user_data = self.add_user('*****@*****.**', 'First', 'User', 'Mx') self.other_user_data = self.add_user('*****@*****.**') test_event = self.add_event() test_event.add_event_role('admin', 1) self.test_event_data = copy.deepcopy(test_event.__dict__) self.add_to_db(test_event) nomination_event = self.add_event(key="AWARD_NOMINATIONS_ONLY") nomination_event.add_event_role('admin', 1) self.test_nomination_event_data = copy.deepcopy( nomination_event.__dict__) self.add_to_db(nomination_event) self.test_form = self.create_application_form(test_event.id, True, False) self.add_to_db(self.test_form) self.test_nomination_form = self.create_application_form( nomination_event.id, True, True) sections = [ Section(test_event.id, 'Nomination Capacity', 'Nomination Details', 1), Section(nomination_event.id, 'Nominee Information', 'Details of person being nominated', 1) ] sections[1].key = 'nominee_section' db.session.add_all(sections) db.session.commit() questions = [ Question(test_event.id, sections[0].id, 'Nomination Capacity', 'Enter 50 to 150 words', 1, 'long_text', ''), Question(test_event.id, sections[0].id, 'some details', 'Enter 50 to 150 words', 2, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'title', 'Enter 50 to 150 words', 1, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'firstname', 'Enter 50 to 150 words', 2, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'lastname', 'Enter 50 to 150 words', 3, 'long_text', ''), Question(nomination_event.id, sections[1].id, 'email', 'Enter 50 to 150 words', 4, 'long_text', ''), ] questions[0].key = 'nominating_capacity' questions[2].key = 'nomination_title' questions[3].key = 'nomination_firstname' questions[4].key = 'nomination_lastname' questions[5].key = 'nomination_email' db.session.add_all(questions) db.session.commit() self.test_response1 = Response( # Self nomination self.test_form.id, self.first_user_data.id) self.add_to_db(self.test_response1) answers = [ Answer(self.test_response1.id, questions[0].id, 'self'), Answer(self.test_response1.id, questions[1].id, 'Blah') ] db.session.add_all(answers) db.session.commit() self.test_response2 = Response( # Nominating other self.test_form.id, self.other_user_data.id) self.add_to_db(self.test_response2) answers = [ Answer(self.test_response2.id, questions[0].id, 'other'), Answer(self.test_response2.id, questions[1].id, 'Blah'), Answer(self.test_response2.id, questions[2].id, 'Mx'), Answer(self.test_response2.id, questions[3].id, 'Skittles'), Answer(self.test_response2.id, questions[4].id, 'Cat'), Answer(self.test_response2.id, questions[5].id, '*****@*****.**'), ] db.session.add_all(answers) db.session.commit() self.first_headers = self.get_auth_header_for("*****@*****.**") self.other_headers = self.get_auth_header_for("*****@*****.**") db.session.flush()