def __call__(self):
        was_activated = self.newsletter_activated
        self.newsletter_activated = not was_activated

        if was_activated:
            msgid = _(u"newsletter_disabled", default="Newsletter functionality disabled.")
        else:
            msgid = _(u"newsletter_enabled", default="Newsletter functionality enabled.")

        translated = self.context.translate(msgid)
        self.messages.addStatusMessage(translated, type="info")

        response = self.request.response
        response.redirect(self.context.absolute_url())
    def __call__(self):
        submitted = self.request.get('form.submitted')

        if submitted == 'add':
            # add new mailinglist
            name = self.request.get('name', None)
            email = self.request.get('email', None)
            protocol = self.request.get('protocol', None)

            if name and email and protocol:
                self.mailinglist_manager.add(name, email, protocol)
            else:
                msgid = _(u"config_add_mailinglist_failed",
                    default="Could not add new mailing list. Please fill out\
                    all fields.")
                translated = self.context.translate(msgid)
                self.messages.addStatusMessage(translated, type="error")

        if submitted == 'remove':
            lists = self.request.get('remove', [])
            for list_id in lists:
                self.mailinglist_manager.remove(list_id)

        return self.index()
    def subscribe(self):
        # TODO: in status messages, give a hint to which newsletters the user
        #       subscribed
        email = self.request.get('subemail', None)
        unsubscribe = self.request.get('unsubscribe', None) and True or False
        
        selected_lists = self.request.get('mailinglists', None)
        
        if len(self.mailinglists) == 1:
            # self.mailinglists is persistent list
            selected_lists = self.mailinglists
        else:
            if selected_lists\
               and not isinstance(selected_lists, list)\
               and not isinstance(selected_lists, tuple):
                selected_lists = [selected_lists]

        if selected_lists:
            for selected_list in selected_lists:
                selected_list_info = self.mailinglist_manager.\
                                   get_mailinglist_by_id(selected_list['id'])
                api = utils.get_list_api(selected_list_info['protocol'])
                if not unsubscribe:
                    try:
                        self._subscribe(email, selected_list_info['email'], api)
                        msgid = _(u"newsletter_signup_successful",
                            default="You've successfully subscribed to the list.")
                        msgtype = "info"
                    except ValidationError:
                        msgid = _(u"newsletter_signup_validation_error",
                            default="Signup failed. Please verify email-address\
                            (you submitted '${subemail}').",
                            mapping={u'subemail': email})
                        msgtype = "error"
                    except:
                        msgid = _(u"newsletter_signup_failed",
                            default="Signup failed. We're unable to process your\
                            request due to an internal error. If the problem\
                            persists, please contact the site administrator.")
                        msgtype = "error"
                        # write traceback to the logfile
                        import traceback
                        traceback.print_exc()
                else:
                    try:
                        self._unsubscribe(email, selected_list_info['email'], api)
                        msgid = _(u"newsletter_signoff_successful",
                            default="You've successfully unsubscribed from the\
                            list.")
                        msgtype = "info"
                    except ValidationError:
                        msgid = _(u"newsletter_signoff_validation_error",
                            default="Sigoff failed. Please verify email-address\
                            (you submitted '${subemail}').",
                            mapping={'subemail': email})
                        msgtype = "error"
                    except:
                        msgid = _(u"newsletter_signoff_failed",
                            default="Signoff failed. We're unable to process your\
                            request due to an internal error. If the problem\
                            persists, please contact the site administrator.")
                        msgtype = "error"
                        # write traceback to the logfile
                        import traceback
                        traceback.print_exc()

                translated = self.context.translate(msgid)
                self.messages.addStatusMessage(translated, type=msgtype)

        if not selected_lists:
            msgid = _(u"newsletter_no_selected_list",
                      default="Please select a newsletter.")
            msgtype = "error"
            translated = self.context.translate(msgid)
            self.messages.addStatusMessage(translated, type=msgtype)


        came_from = self.request.get('came_from', self.context.absolute_url())
        self.request.RESPONSE.redirect(came_from)
 def title(self):
     return _(u"Newsletter Subscription Portlet")
    def __call__(self):
        """
        Depending on mode-parameter, send newsletter to either a single
        recipient, the mailinglist or return a preview.

        If mode is not set, just display form.
        """

        ## POST DOES NOT WORK WITH CSS FILES AS BROWSER RESOURCES
        ## so we have to use get instead
        #assert self.request['REQUEST_METHOD'] == 'POST'

        mode = self.request.get('mode', None)

        # MSN Hotmail and various older e-mail clients do not support UTF-8
        # We opt for latin-1 for the time being for maximum compatibility

        # encoding = getToolByName(self.context,
        #                          'plone_utils').getSiteEncoding()
        encoding = 'latin-1'
        self.request.response.setHeader('Content-Type', 'text/html;charset=%s'
                                        % encoding)

        if mode == 'preview':
            self.request.response.redirect('@@view-as-newsletter')
        try:
            renderer = self.context.restrictedTraverse('@@view-as-newsletter')
            title = self.context.title_or_id()

            if mode == 'single':
                email = self.request.get('email')
                self._send_newsletter(email,
                                      renderer,
                                      title=title,
                                      encoding=encoding)

                msgid = _(u"newsletter_sent_single",
                          default=u"Newsletter sent to a single recipient.")
                translated = self.context.translate(msgid)
                self.messages.addStatusMessage(translated, type="info")

            elif mode == 'send':
                list_id = self.request.get('list')
                mlist = self.mailinglist_manager.get_mailinglist_by_id(list_id)
                email = mlist['email']

                # set up unsubscription parameters
                api = utils.get_list_api(mlist['protocol'])
                unsubscribe_email, message, subject = api.unsubscribe(email)
                self.request['unsubscribe_email'] = unsubscribe_email
                self.request['unsubscribe_message'] = message
                self.request['unsubscribe_subject'] = subject

                self._send_newsletter(email,
                                      renderer,
                                      title=title,
                                      encoding=encoding)

                msgid = _(u"newsletter_sent",
                          default=u"Newsletter sent.")
                translated = self.context.translate(msgid)
                self.messages.addStatusMessage(translated, type="info")

        except ValidationError:
            msgid = _(u"send_newsletter_validation_error",
                default=u"Failed sending newsletter. Please verify\
                email-address (you submitted '${email}.",
                mapping={u'email': email})
            translated = self.context.translate(msgid)
            self.messages.addStatusMessage(translated, type="error")
        except:
            msgid = _(u"send_newsletter_failed",
                default="Failed sending newsletter. We're unable to process\
                your request due to an internal error. If the problem persists,\
                please contact the site administrator.")
            translated = self.context.translate(msgid)
            self.messages.addStatusMessage(translated, type="error")

            # write traceback to the logfile
            import traceback
            traceback.print_exc()

        return self.index()