def post(self): args = self.req_parser.parse_args() user_id = args['user_id'] event_id = args['event_id'] email_template = args['email_template'] offer_date = datetime.strptime((args['offer_date']), '%Y-%m-%dT%H:%M:%S.%fZ') expiry_date = datetime.strptime((args['expiry_date']), '%Y-%m-%dT%H:%M:%S.%fZ') payment_required = args['payment_required'] travel_award = args['travel_award'] accommodation_award = args['accommodation_award'] user = db.session.query(AppUser).filter(AppUser.id == user_id).first() event_name = db.session.query(Event).filter( Event.id == event_id).first().name existing_offer = db.session.query(Offer).filter( Offer.user_id == user_id, Offer.event_id == event_id).first() if existing_offer: return errors.DUPLICATE_OFFER existing_outcome = outcome_repository.get_latest_by_user_for_event( user_id, event_id) if existing_outcome: if existing_outcome.status == Status.REJECTED: return errors.CANDIDATE_REJECTED existing_outcome.reset_latest() new_outcome = Outcome(event_id, user_id, Status.ACCEPTED, g.current_user['id']) outcome_repository.add(new_outcome) offer_entity = Offer(user_id=user_id, event_id=event_id, offer_date=offer_date, expiry_date=expiry_date, payment_required=payment_required, travel_award=travel_award, accommodation_award=accommodation_award) db.session.add(offer_entity) db.session.commit() if user.email: email_body_template = email_template or OFFER_EMAIL_BODY send_mail( recipient=user.email, subject='{} Application Status Update'.format(event_name), body_text=email_body_template.format( user_title=user.user_title, first_name=user.firstname, last_name=user.lastname, event_name=event_name, host=misc.get_baobab_host(), expiry_date=offer_entity.expiry_date.strftime("%Y-%m-%d"))) LOGGER.debug("Sent an offer email to {}".format(user.email)) return offer_info(offer_entity), 201
def post(self): args = self.req_parser.parse_args() user_id = args['user_id'] event_id = args['event_id'] email_template = args['email_template'] offer_date = datetime.strptime((args['offer_date']), '%Y-%m-%dT%H:%M:%S.%fZ') expiry_date = datetime.strptime((args['expiry_date']), '%Y-%m-%dT%H:%M:%S.%fZ') payment_required = args['payment_required'] travel_award = args['travel_award'] accommodation_award = args['accommodation_award'] user = db.session.query(AppUser).filter(AppUser.id == user_id).first() event = db.session.query(Event).filter(Event.id == event_id).first() event_name = event.get_name('en') event_email_from = event.email_from existing_offer = db.session.query(Offer).filter(Offer.user_id == user_id, Offer.event_id == event_id).first() if existing_offer: return errors.DUPLICATE_OFFER existing_outcome = outcome_repository.get_latest_by_user_for_event(user_id, event_id) if existing_outcome: if existing_outcome.status == Status.REJECTED: return errors.CANDIDATE_REJECTED existing_outcome.reset_latest() new_outcome = Outcome( event_id, user_id, Status.ACCEPTED, g.current_user['id'] ) outcome_repository.add(new_outcome) offer_entity = Offer( user_id=user_id, event_id=event_id, offer_date=offer_date, expiry_date=expiry_date, payment_required=payment_required, travel_award=travel_award, accommodation_award=accommodation_award ) db.session.add(offer_entity) db.session.commit() email_user( 'offer', template_parameters=dict( host=misc.get_baobab_host(), expiry_date=offer_entity.expiry_date.strftime("%Y-%m-%d"), event_email_from=event_email_from ), event=event, user=user) return offer_info(offer_entity), 201
def test_repository_get_latest_by_user_for_event(self): """Test that repository method gets the correct latest outcome for a user.""" self.seed_static_data() result = outcome_repository.get_latest_by_user_for_event( self.test_user1.id, self.event1.id) self.assertEqual(result.id, self.event1_user1_outcome2.id) self.assertEqual(result.status, Status.ACCEPTED) result = outcome_repository.get_latest_by_user_for_event( self.test_user2.id, self.event1.id) self.assertEqual(result.id, self.event1_user2_outcome.id) self.assertEqual(result.status, Status.REJECTED)
def post(self, event_id): req_parser = reqparse.RequestParser() req_parser.add_argument('user_id', type=int, required=True) req_parser.add_argument('outcome', type=str, required=True) args = req_parser.parse_args() event = event_repository.get_by_id(event_id) if not event: return errors.EVENT_NOT_FOUND user = user_repository.get_by_id(args['user_id']) if not user: return errors.USER_NOT_FOUND try: status = Status[args['outcome']] except KeyError: return errors.OUTCOME_STATUS_NOT_VALID try: # Set existing outcomes to no longer be the latest outcome existing_outcomes = outcome_repository.get_all_by_user_for_event(args['user_id'], event_id) for existing_outcome in existing_outcomes: existing_outcome.reset_latest() # Add new outcome outcome = Outcome( event_id, args['user_id'], status, g.current_user['id']) outcome_repository.add(outcome) db.session.commit() if status != Status.ACCEPTED: # Email will be sent with offer for accepted candidates email_user( 'outcome-rejected' if status == Status.REJECTED else 'outcome-waitlist', template_parameters=dict( host=misc.get_baobab_host() ), event=event, user=user, ) return outcome, 201 except SQLAlchemyError as e: LOGGER.error("Database error encountered: {}".format(e)) return errors.DB_NOT_AVAILABLE except: LOGGER.error("Encountered unknown error: {}".format(traceback.format_exc())) return errors.DB_NOT_AVAILABLE
def test_get_latest_for_event(self): """Test that repository method gets the latest outcomes for an event.""" self.seed_static_data() result = outcome_repository.get_latest_for_event(self.event1.id) self.assertEqual(len(result), 2) self.assertItemsEqual( [o.id for o in result], [self.event1_user1_outcome2.id, self.event1_user2_outcome.id])
def test_get_all_by_user_for_event(self): """Test that repository method gets all outcomes for a user.""" self.seed_static_data() result = outcome_repository.get_all_by_user_for_event( self.test_user1.id, self.event1.id) self.assertEqual(len(result), 2) self.assertItemsEqual( [o.id for o in result], [self.event1_user1_outcome1.id, self.event1_user1_outcome2.id])
def get(self, event_id): event = event_repository.get_by_id(event_id) if not event: return errors.EVENT_NOT_FOUND try: outcomes = outcome_repository.get_latest_for_event(event_id) return outcomes except SQLAlchemyError as e: LOGGER.error("Database error encountered: {}".format(e)) return errors.DB_NOT_AVAILABLE except: LOGGER.error("Encountered unknown error: {}".format(traceback.format_exc())) return errors.DB_NOT_AVAILABLE
def test_create_offer(self): self.seed_static_data(add_offer=False) response = self.app.post('/api/v1/offer', data=OFFER_DATA, headers=self.adminHeaders) data = json.loads(response.data) self.assertEqual(response.status_code, 201) self.assertTrue(data['payment_required']) self.assertTrue(data['travel_award']) self.assertTrue(data['accommodation_award']) outcome = outcome_repository.get_latest_by_user_for_event( OFFER_DATA['user_id'], OFFER_DATA['event_id']) self.assertEqual(outcome.status, Status.ACCEPTED)
def get_event_status(event_id, user_id): invited_guest = invited_guest_repository.get_for_event_and_user( event_id, user_id) if invited_guest: registration = invited_guest_repository.get_registration_for_event_and_user( event_id, user_id) # If they're an invited guest, we don't bother with whether they applied or not return EventStatus( invited_guest=invited_guest.role, registration_status=_get_registration_status(registration)) response = response_repository.get_by_user_id_for_event(user_id, event_id) if response is None: application_status = None elif response.is_submitted: application_status = 'Submitted' elif response.is_withdrawn: application_status = 'Withdrawn' else: application_status = 'Not Submitted' outcome = outcome_repository.get_latest_by_user_for_event( user_id, event_id) if outcome is None: outcome_status = None else: outcome_status = outcome.status.name offer = offer_repository.get_by_user_id_for_event(user_id, event_id) if offer is None: offer_status = None elif offer.candidate_response: offer_status = 'Accepted' elif offer.candidate_response == False: offer_status = 'Rejected' elif offer.is_expired(): offer_status = 'Expired' else: offer_status = 'Pending' registration = registration_repository.get_by_user_id(user_id, event_id) registration_status = _get_registration_status(registration) return EventStatus(application_status=application_status, outcome_status=outcome_status, offer_status=offer_status, registration_status=registration_status)
def get(self): req_parser = reqparse.RequestParser() req_parser.add_argument('event_id', type=int, required=True) args = req_parser.parse_args() user_id = g.current_user['id'] event_id = args['event_id'] try: outcome = outcome_repository.get_latest_by_user_for_event(user_id, event_id) if not outcome: return errors.OUTCOME_NOT_FOUND return outcome except SQLAlchemyError as e: LOGGER.error("Database error encountered: {}".format(e)) return errors.DB_NOT_AVAILABLE except: LOGGER.error("Encountered unknown error: {}".format(traceback.format_exc())) return errors.DB_NOT_AVAILABLE
def test_post(self): """Test typical post flow.""" self.seed_static_data() response = self.app.post( '/api/v1/outcome', data={ 'event_id': self.event2.id, 'user_id': self.test_user1.id, 'outcome': 'REJECTED' }, headers=self.get_auth_header_for('*****@*****.**')) data = json.loads(response.data) self.assertEqual(response.status_code, 201) outcomes = outcome_repository.get_all_by_user_for_event( self.test_user1_id, self.event2.id) outcomes = list(sorted(outcomes, key=lambda o: o.timestamp)) print(outcomes) self.assertEqual(outcomes[0].status, Status.WAITLIST) self.assertFalse(outcomes[0].latest) self.assertEqual(outcomes[1].status, Status.REJECTED) self.assertTrue(outcomes[1].latest)