Ejemplo n.º 1
0
Archivo: api.py Proyecto: mxaba/Baobab
    def post(self):
        args = self.post_req_parser.parse_args()
        response_id = args['response_id']
        title = args['title']
        firstname = args['firstname']
        lastname = args['lastname']
        relation = args['relation']
        email = args['email']
        user = user_repository.get_by_id(g.current_user['id'])

        if not user:
            return USER_NOT_FOUND

        event = event_repository.get_event_by_response_id(response_id)
        if not event:
            return EVENT_NOT_FOUND

        response = response_repository.get_by_id(response_id)
        if not response:
            return RESPONSE_NOT_FOUND

        reference_request = ReferenceRequest(response_id=response_id,
                                             title=title,
                                             firstname=firstname,
                                             lastname=lastname,
                                             relation=relation,
                                             email=email)
        reference_request_repository.create(reference_request)

        link = "{host}/reference/{token}".format(host=misc.get_baobab_host(),
                                                 token=reference_request.token)

        candidate, candidate_firstname, nominator = _get_candidate_nominator(
            response)
        if nominator is None:
            nomination_text = "has nominated themself"
        else:
            nomination_text = "has been nominated by {}".format(nominator)

        subject = 'REFERENCE REQUEST - {}'.format(event.name)
        body = REFERENCE_REQUEST_EMAIL_BODY.format(
            title=title,
            firstname=firstname,
            lastname=lastname,
            candidate=candidate,
            candidate_firstname=candidate_firstname,
            nomination_text=nomination_text,
            event_name=event.name,
            event_url=event.url,
            application_close_date=event.application_close,
            link=link)
        send_mail(recipient=email, subject=subject, body_text=body)

        reference_request.set_email_sent(datetime.now())
        reference_request_repository.add(reference_request)
        return reference_request, 201
Ejemplo n.º 2
0
    def post(self):
        args = self.post_req_parser.parse_args()
        response_id = args['response_id']
        title = args['title']
        firstname = args['firstname']
        lastname = args['lastname']
        relation = args['relation']
        email = args['email']
        user = user_repository.get_by_id(g.current_user['id'])

        if not user:
            return USER_NOT_FOUND

        event = event_repository.get_event_by_response_id(response_id)
        if not event:
            return EVENT_NOT_FOUND

        response = response_repository.get_by_id(response_id)
        if not response:
            return RESPONSE_NOT_FOUND

        reference_request = ReferenceRequest(response_id=response_id,
                                             title=title,
                                             firstname=firstname,
                                             lastname=lastname,
                                             relation=relation,
                                             email=email)
        reference_request_repository.create(reference_request)

        link = "{host}/reference/{token}".format(host=misc.get_baobab_host(),
                                                 token=reference_request.token)

        try:
            candidate, candidate_firstname, nominator = _get_candidate_nominator(
                response)
        except ValueError as e:
            LOGGER.error(e)
            return BAD_CONFIGURATION

        email_user('reference-request-self-nomination'
                   if nominator is None else 'reference-request',
                   template_parameters=dict(
                       candidate=candidate,
                       candidate_firstname=candidate_firstname,
                       nominator=nominator,
                       event_url=event.url,
                       application_close_date=event.application_close,
                       link=link),
                   event=event,
                   user=user)

        reference_request.set_email_sent(datetime.now())
        reference_request_repository.add(reference_request)
        return reference_request, 201
Ejemplo n.º 3
0
 def test_get_reference_request_by_id(self):
     self._seed_data()
     reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor',
                                      '*****@*****.**')
     reference_request_repository.create(reference_req)
     response = self.app.get('/api/v1/reference-request',
                             data={'id': 1},
                             headers=self.headers)
     data = json.loads(response.data)
     assert response.status_code == 200
     assert data['firstname'] == 'John'
Ejemplo n.º 4
0
 def test_get_reference_request_by_response_id(self):
     self._seed_data()
     reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor',
                                      '*****@*****.**')
     reference_req2 = ReferenceRequest(1, 'Mrs', 'John', 'Jones', 'Manager',
                                       '*****@*****.**')
     reference_request_repository.create(reference_req)
     reference_request_repository.create(reference_req2)
     response = self.app.get('/api/v1/reference-request/list',
                             data={'response_id': 1},
                             headers=self.first_headers)
     data = json.loads(response.data)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(data), 2)
Ejemplo n.º 5
0
 def test_get_reference_request_by_id(self):
     self._seed_data()
     reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor',
                                      '*****@*****.**')
     reference_request_repository.create(reference_req)
     response = self.app.get('/api/v1/reference-request',
                             data={'id': 1},
                             headers=self.first_headers)
     data = json.loads(response.data)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(data['title'], 'Mr')
     self.assertEqual(data['firstname'], 'John')
     self.assertEqual(data['lastname'], 'Snow')
     self.assertEqual(data['relation'], 'Supervisor')
     self.assertEqual(data['email'], '*****@*****.**')
     self.assertIsNone(data['email_sent'])
     self.assertFalse(data['reference_submitted'])
Ejemplo n.º 6
0
    def post(self):
        LOGGER.debug('Received post request for reference-request')
        args = self.post_req_parser.parse_args()
        response_id = args['response_id']
        title = args['title']
        firstname = args['firstname']
        lastname = args['lastname']
        relation = args['relation']
        email = args['email']
        user = user_repository.get_by_id(g.current_user['id'])

        if not user:
            return USER_NOT_FOUND

        response_event = event_repository.get_event_by_response_id(response_id)
        if not response_event or not response_event.Event:
            return EVENT_NOT_FOUND
        event = response_event.Event
        reference_request = ReferenceRequest(response_id=response_id,
                                             title=title,
                                             firstname=firstname,
                                             lastname=lastname,
                                             relation=relation,
                                             email=email)
        reference_request_repository.create(reference_request)

        link = "{host}/{key}/reference?token={token}".format(
            host=misc.get_baobab_host(),
            key=event.key,
            token=reference_request.token)

        subject = 'REFERENCE REQUEST - {}'.format(event.name)
        body = REFERENCE_REQUEST_EMAIL_BODY.format(
            title=title,
            firstname=firstname,
            lastname=lastname,
            event_description=event.description,
            link=link,
            candidate_firstname=user.firstname,
            candidate_lastname=user.lastname,
            application_close_date=event.application_close)
        send_mail(recipient=email, subject=subject, body_text=body)

        reference_request.set_email_sent(datetime.now())
        reference_request_repository.update(reference_request)
        return {}, 201
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
 def test_get_reference_request_detail_by_token_other_nomination(self):
     self._seed_data()
     reference_req = ReferenceRequest(2, 'Mr', 'John', 'Snow', 'Supervisor',
                                      '*****@*****.**')
     reference_request_repository.create(reference_req)
     response = self.app.get('/api/v1/reference-request/detail',
                             data={'token': reference_req.token},
                             headers=self.other_headers)
     data = json.loads(response.data)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(data['candidate'], 'Mx Skittles Cat')
     self.assertEqual(data['nominator'], 'Mrs User Lastname')
     self.assertEqual(data['relation'], reference_req.relation)
     self.assertEqual(data['name'], self.test_event_data['name'])
     self.assertEqual(data['description'],
                      self.test_event_data['description'])
     self.assertEqual(data['is_application_open'], True)
     self.assertEqual(data['email_from'],
                      self.test_event_data['email_from'])
     self.assertIsNone(data['reference_submitted_timestamp'])
Ejemplo n.º 9
0
    def put(self):
        args = self.put_req_parser.parse_args()
        token = args['token']
        uploaded_document = args['uploaded_document']
        reference_request = reference_request_repository.get_by_token(token)
        event = event_repository.get_event_by_response_id(
            reference_request.response_id)
        reference = reference_request_repository.get_reference_by_reference_request_id(
            reference_request.id)

        if not reference_request:
            return REFERENCE_REQUEST_NOT_FOUND

        if not event.is_application_open:
            return APPLICATIONS_CLOSED

        reference.uploaded_document = uploaded_document

        reference_request_repository.commit()

        return {}, 200
Ejemplo n.º 10
0
    def post(self):
        args = self.post_req_parser.parse_args()
        token = args['token']
        uploaded_document = args['uploaded_document']
        reference_request = reference_request_repository.get_by_token(token)
        event = event_repository.get_event_by_response_id(
            reference_request.response_id)

        if not reference_request:
            return REFRERENCE_REQUEST_WITH_TOKEN_NOT_FOUND

        if reference_request.has_reference:
            return DUPLICATE_REFERENCE_SUBMISSION

        if not event.is_application_open:
            return APPLICATIONS_CLOSED

        reference = Reference(reference_request_id=reference_request.id,
                              uploaded_document=uploaded_document)
        reference_request_repository.add(reference)
        return {}, 201
Ejemplo n.º 11
0
    def get(self):
        args = self.req_parser.parse_args()
        event_id = args['event_id']

        review_form = review_repository.get_review_form(event_id)
        if review_form is None:
            return EVENT_NOT_FOUND

        reviews_remaining_count = review_repository.get_remaining_reviews_count(
            g.current_user['id'], review_form.application_form_id)
        skip = self.sanitise_skip(args['skip'], reviews_remaining_count)

        response = review_repository.get_response_to_review(
            skip, g.current_user['id'], review_form.application_form_id)

        references = []
        if response is not None:
            reference_requests = reference_repository.get_all_by_response_id(
                response.id)

            for r in reference_requests:
                reference = reference_repository.get_reference_by_reference_request_id(
                    r.id)
                if reference is not None:
                    references.append(
                        ReviewResponseReference(r.title, r.firstname,
                                                r.lastname, r.relation,
                                                reference.uploaded_document))

        review_response = None if response is None else review_repository.get_review_response(
            review_form.id, response.id, g.current_user['id'])
        references = None if len(references) == 0 else references

        return ReviewResponseUser(review_form,
                                  response,
                                  reviews_remaining_count,
                                  args['language'],
                                  reference_responses=references,
                                  review_response=review_response)
Ejemplo n.º 12
0
    def get(self):
        args = self.post_req_parser.parse_args()
        token = args['token']

        reference_request = reference_request_repository.get_by_token(
            token)  # type: ReferenceRequest
        if not reference_request:
            return REFRERENCE_REQUEST_WITH_TOKEN_NOT_FOUND

        response_id = reference_request.response_id
        response = response_repository.get_by_id(response_id)  # type: Response
        if not response:
            return RESPONSE_NOT_FOUND

        event = event_repository.get_event_by_response_id(
            response_id)  # type: Event

        if not event:
            return EVENT_NOT_FOUND

        reference = reference_repository.get_by_reference_request_id(
            reference_request.id)
        app_form = event.get_application_form()  # type: ApplicationForm

        # Determine whether the response is a nomination
        try:
            candidate, _, nominator = _get_candidate_nominator(response)
        except ValueError as e:
            LOGGER.error(e)
            return BAD_CONFIGURATION

        return_object = {
            'candidate':
            candidate,
            'nominator':
            nominator,
            'relation':
            reference_request.relation,
            'name':
            event.get_name('en'),
            'description':
            event.get_description('en'),
            'is_application_open':
            event.is_application_open,
            'email_from':
            event.email_from,
            'reference_submitted_timestamp':
            reference.timestamp if reference is not None else None
        }

        return return_object, 200
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
    def test_reference_api(self):
        self._seed_data()
        reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor',
                                         '*****@*****.**')
        reference_request_repository.create(reference_req)
        REFERENCE_DETAIL = {
            'token': reference_req.token,
            'uploaded_document': 'DOCT-UPLOAD-78999',
        }
        response = self.app.post('/api/v1/reference',
                                 data=REFERENCE_DETAIL,
                                 headers=self.first_headers)
        self.assertEqual(response.status_code, 201)

        response = self.app.get('/api/v1/reference',
                                data={'response_id': 1},
                                headers=self.first_headers)
        LOGGER.debug(response.data)
        data = json.loads(response.data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(data), 1)
        reference_request = reference_request_repository.get_by_id(1)
        self.assertEqual(reference_request.has_reference, True)
Ejemplo n.º 15
0
    def test_reference_application_closed(self):

        self.add_organisation('Deep Learning Indaba', 'blah.png',
                              'blah_big.png')
        other_user_data = self.add_user('*****@*****.**')

        test_event = self.add_event()
        test_event.add_event_role('admin', 1)
        test_event.set_application_close(datetime.now())

        self.add_to_db(test_event)
        self.test_form = self.create_application_form(test_event.id, True,
                                                      False)
        self.add_to_db(self.test_form)

        self.test_response = Response(self.test_form.id, other_user_data.id)
        self.add_to_db(self.test_response)
        self.headers = self.get_auth_header_for("*****@*****.**")

        db.session.flush()

        reference_req = ReferenceRequest(1, 'Mr', 'John', 'Snow', 'Supervisor',
                                         '*****@*****.**')
        reference_request_repository.create(reference_req)
        REFERENCE_DETAIL = {
            'token': reference_req.token,
            'uploaded_document': 'DOCT-UPLOAD-78999',
        }
        response = self.app.post('/api/v1/reference',
                                 data=REFERENCE_DETAIL,
                                 headers=self.headers)
        self.assertEqual(response.status_code, 403)

        response = self.app.put('/api/v1/reference',
                                data=REFERENCE_DETAIL,
                                headers=self.headers)
        self.assertEqual(response.status_code, 403)
Ejemplo n.º 16
0
 def test_create_reference_request(self):
     self._seed_data()
     REFERENCE_REQUEST_DETAIL = {
         'response_id': 1,
         'title': 'Mr',
         'firstname': 'John',
         'lastname': 'Snow',
         'relation': 'Suppervisor',
         'email': '*****@*****.**'
     }
     response = self.app.post('/api/v1/reference-request',
                              data=REFERENCE_REQUEST_DETAIL,
                              headers=self.first_headers)
     resference_requests = reference_request_repository.get_all()
     self.assertEqual(response.status_code, 201)
     self.assertEqual(len(resference_requests), 1)
Ejemplo n.º 17
0
    def get(self):
        args = self.get_req_parser.parse_args()
        user = user_repository.get_by_id(g.current_user['id'])
        response = response_repository.get_by_id(args['response_id'])
        if not response:
            return RESPONSE_NOT_FOUND

        event = event_repository.get_event_by_response_id(response.id)

        if not user.is_event_admin(event.id):
            return FORBIDDEN
        reference_responses = reference_request_repository.get_references_by_response_id(
            response.id)
        return [
            reference_response.Reference
            for reference_response in reference_responses
        ], 200
Ejemplo n.º 18
0
 def get(self):
     args = self.get_req_parser.parse_args()
     return reference_request_repository.get_by_id(args['id']), 200
Ejemplo n.º 19
0
 def get(self):
     LOGGER.debug('Received get request for reference-request')
     args = self.get_req_parser.parse_args()
     return reference_request_repository.get_by_id(args['id']), 200