Ejemplo n.º 1
0
def publish_event(self, request):
    """ Publish an event. """

    self.publish()

    request.success(_("You have accepted the event ${title}", mapping={
        'title': self.title
    }))

    if self.meta.get('submitter_email'):

        session = request.app.session()
        ticket = TicketCollection(session).by_handler_id(self.id.hex)

        send_html_mail(
            request=request,
            template='mail_event_accepted.pt',
            subject=_("Your event was accepted"),
            receivers=(self.meta.get('submitter_email'), ),
            content={
                'model': self,
                'ticket': ticket
            }
        )

    if 'return-to' in request.GET:
        return morepath.redirect(request.GET['return-to'])

    return morepath.redirect(request.link(self))
Ejemplo n.º 2
0
def accept_reservation(self, request):
    if not self.data or not self.data.get('accepted'):
        collection = ResourceCollection(request.app.libres_context)
        resource = collection.by_id(self.resource)
        scheduler = resource.get_scheduler(request.app.libres_context)
        reservations = scheduler.reservations_by_token(self.token)

        send_html_mail(
            request=request,
            template='mail_reservation_accepted.pt',
            subject=_("Your reservation was accepted"),
            receivers=(self.email, ),
            content={
                'model': self,
                'resource': resource,
                'reservations': reservations
            }
        )

        for reservation in reservations:
            reservation.data = reservation.data or {}
            reservation.data['accepted'] = True

            # libres does not automatically detect changes yet
            flag_modified(reservation, 'data')

        request.success(_("The reservation was accepted"))
    else:
        request.warning(_("The reservation has already been accepted"))

    return morepath.redirect(request.params['return-to'])
Ejemplo n.º 3
0
def reject_reservation(self, request):
    collection = ResourceCollection(request.app.libres_context)
    resource = collection.by_id(self.resource)
    scheduler = resource.get_scheduler(request.app.libres_context)
    reservations = scheduler.reservations_by_token(self.token.hex)
    forms = FormCollection(request.app.session())
    submission = forms.submissions.by_id(self.token.hex)

    send_html_mail(
        request=request,
        template='mail_reservation_rejected.pt',
        subject=_("Your reservation was rejected"),
        receivers=(self.email, ),
        content={
            'model': self,
            'resource': resource,
            'reservations': reservations
        }
    )

    # create a snapshot of the ticket to keep the useful information
    tickets = TicketCollection(request.app.session())
    ticket = tickets.by_handler_id(self.token.hex)
    ticket.create_snapshot(request)

    scheduler.remove_reservation(self.token.hex)

    if submission:
        forms.submissions.delete(submission)

    request.success(_("The reservation was rejected"))

    # return none on intercooler js requests
    if not request.headers.get('X-IC-Request'):
        return morepath.redirect(request.params['return-to'])
Ejemplo n.º 4
0
def reopen_ticket(self, request):
    user = UserCollection(request.app.session()).by_username(
        request.identity.userid)

    try:
        self.reopen_ticket(user)
    except InvalidStateChange:
        request.alert(
            _("The ticket cannot be re-opened because it's not closed.")
        )
    else:
        request.success(_("You have reopened ticket ${number}", mapping={
            'number': self.number
        }))

    email = self.snapshot.get('email') or self.handler.email

    send_html_mail(
        request=request,
        template='mail_ticket_reopened.pt',
        subject=_("Your ticket has been reopened"),
        receivers=(email, ),
        content={
            'model': self
        }
    )

    request.app.update_ticket_count()

    return morepath.redirect(request.link(self))
Ejemplo n.º 5
0
def close_ticket(self, request):

    try:
        self.close_ticket()
    except InvalidStateChange:
        request.alert(
            _("The ticket cannot be closed because it's not pending")
        )
    else:
        request.success(_("You have closed ticket ${number}", mapping={
            'number': self.number
        }))

    email = self.snapshot.get('email') or self.handler.email

    send_html_mail(
        request=request,
        template='mail_ticket_closed.pt',
        subject=_("Your ticket has been closed"),
        receivers=(email, ),
        content={
            'model': self
        }
    )

    request.app.update_ticket_count()

    return morepath.redirect(
        request.link(TicketCollection(request.app.session())))
Ejemplo n.º 6
0
def handle_password_reset_request(self, request, form):
    """ Handles the GET and POST password reset requests. """
    if form.submitted(request):
        user, token = form.get_token(request)
        if user is not None and token is not None:
            url = "{0}?token={1}".format(request.link(self, name="reset-password"), token)
            send_html_mail(
                request=request,
                template="mail_password_reset.pt",
                subject=_("Password reset"),
                receivers=(user.username,),
                content={"model": None, "url": url},
            )
        else:
            log.info("Failed password reset attempt by {}".format(request.client_addr))

        response = morepath.redirect(request.link(self))
        request.success(
            _(
                (
                    u"A password reset link has been sent to ${email}, provided an "
                    u"account exists for this email address."
                ),
                mapping={"email": form.email.data},
            )
        )
        return response

    layout = DefaultLayout(self, request)
    layout.breadcrumbs = [
        Link(_("Homepage"), layout.homepage_url),
        Link(_("Reset password"), request.link(self, name="request-password")),
    ]

    return {"layout": layout, "title": _(u"Reset password"), "form": form, "form_width": "small"}
Ejemplo n.º 7
0
def view_event(self, request):
    """ View an event.

    If the event is not already submitted, the submit form is displayed.

    A logged-in user can view all events and might edit them, an anonymous user
    will be redirected.
    """

    assert_anonymous_access_only_temporary(request, self)

    if 'complete' in request.POST and self.state == 'initiated':
        if self.meta and 'session_id' in self.meta:
            del self.meta['session_id']

        self.submit()

        session = request.app.session()
        with session.no_autoflush:
            ticket = TicketCollection(session).open_ticket(
                handler_code='EVN', handler_id=self.id.hex
            )

        send_html_mail(
            request=request,
            template='mail_ticket_opened.pt',
            subject=_("A ticket has been opened"),
            receivers=(self.meta.get('submitter_email'), ),
            content={
                'model': ticket
            }
        )

        request.success(_("Thank you for your submission!"))
        request.app.update_ticket_count()

        return morepath.redirect(request.link(ticket, 'status'))

    session = request.app.session()
    ticket = TicketCollection(session).by_handler_id(self.id.hex)

    return {
        'completable': self.state == 'initiated',
        'edit_url': request.link(self, 'bearbeiten'),
        'event': self,
        'layout': EventLayout(self, request),
        'ticket': ticket,
        'title': self.title,
    }
Ejemplo n.º 8
0
def finalize_reservation(self, request):

    # this view is public, but only for a limited time
    assert_anonymous_access_only_temporary(self, request)

    collection = ResourceCollection(request.app.libres_context)
    resource = collection.by_id(self.resource)
    scheduler = resource.get_scheduler(request.app.libres_context)
    session_id = get_libres_session_id(request)

    try:
        scheduler.queries.confirm_reservations_for_session(session_id)
        scheduler.approve_reservations(self.token)
    except LibresError as e:
        utils.show_libres_error(e, request)

        layout = ReservationLayout(resource, request)
        layout.breadcrumbs.append(Link(_("Error"), '#'))

        return {
            'title': _("The reservation could not be completed"),
            'layout': layout,
        }
    else:
        forms = FormCollection(request.app.session())
        submission = forms.submissions.by_id(self.token)

        if submission:
            forms.submissions.complete_submission(submission)

        with forms.session.no_autoflush:
            ticket = TicketCollection(request.app.session()).open_ticket(
                handler_code='RSV', handler_id=self.token.hex
            )

        send_html_mail(
            request=request,
            template='mail_ticket_opened.pt',
            subject=_("A ticket has been opened"),
            receivers=(self.email, ),
            content={
                'model': ticket
            }
        )

        request.success(_("Thank you for your reservation!"))
        request.app.update_ticket_count()

        return morepath.redirect(request.link(ticket, 'status'))
Ejemplo n.º 9
0
def handle_complete_submission(self, request):
    form = request.get_form(self.form_class, data=self.data)

    # we're not really using a csrf protected form here (the complete form
    # button is basically just there so we can use a POST instead of a GET)
    form.validate()
    form.ignore_csrf_error()

    if form.errors:
        return morepath.redirect(request.link(self))
    else:
        if self.state == 'complete':
            self.data.changed()  # trigger updates
            request.success(_(u"Your changes were saved"))

            return morepath.redirect(request.link(
                FormCollection(request.app.session()).scoped_submissions(
                    self.name, ensure_existance=False)
            ))
        else:
            collection = FormCollection(request.app.session())
            collection.submissions.complete_submission(self)

            # make sure accessing the submission doesn't flush it, because
            # it uses sqlalchemy utils observe, which doesn't like premature
            # flushing at all
            with collection.session.no_autoflush:
                ticket = TicketCollection(request.app.session()).open_ticket(
                    handler_code='FRM', handler_id=self.id.hex
                )

            send_html_mail(
                request=request,
                template='mail_ticket_opened.pt',
                subject=_("A ticket has been opened"),
                receivers=(self.email, ),
                content={
                    'model': ticket
                }
            )

            request.success(_("Thank you for your submission!"))
            request.app.update_ticket_count()

            return morepath.redirect(request.link(ticket, 'status'))
Ejemplo n.º 10
0
def handle_password_reset_request(self, request, form):
    """ Handles the GET and POST password reset requests. """
    if form.submitted(request):
        user, token = form.get_token(request)
        if user is not None and token is not None:
            url = '{0}?token={1}'.format(
                request.link(self, name='reset-password'), token
            )
            send_html_mail(
                request=request,
                template='mail_password_reset.pt',
                subject=_("Password reset"),
                receivers=(user.username, ),
                content={'model': None, 'url': url}
            )
        else:
            log.info(
                "Failed password reset attempt by {}".format(
                    request.client_addr
                )
            )

        response = morepath.redirect(request.link(self))
        request.success(
            _(('A password reset link has been sent to ${email}, provided an '
               'account exists for this email address.'),
              mapping={'email': form.email.data})
        )
        return response

    layout = DefaultLayout(self, request)
    layout.breadcrumbs = [
        Link(_("Homepage"), layout.homepage_url),
        Link(_("Reset password"), request.link(self, name='request-password'))
    ]

    return {
        'layout': layout,
        'title': _('Reset password'),
        'form': form,
        'form_width': 'small'
    }
Ejemplo n.º 11
0
def close_ticket(self, request):
    self.close_ticket()

    request.success(_(u"You have closed ticket ${number}", mapping={
        'number': self.number
    }))

    send_html_mail(
        request=request,
        template='mail_ticket_closed.pt',
        subject=_("Your ticket has been closed"),
        receivers=(self.handler.email, ),
        content={
            'model': self
        }
    )

    request.app.update_ticket_count()

    return morepath.redirect(
        request.link(TicketCollection(request.app.session())))
Ejemplo n.º 12
0
def reopen_ticket(self, request):
    user = UserCollection(request.app.session()).by_username(
        request.identity.userid)

    self.reopen_ticket(user)

    request.success(_(u"You have reopened ticket ${number}", mapping={
        'number': self.number
    }))

    send_html_mail(
        request=request,
        template='mail_ticket_reopened.pt',
        subject=_("Your ticket has been reopened"),
        receivers=(self.handler.email, ),
        content={
            'model': self
        }
    )

    request.app.update_ticket_count()

    return morepath.redirect(request.link(self))
Ejemplo n.º 13
0
def handle_delete_event(self, request):
    """ Delete an event. """

    request.assert_valid_csrf_token()

    # Create a snapshot of the ticket to keep the useful information.
    tickets = TicketCollection(request.app.session())
    ticket = tickets.by_handler_id(self.id.hex)
    if ticket:
        ticket.create_snapshot(request)

    if self.meta.get('submitter_email'):
        send_html_mail(
            request=request,
            template='mail_event_rejected.pt',
            subject=_("Your event was rejected"),
            receivers=(self.meta.get('submitter_email'), ),
            content={
                'model': self,
                'ticket': ticket
            }
        )

    EventCollection(request.app.session()).delete(self)