Esempio n. 1
0
def users_for_select_element(request):
    u = UserCollection(request.session).query()
    u = u.with_entities(User.id, User.username, User.title, User.realname)
    u = u.order_by(func.lower(User.title))
    u = u.filter_by(active=True)
    return tuple(u)
Esempio n. 2
0
def book_occasion(self, request, form):

    # for the "nth. occasion" title
    number = request.session.execute("""
        SELECT count(*) FROM occasions
        WHERE activity_id = :activity_id AND "order" <= :order
    """, {
        'activity_id': self.activity_id,
        'order': self.order
    }).scalar()

    if form.submitted(request):
        attendees = AttendeeCollection(request.session)
        user = form.user

        if form.is_new_attendee:
            attendee = attendees.add(
                user=user,
                name=form.name,
                birth_date=form.birth_date.data,
                gender=form.gender.data,
                notes=form.notes.data
            )
        else:
            attendee = attendees.by_id(form.attendee.data)
            assert attendee.username == form.username

        # should be caught by the form
        assert not (self.full and self.period.confirmed)
        assert self.activity.state == 'accepted'

        bookings = BookingCollection(request.session)

        # if there's a canceled/denied booking blocking the way, reactivate it
        booking = None

        if not form.is_new_attendee:
            booking = bookings.query()\
                .filter(Booking.occasion_id == self.id)\
                .filter(Booking.username == user.username)\
                .filter(Booking.attendee == attendee)\
                .filter(Booking.state.in_((
                    'cancelled',
                    'denied',
                    'blocked',
                )))\
                .first()

            if booking:
                booking.state = 'open'

        if booking is None:
            booking = bookings.add(
                user=user,
                attendee=attendee,
                occasion=self
            )

        # if the TOS have been accepted, record this now
        if hasattr(form, 'accept_tos') and form.accept_tos:
            if form.accept_tos.data:
                request.current_user.data['tos_accepted'] = True

        if self.period.confirmed:
            bookings.accept_booking(booking)
            request.success(
                _("The booking for ${name} was succesfull", mapping={
                    'name': attendee.name
                })
            )
        else:
            request.success(
                _("The occasion was added to ${name}'s wishlist", mapping={
                    'name': attendee.name
                }))

        return request.redirect(request.link(self.activity))

    title = _("Enroll Attendee")

    users = []

    if request.is_admin:
        u = UserCollection(request.session).query()
        u = u.with_entities(User.username, User.title)
        u = u.order_by(User.title)

        users = u

    return {
        'layout': OccasionFormLayout(self.activity, request, title),
        'title': title,
        'form': form,
        'occasion': self,
        'users': users,
        'button_text': _("Enroll"),
        'number': number,
    }