Ignore:
Timestamp:
4 Nov 2020, 17:52:22 (4 years ago)
Author:
Henrik Bettermann
Message:

Implement bulk emailing.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser.py

    r16286 r16299  
    3636from zope.security import checkPermission
    3737from zope.securitypolicy.interfaces import IPrincipalRoleManager
     38from waeup.kofa.smtp import encode_address
    3839from waeup.kofa.accesscodes import invalidate_accesscode, get_access_code
    3940from waeup.kofa.accesscodes.workflow import USED
     
    309310    search_button = _('Find student(s)')
    310311    remove_button = _('Remove selected')
     312    send_email_button = _('Send bulk email')
    311313    doclink = DOCLINK + '/students.html'
    312314
     
    340342            if not self.hitlist:
    341343                self.flash(_('No student found.'), type="warning")
    342             if 'remove' in form:
     344            if 'remove' in form or 'send_email' in form:
    343345                self.flash(_('No item selected.'), type="warning")
    344346            return
     
    346348        if isinstance(entries, basestring):
    347349            entries = [entries]
     350        entries.sort()
    348351        deleted = []
     352        emails_tobesent = ''
    349353        for entry in entries:
    350354            if 'remove' in form:
    351355                del self.context[entry]
    352356                deleted.append(entry)
     357            if 'send_email' in form and self.context[entry].email:
     358                emails_tobesent += '%s,' % encode_address(
     359                    self.context[entry].email,
     360                    self.context[entry].student_id)
     361        if 'send_email' in form and not len(emails_tobesent):
     362            self.flash(_('No email address found.'), type="warning")
     363            return
     364        if len(emails_tobesent):
     365            args = {'bcc_to': emails_tobesent.strip(',')}
     366            self.redirect(self.url(self.context) +
     367                '/send_bulk_email?%s' % urlencode(args))
     368            return
    353369        self.hitlist = search(query=self.searchterm,
    354370            searchtype=self.searchtype, view=self)
     
    521537                self.config.name,
    522538                data['body'],data['subject'])
     539        if success:
     540            self.flash(_('Your message has been sent.'))
     541        else:
     542            self.flash(_('An smtp server error occurred.'), type="danger")
     543        return
     544
     545class SendBulEmailsFormPage(ContactAdminFormPage):
     546    grok.context(IStudentsContainer)
     547    grok.name('send_bulk_email')
     548    grok.require('waeup.manageStudent')
     549    pnav = 4
     550    form_fields = grok.AutoFields(IContactForm).select('subject', 'body', 'bcc_to')
     551    label = _('Send bulk email')
     552
     553    def update(self, bcc_to=u''):
     554        self.form_fields.get('bcc_to').field.default = bcc_to
     555        return
     556
     557    @action('Send message now', style='primary')
     558    def send(self, *args, **data):
     559        try:
     560            email = self.request.principal.email
     561        except AttributeError:
     562            email = self.config.email_admin
     563        usertype = getattr(self.request.principal,
     564                           'user_type', 'system').title()
     565        kofa_utils = getUtility(IKofaUtils)
     566        success = kofa_utils.sendContactForm(
     567                self.request.principal.title,email,
     568                self.request.principal.title,email, # sent to the sender's address
     569                self.request.principal.id,usertype,
     570                self.config.name,
     571                data['body'],data['subject'],
     572                data['bcc_to'])
    523573        if success:
    524574            self.flash(_('Your message has been sent.'))
  • main/waeup.kofa/trunk/src/waeup/kofa/students/browser_templates/containermanagepage.pt

    r15417 r16299  
    7575    <input type="submit" name="remove"
    7676           tal:attributes="value view/remove_button" class="btn btn-default"
    77            onclick="return confirmPost('Are you sure?')"/>
     77           onclick="return window.confirm('Are you sure?')"/>
     78    <input type="submit" name="send_email"
     79           tal:attributes="value view/send_email_button" class="btn btn-default"
     80           onclick="return window.confirm('Are you sure?')"/>
    7881
    7982  </div>
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py

    r16266 r16299  
    9090    def setUp(self):
    9191        super(StudentsFullSetup, self).setUp()
     92        self.setup_logging()
    9293
    9394        # Setup a sample site for each test
     
    252253
    253254    layer = FunctionalLayer
     255
     256    def setup_logging(self):
     257        # setup a log-handler that catches all fake mailer output
     258        self.stream = StringIO()
     259        handler = logging.StreamHandler(self.stream)
     260        logger = logging.getLogger('test.smtp')
     261        logger.addHandler(handler)
     262        logger.setLevel(logging.INFO)
     263        return
     264
     265    def get_fake_smtp_output(self):
     266        # get output generated by fake mailer
     267        self.stream.flush()
     268        self.stream.seek(0)
     269        return self.stream.read()
    254270
    255271    def test_anonymous_access(self):
     
    432448        self.assertTrue('No student found' in self.browser.contents)
    433449        return
     450
     451    def test_send_bulk_emails(self):
     452        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
     453        self.browser.open(self.manage_container_path)
     454        self.browser.getLink("Add student").click()
     455        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     456        self.assertEqual(self.browser.url, self.add_student_path)
     457        self.browser.getControl(name="form.firstname").value = 'Bob'
     458        self.browser.getControl(name="form.lastname").value = 'Tester'
     459        self.browser.getControl(name="form.reg_number").value = '1234'
     460        self.browser.getControl("Create student").click()
     461        self.assertTrue('Student record created' in self.browser.contents)
     462        self.browser.getLink("Manage").click()
     463        self.browser.getControl(name="form.email").value = 'uli@uni.ng'
     464        self.browser.getControl(name="form.sex").value = ['m']
     465        self.browser.getControl("Save").click()
     466        # We can find Anna and Bob
     467        self.browser.open(self.manage_container_path)
     468        self.browser.getControl(name="searchtype").value = ['student_id']
     469        self.browser.getControl(name="searchterm").value = '*'
     470        self.browser.getControl("Find student(s)").click()
     471        self.assertTrue('Anna Tester' in self.browser.contents)
     472        self.assertTrue('Bob Tester' in self.browser.contents)
     473        # We can send bulk emails
     474        ctrl = self.browser.getControl(name='entries')
     475        ctrl.getControl(value='K1000000').selected = True
     476        ctrl.getControl(value='K1000001').selected = True
     477        self.browser.getControl("Send bulk", index=0).click()
     478        self.assertTrue('K1000000 &lt;aa@aa.ng&gt;,K1000001 &lt;uli@uni.ng&gt;'
     479            in self.browser.contents)
     480        self.browser.getControl(name="form.subject").value = 'Test'
     481        self.browser.getControl(name="form.body").value = 'Hello world'
     482        self.browser.getControl("Send message").click()
     483        self.assertTrue('Your message has been sent' in self.browser.contents)
     484        self.assertMatches(
     485        u'Sending email from no-reply@waeup.org to contact@waeup.org:\n'
     486        u'Message:\n'
     487        u'msg: MIME-Version: 1.0\n'
     488        u'msg: Content-Type: text/plain; charset="us-ascii"\n'
     489        u'msg: Content-Transfer-Encoding: 7bit\n'
     490        u'msg: From: Manager <no-reply@waeup.org>\n'
     491        u'msg: To: Manager <contact@waeup.org>\n'
     492        u'msg: Bcc: K1000000 <aa@aa.ng>,K1000001 <uli@uni.ng>\n'
     493        u'msg: Reply-To: Manager <contact@waeup.org>\n'
     494        u'msg: Subject: Test\n'
     495        u'msg: \n'
     496        u'msg: Hello world\n'
     497        u'msg: \n'
     498        u'msg: ---\n'
     499        u'msg: Manager (id: zope.mgr)\n'
     500        u'msg: Sample University\n'
     501        u'msg: \n',
     502            self.get_fake_smtp_output()
     503            )
    434504
    435505    def test_add_graduated_students(self):
Note: See TracChangeset for help on using the changeset viewer.