def delete(self): args = self.del_req_parser.parse_args() current_user_id = g.current_user['id'] response = response_repository.get_by_id(args['id']) if not response: return errors.RESPONSE_NOT_FOUND if response.user_id != current_user_id: return errors.UNAUTHORIZED response.withdraw() response_repository.save(response) try: user = user_repository.get_by_id(current_user_id) event = response.application_form.event organisation = event.organisation emailer.email_user( 'withdrawal', template_parameters=dict( organisation_name=organisation.name ), event=event, user=user ) except: LOGGER.error('Failed to send withdrawal confirmation email for response with ID : {id}, but the response was withdrawn succesfully'.format(id=args['id'])) return {}, 204
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 put(self): args = self.put_req_parser.parse_args() user_id = g.current_user['id'] is_submitted = args['is_submitted'] language = args['language'] response = response_repository.get_by_id(args['id']) if not response: return errors.RESPONSE_NOT_FOUND if response.user_id != user_id: return errors.UNAUTHORIZED if response.application_form_id != args['application_form_id']: return errors.UPDATE_CONFLICT response.is_submitted = is_submitted response.language = language if is_submitted: response.submit() response_repository.save(response) answers = [] for answer_args in args['answers']: answer = response_repository.get_answer_by_question_id_and_response_id( answer_args['question_id'], response.id) if answer: answer.update(answer_args['value']) else: 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, 200