def handle_error(self, error, data): if error.code == 400: error_msg = _(u"mailinglijst_error_msg_already_subscribed", default=u"Could not subscribe to newsletter. " u"Either the email '${email}' is already subscribed " u"or something else is wrong. Try again later.", mapping={u"email": data['email']}) translated_error_msg = self.context.translate(error_msg) raise WidgetActionExecutionError('email', Invalid(translated_error_msg)) elif error.code == 220: error_msg = _(u"mailinglijst_error_msg_banned", default=u"Could not subscribe to newsletter. " u"The email '${email}' has been banned.", mapping={u"email": data['email']}) translated_error_msg = self.context.translate(error_msg) raise WidgetActionExecutionError('email', Invalid(translated_error_msg)) else: error_msg = _(u"mailinglijst_error_msg", default=u"Could not subscribe to newsletter. " u"Please contact the site administrator: " u"'${error}'", mapping={u"error": error}) translated_error_msg = self.context.translate(error_msg) raise ActionExecutionError(Invalid(translated_error_msg))
class INewsletterSubscribe(Interface): email = schema.TextLine(title=_(u"Email address"), description=_( u"help_email", default=u"Please enter your email address."), required=True, constraint=validate_email) """interest_groups = schema.Tuple( title=_(u"Interest groups"), description=_( u"help_interest_groups", default=u"" ), value_type=schema.Choice( vocabulary="collective.mailinglijst.vocabularies.InterestGroups", ), required=False, ) email_type = schema.Choice( title=_(u"Mail format"), vocabulary="collective.mailinglijst.vocabularies.EmailType", default="text", required=False, )""" list_id = schema.TextLine(title=_(u"List ID"), required=False)
def handleApply(self, action): data, errors = self.extractData() if 'email' not in data: return mailinglijst = getUtility(IMailinglijstLocator) # Retrieve list_id either from a hidden field in the form or fetch # the first list from mailinglijst. if 'list_id' in data and data['list_id'] is not None: list_id = data['list_id'] else: list_id = mailinglijst.default_list_id() # Use email_type if one is provided by the form, if not choose the # default email type from the control panel settings. """if 'email_type' in data: email_type = data['email_type'] else: email_type = 'HTML'""" # Subscribe to Mailinglijst list try: mailinglijst.subscribe(list_id=list_id, email_address=data['email']) except MailinglijstException as error: return self.handle_error(error, data) registry = getUtility(IRegistry) mailinglijst_settings = registry.forInterface(IMailinglijstSettings) if mailinglijst_settings.double_optin: message = _( u"We have to confirm your email address. In order to " + u"finish the newsletter subscription, click on the link " + u"inside the email we just send you.") else: message = _( u"You have been subscribed to our newsletter succesfully.") IStatusMessage(self.context.REQUEST).addStatusMessage(message, type="info") portal = getSite() self.request.response.redirect(portal.absolute_url())
class NewsletterSubscriberForm(extensible.ExtensibleForm, form.Form): fields = field.Fields(INewsletterSubscribe) ignoreContext = True id = "newsletter-subscriber-form" label = _(u"Subscribe to newsletter") def updateActions(self): super(NewsletterSubscriberForm, self).updateActions() self.actions['subscribe'].addClass('context') def updateFields(self): super(NewsletterSubscriberForm, self).updateFields() """self.fields['email_type'].widgetFactory = \ RadioFieldWidget""" def updateWidgets(self): super(NewsletterSubscriberForm, self).updateWidgets() # Show/hide mail format option widget registry = getUtility(IRegistry) mailinglijst_settings = registry.forInterface(IMailinglijstSettings) """if not mailinglijst_settings.email_type_is_optional: self.widgets['email_type'].mode = HIDDEN_MODE""" # Retrieve the list id either from the request/form or fall back to # the default_list setting. if 'list_id' in self.context.REQUEST: list_id = self.context.REQUEST['list_id'] elif 'form.widgets.list_id' in self.request.form: list_id = self.request.form['form.widgets.list_id'] else: list_id = mailinglijst_settings.default_list self.widgets['list_id'].mode = HIDDEN_MODE self.widgets['list_id'].value = list_id @button.buttonAndHandler(_(u"subscribe_to_newsletter_button", default=u"Subscribe"), name='subscribe') def handleApply(self, action): data, errors = self.extractData() if 'email' not in data: return mailinglijst = getUtility(IMailinglijstLocator) # Retrieve list_id either from a hidden field in the form or fetch # the first list from mailinglijst. if 'list_id' in data and data['list_id'] is not None: list_id = data['list_id'] else: list_id = mailinglijst.default_list_id() # Use email_type if one is provided by the form, if not choose the # default email type from the control panel settings. """if 'email_type' in data: email_type = data['email_type'] else: email_type = 'HTML'""" # Subscribe to Mailinglijst list try: mailinglijst.subscribe(list_id=list_id, email_address=data['email']) except MailinglijstException as error: return self.handle_error(error, data) registry = getUtility(IRegistry) mailinglijst_settings = registry.forInterface(IMailinglijstSettings) if mailinglijst_settings.double_optin: message = _( u"We have to confirm your email address. In order to " + u"finish the newsletter subscription, click on the link " + u"inside the email we just send you.") else: message = _( u"You have been subscribed to our newsletter succesfully.") IStatusMessage(self.context.REQUEST).addStatusMessage(message, type="info") portal = getSite() self.request.response.redirect(portal.absolute_url()) def handle_error(self, error, data): if error.code == 400: error_msg = _(u"mailinglijst_error_msg_already_subscribed", default=u"Could not subscribe to newsletter. " u"Either the email '${email}' is already subscribed " u"or something else is wrong. Try again later.", mapping={u"email": data['email']}) translated_error_msg = self.context.translate(error_msg) raise WidgetActionExecutionError('email', Invalid(translated_error_msg)) elif error.code == 220: error_msg = _(u"mailinglijst_error_msg_banned", default=u"Could not subscribe to newsletter. " u"The email '${email}' has been banned.", mapping={u"email": data['email']}) translated_error_msg = self.context.translate(error_msg) raise WidgetActionExecutionError('email', Invalid(translated_error_msg)) else: error_msg = _(u"mailinglijst_error_msg", default=u"Could not subscribe to newsletter. " u"Please contact the site administrator: " u"'${error}'", mapping={u"error": error}) translated_error_msg = self.context.translate(error_msg) raise ActionExecutionError(Invalid(translated_error_msg))
class NotAnEmailAddress(schema.ValidationError): __doc__ = _(u"Invalid email address")
class IMailinglijstSettings(Interface): """Global mailinglijst settings. This describes records stored in the configuration registry and obtainable via plone.registry. """ api_key = schema.TextLine( title=_(u"Mailinglijst API Key"), description=_( u"help_api_key", default=u"Enter in your Mailinglijst key here (.e.g. " + u"'8b785dcabe4b5aa24ef84201ea7dcded-us4'). Log into " + u"mailinglijst.com, go to account -> extras -> API Keys & " + u"Authorized Apps and copy the API Key to this field."), default=u"", required=True) default_list = schema.Choice( title=_(u"default_list"), description=_( u"help_default_list", default=u"Default list which is used in the @@newsletter view if " u"no list_id param is provided."), vocabulary="collective.mailinglijst.vocabularies.AvailableLists", required=False, ) double_optin = schema.Bool( title=_(u"double_optin"), description=_( u"help_double_optin", default=u"Flag to control whether a double opt-in confirmation " u"message is sent, defaults to true. Abusing this may " u"cause your account to be suspended."), required=True, default=True) """email_type = schema.Choice( title=_(u"email_type"), description=_( u"help_email_type", default=u"Email type preference for the email (html, text, or " u"mobile defaults to html)"), vocabulary="collective.mailinglijst.vocabularies.EmailType", default="html", required=True, )""" """email_type_is_optional = schema.Bool( title=_(u"email_type_is_optional"), description=_( u"help_email_type_is_optional", default=u"Let users choose their email type preference in the " u"newsletter subscription form." ), required=True, default=False )""" """update_existing = schema.Bool( title=_(u"update_existing"), description=_( u"help_update_existing", default=u"Flag to control whether existing subscribers should be " u"updated instead of throwing an error, defaults to false" ), required=True, default=False ) replace_interests = schema.Bool( title=_(u"replace_interests"), description=_( u"help_replace_interests", default=u"Flag to determine whether we replace the interest " u"groups with the groups provided or we add the provided" u"groups to the member's interest groups (optional, " u"defaults to true)" ), required=True, default=True )""" """send_welcome = schema.Bool( title=_(u"send_welcome"), description=_( u"help_send_welcome", default=u"If your double_optin is false and this is true, we " u"will send your lists Welcome Email if this subscribe " u"succeeds - this will *not* fire if we end up updating " u"an existing subscriber. If double_optin is true, this " u"has no effect. defaults to false." ), required=True, default=False )""" @invariant def valid_api_key(data): if len(data.api_key) == 0: return parts = data.api_key.split('-') if len(parts) != 5: raise Invalid( u"Your Mailinglijst API key is not valid. Please go " + u"to mailinglijst.com and check your API key.")