Example #1
0
    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
Example #2
0
    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
Example #3
0
    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