Ejemplo n.º 1
0
 def get_form_status(self):
     res = {"active": True}
     dates_status_error = self.get_dates_status_error()
     if dates_status_error:
         res["active"] = False
         res.update(dates_status_error)
         return res
     max_attendees = getattr(self.context, "max_attendees", 0)
     waiting_list_size = getattr(self.context, "waiting_list_size", 0)
     if not max_attendees:
         # no limit
         return res
     registrants_data = self.registrants_data
     if registrants_data.has_reached_subscriptions_limit(
         max_attendees=max_attendees, waiting_list_size=waiting_list_size
     ):
         if registrants_data.waiting_list_is_open(
             max_attendees=max_attendees, waiting_list_size=waiting_list_size
         ):
             res["status_message"] = self.set_status_message(
                 message=_(
                     "message_waiting_list_open",
                     default=u"You can subscribe anyway and enter"
                     u" to a waiting list. "
                     u"We will contact you in case some "
                     u"participants will cancel their subscription.",
                 ),
                 strong_message=_(
                     "message_waiting_list_open_strong",
                     default=u"Attendees limit reached.",
                 ),
                 type="warning",
             )
         else:
             res["active"] = False
             res["status_message"] = self.set_status_message(
                 message=_(
                     "message_attendees_limit_reached",
                     default=u"Attendees limit reached.",
                 ),
                 type="error",
             )
     return res
Ejemplo n.º 2
0
 def set_status_message(self, message, type, strong_message=""):
     if not strong_message:
         strong_message = _(
             "registration_form_error", default=u"You can't subscribe to this form.",
         )
     return {
         "type": type,
         "message": message,
         "strong_message": strong_message,
     }
Ejemplo n.º 3
0
 def handleSubmit(self, action):
     data, errors = self.extractData()
     if errors:
         self.status = self.formErrorsMessage
         return
     registrants = data.get("registrants", [])
     waiting_list = data.get("waiting_list", [])
     if not registrants and not waiting_list:
         api.portal.show_message(
             message=_(
                 "contact_recipients_error",
                 default=u"You need to select at least one recipient from"
                 u" registrants or waiting_list list.",
             ),
             request=self.request,
             type="error",
         )
         return
     email_addresses = self.extract_recipients(registrants, waiting_list)
     if isinstance(email_addresses, str):
         api.portal.show_message(message=email_addresses,
                                 request=self.request,
                                 type="error")
         return
     try:
         self.send_message(data=data, email_addresses=email_addresses)
         api.portal.show_message(
             message=_("contact_recipients_sent", u"Emails sent."),
             request=self.request,
         )
         self.request.response.redirect(self.nextURL())
     except (SMTPException, RuntimeError) as e:
         logger.exception(e)
         plone_utils = api.portal.get_tool(name="plone_utils")
         exception = plone_utils.exceptionString()
         message = pmf(
             u"Unable to send mail: ${exception}",
             mapping={u"exception": exception},
         )
         api.portal.show_message(message=message,
                                 request=self.request,
                                 type="error")
Ejemplo n.º 4
0
 def close_past_msg(self, date):
     return self.set_status_message(
         message=_(
             "message_close_date_past",
             default=u"Registration ended on ${date} at ${hour}.",
             mapping={
                 "date": api.portal.get_localized_time(datetime=date),
                 "hour": date.time().strftime("%H:%M"),
             },
         ),
         type="error",
     )
Ejemplo n.º 5
0
 def open_future_msg(self, date):
     return self.set_status_message(
         message=_(
             "message_open_date_future",
             default=u"Registration opening on ${date} at ${hour}.",
             mapping={
                 "date": api.portal.get_localized_time(datetime=date),
                 "hour": date.time().strftime("%H:%M"),
             },
         ),
         type="warning",
     )
Ejemplo n.º 6
0
class IContactRegistrantsForm(model.Schema):
    subject = schema.TextLine(title=_(u"Subject"), required=True)
    message = schema.Text(title=_(u"Message"), required=True)

    registrants = schema.Tuple(
        title=_(u"Registrants"),
        description=_(
            u"List of registered people that compiled email fields "
            u"(could be one of these fields: email, e-mail, replyto)."),
        required=False,
        default=(),
        missing_value=(),
        value_type=schema.Choice(
            source="collective.easyformplugin.registration.registrants"),
    )
    waiting_list = schema.Tuple(
        title=_(u"Waiting list"),
        description=_(
            u"List of people in waiting list that compiled email fields "
            u"(could be one of these fields: email, e-mail, replyto)."),
        required=False,
        default=(),
        missing_value=(),
        value_type=schema.Choice(
            source="collective.easyformplugin.registration.waiting_list"),
    )
Ejemplo n.º 7
0
 def extract_recipients(self, registrants, waiting_list):
     emails = []
     registrants_action = None
     for name, action in getFieldsInOrder(get_actions(self.context)):
         if IRegistrantData.providedBy(action) and action.required:
             registrants_action = action
     if not registrants_action:
         return _(
             "contact_registrants_action_not_found",
             default=u"Unable to send messages. Action not found in Form.",
         )
     for registrant in registrants_action._storage.values():
         if (registrant.get("id") in registrants
                 or registrant.get("id") in waiting_list):
             email = (registrant.get("email", "")
                      or registrant.get("e-mail", "")
                      or registrant.get("replyto", ""))
             emails.append(email)
     return set(emails)
class IRegistrationForm(model.Schema):
    """Behavior interface to make a EasyForm type support registration form.
    """

    is_registration_form = schema.Bool(
        title=_(u"label_is_registration_form", default=u"Registration form"),
        description=_(
            "help_is_registration_form",
            default=u"Select if this form is a registration form. "
            u"If selected, will be created a new form action (registrants) and"
            u" a new hidden field (waiting_list). If one of these two defaults"
            u" is missing, the registration form can't work properly.",
        ),
    )

    max_attendees = schema.Int(
        title=_(u"label_max_attendees", default=u"Max attendees"),
        description=_(
            "help_max_attendees",
            default=u"Set a maximum number of attendees for this registration"
            u" form. Leave empty for unlimited.",
        ),
        required=False,
    )

    waiting_list_size = schema.Int(
        title=_(u"label_waiting_list_size", default=u"Waiting list size"),
        description=_(
            "help_waiting_list_size",
            default=u"Set a number of available seats in waiting list."
            u" Leave empty for disable the waiting list.",
        ),
        required=False,
    )

    show_seats_left = schema.Bool(
        title=_(u"label_show_seats_left", default=u"Show seats left"),
        description=_(
            "help_show_seats_left",
            default=u"Allows to show in the subscription page how many seats "
            u"are still available. Waiting list seats are not counted.",
        ),
    )

    open_date = schema.Datetime(
        title=_(u"label_subscription_open_date", default=u"Open date"),
        description=_(
            u"help_subscription_open_date",
            default=u"Date and Time, when the registration form opens.",
        ),
        required=False,
        # defaultFactory=default_open_date,
    )
    directives.widget("open_date",
                      DatetimeFieldWidget,
                      default_timezone=default_timezone)

    close_date = schema.Datetime(
        title=_(u"label_subscription_close_date", default=u"Close date"),
        description=_(
            u"help_subscription_close_date",
            default=u"Date and Time, when the registration form closes.",
        ),
        required=False,
        # defaultFactory=default_close_date,
    )
    directives.widget("close_date",
                      DatetimeFieldWidget,
                      default_timezone=default_timezone)

    fieldset(
        "Registration",
        label=_(u"Registration"),
        fields=[
            "is_registration_form",
            "max_attendees",
            "waiting_list_size",
            "show_seats_left",
            "open_date",
            "close_date",
        ],
    )
Ejemplo n.º 9
0
class ContactRegistrantsForm(form.Form):
    """
    Contact registrants form
    """

    ignoreContext = True
    css_class = "contact-registrants"
    fields = field.Fields(IContactRegistrantsForm)

    fields["registrants"].widgetFactory = CheckBoxFieldWidget
    fields["waiting_list"].widgetFactory = CheckBoxFieldWidget

    @button.buttonAndHandler(_(u"Submit"), name="submit")
    def handleSubmit(self, action):
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return
        registrants = data.get("registrants", [])
        waiting_list = data.get("waiting_list", [])
        if not registrants and not waiting_list:
            api.portal.show_message(
                message=_(
                    "contact_recipients_error",
                    default=u"You need to select at least one recipient from"
                    u" registrants or waiting_list list.",
                ),
                request=self.request,
                type="error",
            )
            return
        email_addresses = self.extract_recipients(registrants, waiting_list)
        if isinstance(email_addresses, str):
            api.portal.show_message(message=email_addresses,
                                    request=self.request,
                                    type="error")
            return
        try:
            self.send_message(data=data, email_addresses=email_addresses)
            api.portal.show_message(
                message=_("contact_recipients_sent", u"Emails sent."),
                request=self.request,
            )
            self.request.response.redirect(self.nextURL())
        except (SMTPException, RuntimeError) as e:
            logger.exception(e)
            plone_utils = api.portal.get_tool(name="plone_utils")
            exception = plone_utils.exceptionString()
            message = pmf(
                u"Unable to send mail: ${exception}",
                mapping={u"exception": exception},
            )
            api.portal.show_message(message=message,
                                    request=self.request,
                                    type="error")

    @button.buttonAndHandler(_(u"Reset"), name="reset")
    def handleReset(self, action):
        self.request.response.redirect(self.nextURL())

    def nextURL(self):
        return self.context.absolute_url()

    def extract_recipients(self, registrants, waiting_list):
        emails = []
        registrants_action = None
        for name, action in getFieldsInOrder(get_actions(self.context)):
            if IRegistrantData.providedBy(action) and action.required:
                registrants_action = action
        if not registrants_action:
            return _(
                "contact_registrants_action_not_found",
                default=u"Unable to send messages. Action not found in Form.",
            )
        for registrant in registrants_action._storage.values():
            if (registrant.get("id") in registrants
                    or registrant.get("id") in waiting_list):
                email = (registrant.get("email", "")
                         or registrant.get("e-mail", "")
                         or registrant.get("replyto", ""))
                emails.append(email)
        return set(emails)

    def generate_mail(self, variables, encoding="utf-8"):
        template = api.content.get_view(
            name="contact-registrants-email",
            context=self.context,
            request=self.request,
        )
        return template(self.context, **variables).encode(encoding)

    def send_message(self, data, email_addresses):
        subject = data.get("subject")

        registry = getUtility(IRegistry)
        mail_settings = registry.forInterface(IMailSchema, prefix="plone")
        send_to_address = mail_settings.email_from_address
        from_address = mail_settings.email_from_address
        registry = getUtility(IRegistry)
        encoding = registry.get("plone.email_charset", "utf-8")
        host = api.portal.get_tool(name="MailHost")

        message = self.generate_mail(data, encoding)
        message = MIMEText(message, "plain", encoding)
        for send_to_address in email_addresses:
            # This actually sends out the mail
            host.send(
                message,
                send_to_address,
                from_address,
                subject=subject,
                charset=encoding,
            )

    def get_package_version(self):
        return pkg_resources.get_distribution(
            "collective.easyformplugin.registration").version