def get(self): args = self.req_parser.parse_args() response = response_repository.get_by_id_and_user_id( args['response_id'], g.current_user['id']) if not response: return RESPONSE_NOT_FOUND return reference_request_repository.get_all_by_response_id( response.id), 200
def get(self): LOGGER.debug('Received get request for reference-request') user = user_repository.get_by_id(g.current_user['id']) if not user: return USER_NOT_FOUND args = self.req_parser.parse_args() response = response_repository.get_by_id_and_user_id( args['response_id'], user.id) if not response: return RESPONSE_NOT_FOUND return reference_request_repository.get_all_by_response_id( response.id), 200
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
def test_repo_get_response(self): """Test for when retrieving a response from the repository and not via the api directly""" self._seed_data() response = response_repository.get_by_id_and_user_id( self.test_response.id, self.other_user_data['id']) self.assertEqual(response.application_form_id, self.test_form.id) self.assertEqual(response.user_id, self.other_user_data['id']) self.assertIsNone(response.submitted_timestamp) self.assertFalse(response.is_withdrawn) self.assertTrue(response.answers) same_response = response_repository.get_by_id(self.test_response.id) self.assertEqual(response, same_response) all_user_responses = response_repository.get_by_user_id( self.other_user_data['id']) self.assertEqual(len(all_user_responses), 2) self.assertEqual(response, all_user_responses[0])