Ejemplo n.º 1
0
    def put(self):
        args = self.req_parser.parse_args()

        if len(args['name']) == 0 or len(args['description']) == 0:
            return EVENT_MUST_CONTAIN_TRANSLATION

        if set(args['name']) != set(args['description']):
            return EVENT_TRANSLATION_MISMATCH

        event = event_repository.get_by_id(args['id'])
        if not event:
            return EVENT_NOT_FOUND

        if event_repository.exists_by_key(
                args['key']) and args['key'] != event.key:
            return EVENT_KEY_IN_USE

        user_id = g.current_user["id"]
        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_event_admin(event.id):
            return FORBIDDEN

        event.update(args['name'], args['description'], args['start_date'],
                     args['end_date'], args['key'], args['organisation_id'],
                     args['email_from'], args['url'], args['application_open'],
                     args['application_close'], args['review_open'],
                     args['review_close'], args['selection_open'],
                     args['selection_close'], args['offer_open'],
                     args['offer_close'], args['registration_open'],
                     args['registration_close'], args['travel_grant'],
                     args['miniconf_url'])
        db.session.commit()

        event = event_repository.get_by_id(event.id)
        return event, 200
Ejemplo n.º 2
0
    def post(self):
        args = self.req_parser.parse_args()

        user_id = g.current_user["id"]
        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_admin:
            return FORBIDDEN

        if event_repository.exists_by_key(args['key']):
            return EVENT_KEY_IN_USE

        if len(args['name']) == 0 or len(args['description']) == 0:
            return EVENT_MUST_CONTAIN_TRANSLATION

        if set(args['name']) != set(args['description']):
            return EVENT_TRANSLATION_MISMATCH

        event = Event(args['name'], args['description'], args['start_date'],
                      args['end_date'], args['key'], args['organisation_id'],
                      args['email_from'], args['url'],
                      args['application_open'], args['application_close'],
                      args['review_open'], args['review_close'],
                      args['selection_open'], args['selection_close'],
                      args['offer_open'], args['offer_close'],
                      args['registration_open'], args['registration_close'],
                      EventType[args['event_type'].upper()],
                      args['travel_grant'], args['miniconf_url'])

        event.add_event_role('admin', user_id)
        event = event_repository.add(event)

        event = event_repository.get_by_id(event.id)
        return event, 201
Ejemplo n.º 3
0
 def get(self):
     event_id = request.args['id']
     event = event_repository.get_by_id(event_id)
     if not event:
         return EVENT_NOT_FOUND
     else:
         event_org = event_repository.get_by_id_with_organisation(event.id)
         return event_details(event_org), 200
Ejemplo n.º 4
0
    def put(self):
        args = self.req_parser.parse_args()

        event = event_repository.get_by_id(args['id'])
        if not event:
            return EVENT_NOT_FOUND

        user_id = g.current_user["id"]
        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_event_admin(event.id):
            return FORBIDDEN

        _date_format = '%Y-%m-%dT%H:%M:%S.%fZ'
        name = args['name']
        description = args['description']
        start_date = datetime.strptime((args['start_date']), _date_format)
        end_date = datetime.strptime((args['end_date']), _date_format)
        key = args['key']
        organisation_id = args['organisation_id']
        email_from = args['email_from']
        url = args['url']
        application_open = datetime.strptime((args['application_open']),
                                             _date_format)
        application_close = datetime.strptime((args['application_close']),
                                              _date_format)
        review_open = datetime.strptime((args['review_open']), _date_format)
        review_close = datetime.strptime((args['review_close']), _date_format)
        selection_open = datetime.strptime((args['selection_open']),
                                           _date_format)
        selection_close = datetime.strptime((args['selection_close']),
                                            _date_format)
        offer_open = datetime.strptime((args['offer_open']), _date_format)
        offer_close = datetime.strptime((args['offer_close']), _date_format)
        registration_open = datetime.strptime((args['registration_open']),
                                              _date_format)
        registration_close = datetime.strptime((args['registration_close']),
                                               _date_format)

        event.update(name, description, start_date, end_date, key,
                     organisation_id, email_from, url, application_open,
                     application_close, review_open, review_close,
                     selection_open, selection_close, offer_open, offer_close,
                     registration_open, registration_close)

        try:
            db.session.commit()
        except IntegrityError:
            LOGGER.error("Event with KEY: {} already exists".format(key))
            return EVENT_KEY_IN_USE

        event_org = event_repository.get_by_id_with_organisation(event.id)
        return event_details(event_org), 200
Ejemplo n.º 5
0
    def post(self):
        args = self.req_parser.parse_args()

        user_id = g.current_user["id"]
        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_admin:
            return FORBIDDEN

        _date_format = '%Y-%m-%dT%H:%M:%S.%fZ'
        name = args['name']
        description = args['description']
        start_date = datetime.strptime((args['start_date']), _date_format)
        end_date = datetime.strptime((args['end_date']), _date_format)
        key = args['key']
        organisation_id = args['organisation_id']
        email_from = args['email_from']
        url = args['url']
        application_open = datetime.strptime((args['application_open']),
                                             _date_format)
        application_close = datetime.strptime((args['application_close']),
                                              _date_format)
        review_open = datetime.strptime((args['review_open']), _date_format)
        review_close = datetime.strptime((args['review_close']), _date_format)
        selection_open = datetime.strptime((args['selection_open']),
                                           _date_format)
        selection_close = datetime.strptime((args['selection_close']),
                                            _date_format)
        offer_open = datetime.strptime((args['offer_open']), _date_format)
        offer_close = datetime.strptime((args['offer_close']), _date_format)
        registration_open = datetime.strptime((args['registration_open']),
                                              _date_format)
        registration_close = datetime.strptime((args['registration_close']),
                                               _date_format)
        event_type = args['event_type'].upper()

        event = Event(name, description, start_date, end_date, key,
                      organisation_id, email_from, url, application_open,
                      application_close, review_open, review_close,
                      selection_open, selection_close, offer_open, offer_close,
                      registration_open, registration_close,
                      EventType[event_type])
        event.add_event_role('admin', user_id)
        try:
            event = event_repository.add(event)
        except IntegrityError as e:

            LOGGER.error("Event with KEY: {} already exists".format(key))
            LOGGER.error(e)
            return EVENT_KEY_IN_USE

        event_org = event_repository.get_by_id_with_organisation(event.id)
        return event_details(event_org), 201
Ejemplo n.º 6
0
    def post(self):
        args = self.req_parser.parse_args()
        registration_id = args['registration_id']
        user_id = g.current_user['id']

        try:
            current_user = UserRepository.get_by_id(user_id)
            registration, offer = RegistrationRepository.get_by_id_with_offer(
                registration_id)
            if not current_user.is_registration_admin(offer.event_id):
                return errors.FORBIDDEN

            registration.confirm()
            registration_user = UserRepository.get_by_id(offer.user_id)
            registration_event = EventRepository.get_by_id(offer.event_id)
            if _send_registration_confirmation_mail(registration_user,
                                                    registration_event):
                registration.confirmation_email_sent_at = datetime.now()

            db.session.commit()
            return 'Confirmed Registration for {} {}'.format(
                registration_user.firstname, registration_user.lastname), 200

        except Exception as e:
            LOGGER.error(
                'Error occured while confirming registration with id {}: {}'.
                format(registration_id, e))
            return errors.DB_NOT_AVAILABLE
Ejemplo n.º 7
0
    def delete(self):
        args = self.req_parser.parse_args()
        event_id = args['event_id']
        user_id = args['user_id']
        registration_user_id = g.current_user['id']

        registration_user = user_repository.get_by_id(registration_user_id)
        if not registration_user.is_registration_volunteer(event_id):
            return FORBIDDEN

        event = event_repository.get_by_id(event_id)
        if event is None:
            return EVENT_NOT_FOUND

        user = user_repository.get_by_id(user_id)
        if user is None:
            return USER_NOT_FOUND

        attendance = attendance_repository.get(event_id, user_id)
        if attendance is None:
            return ATTENDANCE_NOT_FOUND

        attendance_repository.delete(attendance)

        return 200
Ejemplo n.º 8
0
    def get(self):
        args = self.req_parser.parse_args()

        event = event_repository.get_by_key_with_organisation(
            args['event_key'])
        if not event:
            return EVENT_WITH_KEY_NOT_FOUND

        return event_info(g.current_user['id'], event), 200
Ejemplo n.º 9
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.º 10
0
Archivo: api.py Proyecto: mxaba/Baobab
    def post(self, send_email=True):
        args = self.req_parser.parse_args()
        event_id = args['event_id']
        email = args['email']
        role = args['role']

        user = user_repository.get_by_email(email, g.organisation.id)

        if not user:
            return USER_NOT_FOUND

        event = event_repository.get_by_id(event_id)
        if not event:
            return EVENT_NOT_FOUND

        existingInvitedGuest = db.session.query(InvitedGuest).filter(
            InvitedGuest.event_id == event_id).filter(
                InvitedGuest.user_id == user.id).first()

        if existingInvitedGuest:
            return INVITED_GUEST_FOR_EVENT_EXISTS

        invitedGuest = InvitedGuest(event_id=event_id,
                                    user_id=user.id,
                                    role=role)

        db.session.add(invitedGuest)

        try:
            db.session.commit()
        except IntegrityError:
            LOGGER.error("Failed to add invited guest: {}".format(email))
            return ADD_INVITED_GUEST_FAILED

        if send_email:
            try:
                send_mail(recipient=user.email,
                          subject='Your invitation to {}'.format(event.name),
                          body_text=GUEST_EMAIL_TEMPLATE.format(
                              user_title=user.user_title,
                              firstname=user.firstname,
                              lastname=user.lastname,
                              role=role,
                              event_name=event.name,
                              event_key=event.key,
                              system_name=g.organisation.system_name,
                              host=misc.get_baobab_host()))
            except Exception as e:
                LOGGER.error(
                    'Failed to send email to invited guest with user Id {}, due to {}'
                    .format(user.id, e))
                return INVITED_GUEST_EMAIL_FAILED

        return invitedGuest_info(invitedGuest, user), 201
Ejemplo n.º 11
0
    def post(self, send_email=True):
        args = self.req_parser.parse_args()
        event_id = args['event_id']
        email = args['email']
        role = args['role']

        user = user_repository.get_by_email(email, g.organisation.id)

        if not user:
            return USER_NOT_FOUND

        event = event_repository.get_by_id(event_id)
        if not event:
            return EVENT_NOT_FOUND

        existingInvitedGuest = db.session.query(InvitedGuest).filter(
            InvitedGuest.event_id == event_id).filter(InvitedGuest.user_id == user.id).first()

        if existingInvitedGuest:
            return INVITED_GUEST_FOR_EVENT_EXISTS

        invitedGuest = InvitedGuest(
            event_id=event_id,
            user_id=user.id,
            role=role
        )

        db.session.add(invitedGuest)

        try:
            db.session.commit()
        except IntegrityError:
            LOGGER.error(
                "Failed to add invited guest: {}".format(email))
            return ADD_INVITED_GUEST_FAILED

        if send_email:
            try:
                email_user(
                    'guest-invitation-with-registration' if event.is_registration_open else 'guest-invitation',
                    template_parameters=dict(
                        role=role,
                        system_name=g.organisation.system_name,
                        host=misc.get_baobab_host(),
                        event_key=event.key
                    ),
                    event=event,
                    user=user)

            except Exception as e:
                LOGGER.error('Failed to send email to invited guest with user Id {}, due to {}'.format(user.id, e))
                return INVITED_GUEST_EMAIL_FAILED

        return invitedGuest_info(invitedGuest, user), 201
Ejemplo n.º 12
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.º 13
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
Ejemplo n.º 14
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.º 15
0
    def get(self):
        user_id = 0

        if g and hasattr(g, 'current_user') and g.current_user:
            user_id = g.current_user["id"]

        events = event_repository.get_upcoming_for_organisation(
            g.organisation.id)
        returnEvents = []

        for event in events:
            returnEvents.append(event_info(user_id, event))

        return returnEvents, 200
Ejemplo n.º 16
0
    def get(self):
        user_id = g.current_user["id"]
        language = request.args['language']
        default_language = 'en'

        upcoming_events = event_repository.get_upcoming_for_organisation(
            g.organisation.id)
        attended_events = event_repository.get_attended_by_user_for_organisation(
            g.organisation.id, user_id)

        returnEvents = []

        for event in itertools.chain(upcoming_events, attended_events):
            if not event.has_specific_translation(language):
                LOGGER.error('Missing {} translation for event {}.'.format(
                    language, event.id))
                language = default_language
            status = None if user_id == 0 else event_status.get_event_status(
                event.id, user_id)
            returnEvents.append(event_info(user_id, event, status, language))
            language = request.args['language']

        return returnEvents, 200
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
    def get(self):
        args = self.get_req_parser.parse_args()
        event_id = args['event_id']
        current_user_id = g.current_user['id']

        event = event_repository.get_by_id(event_id)
        if not event:
            return errors.EVENT_NOT_FOUND

        if not event.has_application_form():
            return errors.FORM_NOT_FOUND

        form = event.get_application_form()
        responses = response_repository.get_all_for_user_application(current_user_id, form.id)
        return responses
Ejemplo n.º 19
0
    def post(self, event_id):
        parser = reqparse.RequestParser()
        parser.add_argument('response_ids',
                            type=int,
                            required=True,
                            action='append')
        parser.add_argument('reviewer_email', type=str, required=True)
        args = parser.parse_args()

        response_ids = args['response_ids']
        reviewer_email = args['reviewer_email']

        filtered_response_ids = response_repository.filter_ids_to_event(
            response_ids, event_id)

        print('response_ids:', response_ids)
        print('filtered_response_ids:', filtered_response_ids)

        if set(filtered_response_ids) != set(response_ids):
            return FORBIDDEN

        event = event_repository.get_by_id(event_id)

        reviewer_user = user_repository.get_by_email(reviewer_email,
                                                     g.organisation.id)
        if reviewer_user is None:
            return USER_NOT_FOUND

        if not reviewer_user.is_reviewer(event_id):
            _add_reviewer_role(reviewer_user.id, event_id)

        response_reviewers = [
            ResponseReviewer(response_id, reviewer_user.id)
            for response_id in response_ids
        ]
        db.session.add_all(response_reviewers)
        db.session.commit()

        if len(response_ids) > 0:
            email_user('reviews-assigned',
                       template_parameters=dict(
                           num_reviews=len(response_ids),
                           baobab_host=misc.get_baobab_host(),
                           system_name=g.organisation.system_name,
                           event_key=event.key),
                       event=event,
                       user=reviewer_user)
        return {}, 201
Ejemplo n.º 20
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.º 21
0
    def post(self):
        args = self.post_req_parser.parse_args()
        user_id = g.current_user['id']
        event_id = args['event_id']
        reviewer_user_email = args['reviewer_user_email']
        num_reviews = args['num_reviews']

        event = event_repository.get_by_id(event_id)
        if not event:
            return EVENT_NOT_FOUND

        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_event_admin(event_id):
            return FORBIDDEN

        reviewer_user = user_repository.get_by_email(reviewer_user_email,
                                                     g.organisation.id)
        if reviewer_user is None:
            return USER_NOT_FOUND

        if not reviewer_user.is_reviewer(event_id):
            _add_reviewer_role(reviewer_user.id, event_id)

        config = review_configuration_repository.get_configuration_for_event(
            event_id)

        response_ids = self.get_eligible_response_ids(
            event_id, reviewer_user.id, num_reviews,
            config.num_reviews_required)
        response_reviewers = [
            ResponseReviewer(response_id, reviewer_user.id)
            for response_id in response_ids
        ]
        db.session.add_all(response_reviewers)
        db.session.commit()

        if len(response_ids) > 0:
            email_user('reviews-assigned',
                       template_parameters=dict(
                           num_reviews=len(response_ids),
                           baobab_host=misc.get_baobab_host(),
                           system_name=g.organisation.system_name,
                           event_key=event.key),
                       event=event,
                       user=reviewer_user)
        return {}, 201
Ejemplo n.º 22
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.º 23
0
Archivo: api.py Proyecto: mxaba/Baobab
    def post(self):
        args = self.req_parser.parse_args()

        user_api = UserAPI.UserAPI()
        user, status = user_api.post(invitedGuest=True)
        if status != 201:
            return user, status

        invited_guest_api = InvitedGuestAPI()
        invited_guest_info, status = invited_guest_api.post(send_email=False)

        if status == 201:
            event_id = invited_guest_info['event_id']
            role = invited_guest_info['role']
            user = user_repository.get_by_id(user['id'])
            event = event_repository.get_by_id(event_id)

            reset_code = misc.make_code()
            password_reset = PasswordReset(user=user)
            db.session.add(password_reset)
            db.session.commit()

            try:
                send_mail(recipient=user.email,
                          subject='Your invitation to {}'.format(event.name),
                          body_text=NEW_GUEST_EMAIL_TEMPLATE.format(
                              user_title=user.user_title,
                              firstname=user.firstname,
                              lastname=user.lastname,
                              role=role,
                              event_name=event.name,
                              event_key=event.key,
                              system_name=g.organisation.system_name,
                              host=misc.get_baobab_host(),
                              reset_code=password_reset.code))
            except Exception as e:
                LOGGER.error(
                    'Failed to send email for invited guest with user Id {} due to: {}'
                    .format(user.id, e))
                return INVITED_GUEST_EMAIL_FAILED

        return invited_guest_info, status
Ejemplo n.º 24
0
    def post(self):
        args = self.req_parser.parse_args()

        user_api = UserAPI.UserAPI()
        user, status = user_api.post(invitedGuest=True)
        if status != 201:
            return user, status

        invited_guest_api = InvitedGuestAPI()
        invited_guest_info, status = invited_guest_api.post(send_email=False)

        if status == 201:
            event_id = invited_guest_info['event_id']
            role = invited_guest_info['role']
            user = user_repository.get_by_id(user['id'])
            event = event_repository.get_by_id(event_id)
            
            reset_code = misc.make_code()
            password_reset=PasswordReset(user=user)
            db.session.add(password_reset)
            db.session.commit()

            try:

                email_user(
                    'new-guest-registration' if event.is_registration_open else 'new-guest-no-registration',
                    template_parameters=dict(
                        event_key=event.key,
                        system_name=g.organisation.system_name,
                        host=misc.get_baobab_host(),
                        role=role,
                        reset_code=password_reset.code,
                    ),
                    event=event,
                    user=user
                )
            except Exception as e:
                LOGGER.error('Failed to send email for invited guest with user Id {} due to: {}'.format(user.id, e))
                return INVITED_GUEST_EMAIL_FAILED

        return invited_guest_info, status
Ejemplo n.º 25
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.º 26
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.º 27
0
    def get(self):
        args = self.req_parser.parse_args()

        user_id = g.current_user['id']
        language = args['language']
        if language is None or len(language) > 2:
            LOGGER.warning(
                "Missing or invalid language parameter for EventsByKeyAPI. Defaulting to 'en'"
            )
            default_language = 'en'
            language = default_language

        event = event_repository.get_by_key(args['event_key'])
        if not event:
            return EVENT_WITH_KEY_NOT_FOUND

        if not event.has_specific_translation(language):
            return EVENT_WITH_TRANSLATION_NOT_FOUND

        info = event_info(g.current_user['id'], event,
                          event_status.get_event_status(event.id, user_id),
                          language)
        return info, 200
Ejemplo n.º 28
0
    def get(self):
        args = self.req_parser.parse_args()

        event = event_repository.get_by_id_with_organisation(args['event_id'])
        if not event:
            return EVENT_NOT_FOUND

        user_id = g.current_user["id"]
        event_id = args['event_id']
        current_user = user_repository.get_by_id(user_id)
        if not current_user.is_event_admin(event_id):
            return FORBIDDEN

        num_users = db.session.query(AppUser.id).count()
        num_responses = db.session.query(Response.id).count()
        num_submitted_respones = db.session.query(Response).filter(
            Response.is_submitted == True).count()

        return {
            'event_description': event.Event.description,
            'num_users': num_users,
            'num_responses': num_responses,
            'num_submitted_responses': num_submitted_respones
        }, 200
Ejemplo n.º 29
0
    def post(self):
        args = self.req_parser.parse_args()
        event_id = args['event_id']
        user_id = args['user_id']
        registration_user_id = g.current_user['id']

        registration_user = user_repository.get_by_id(registration_user_id)
        if not registration_user.is_registration_volunteer(event_id):
            return FORBIDDEN

        event = event_repository.get_by_id(event_id)
        if event is None:
            return EVENT_NOT_FOUND

        user = user_repository.get_by_id(user_id)
        if user is None:
            return USER_NOT_FOUND

        if attendance_repository.exists(event_id, user_id):
            return ATTENDANCE_ALREADY_CONFIRMED

        attendance = Attendance(event_id, user_id, registration_user_id)
        attendance_repository.create(attendance)

        send_mail(recipient=user.email,
                  subject='Welcome to {}'.format(event.name),
                  body_text=ATTENDANCE_EMAIL_BODY.format(
                      user_title=user.user_title,
                      first_name=user.firstname,
                      last_name=user.lastname,
                      event_name=event.name))

        # Other Fields
        unavailable_response = None
        invitedguest_role = unavailable_response
        is_invited_guest = unavailable_response
        has_accepted_accom_award = unavailable_response

        offer = db.session.query(Offer).filter(
            Offer.user_id == user_id).filter(
                Offer.event_id == event_id).first()
        if offer:
            has_accepted_accom_award = (offer.accommodation_award
                                        and offer.accepted_accommodation_award)

        # Check if invited guest
        invited_guest = db.session.query(InvitedGuest).filter(
            InvitedGuest.event_id == event_id).filter(
                InvitedGuest.user_id == user.id).first()
        if (invited_guest):
            is_invited_guest = True
            invitedguest_role = invited_guest.role

        # Shirt Size
        shirt_answer = (
            get_registration_answer_based_headline(user_id, "T-Shirt Size")
            and get_registration_answer_based_headline(
                user_id, "T-Shirt Size").value) or unavailable_response

        # Poster registration
        bringing_poster = False
        poster_answer = get_registration_answer_based_headline(
            user_id, "Will you be bringing a poster?")
        if (poster_answer is not None and poster_answer.value == 'yes'):
            bringing_poster = True

        attendance_user = AttendanceUser(attendance, has_accepted_accom_award,
                                         shirt_answer, is_invited_guest,
                                         bringing_poster, invitedguest_role)
        return attendance_user, 201
Ejemplo n.º 30
0
    def put(self):
        # Update an existing response for the logged-in user.
        req_parser = reqparse.RequestParser()
        args = self.req_parser.parse_args()
        try:
            user_id = verify_token(request.headers.get('Authorization'))['id']

            registration = db.session.query(Registration).filter(
                Registration.id == args['registration_id']).one_or_none()
            if registration is None:
                return 'Registration not found', 404

            db_offer = db.session.query(Offer).filter(
                Offer.id == registration.offer_id).one_or_none()

            if db_offer is None:
                return errors.OFFER_NOT_FOUND

            if db_offer.user_id != user_id:
                return errors.FORBIDDEN

            registration.registration_form_id = args['registration_form_id']
            db.session.commit()

            for answer_args in args['answers']:
                answer = db.session.query(RegistrationAnswer).filter(
                    RegistrationAnswer.registration_question_id ==
                    answer_args['registration_question_id'],
                    RegistrationAnswer.registration_id ==
                    args['registration_id']).one_or_none()
                if answer is not None:
                    answer.value = answer_args['value']

                elif db.session.query(RegistrationQuestion).filter(
                        RegistrationQuestion.id ==
                        answer_args['registration_question_id']).one():

                    answer = RegistrationAnswer(
                        registration_id=registration.id,
                        registration_question_id=answer_args[
                            'registration_question_id'],
                        value=answer_args['value'])

                    db.session.add(answer)
            db.session.commit()

            current_user = user_repository.get_by_id(user_id)

            registration_answers = db.session.query(RegistrationAnswer).filter(
                RegistrationAnswer.registration_id == registration.id).all()

            registration_questions = db.session.query(
                RegistrationQuestion).filter(
                    RegistrationQuestion.registration_form_id ==
                    args['registration_form_id']).all()

            registration_form = db.session.query(RegistrationForm).filter(
                RegistrationForm.id == args['registration_form_id']).first()

            event = event_repository.get_by_id(registration_form.event_id)

            self.send_confirmation(current_user, registration_questions,
                                   registration_answers,
                                   registration.confirmed, event)

            return 200
        except Exception as e:
            return 'Could not access DB', 400