def post(self): args = self.post_req_parser.parse_args() user_id = g.current_user['id'] is_submitted = args['is_submitted'] application_form_id = args['application_form_id'] language = args['language'] if len(language) != 2: language = 'en' # Fallback to English if language doesn't look like an ISO 639-1 code application_form = application_form_repository.get_by_id( application_form_id) if application_form is None: return errors.FORM_NOT_FOUND_BY_ID user = user_repository.get_by_id(user_id) responses = response_repository.get_all_for_user_application( user_id, application_form_id) if not application_form.nominations and len(responses) > 0: return errors.RESPONSE_ALREADY_SUBMITTED response = Response(application_form_id, user_id, language) if is_submitted: response.submit() response_repository.save(response) answers = [] for answer_args in args['answers']: answer = Answer(response.id, answer_args['question_id'], answer_args['value']) answers.append(answer) response_repository.save_answers(answers) try: if response.is_submitted: LOGGER.info( 'Sending confirmation email for response with ID : {id}'. format(id=response.id)) user = user_repository.get_by_id(user_id) response = response_repository.get_by_id_and_user_id( response.id, user_id) self.send_confirmation(user, response) except: LOGGER.warn( 'Failed to send confirmation email for response with ID : {id}, but the response was submitted succesfully' .format(id=response.id)) finally: return response, 201
def get(self): args = self.get_req_parser.parse_args() event_id = args['event_id'] current_user_id = g.current_user['id'] event = event_repository.get_by_id(event_id) if not event: return errors.EVENT_NOT_FOUND if not event.has_application_form(): return errors.FORM_NOT_FOUND form = event.get_application_form() responses = response_repository.get_all_for_user_application(current_user_id, form.id) return responses