~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/mail/notifications.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-12-22 04:45:35 UTC
  • mfrom: (14565.2.24 apocalyptic-pieces)
  • Revision ID: launchpad@pqm.canonical.com-20111222044535-jbjyzq3hzwiy7g20
[rs=sinzui][no-qa] Move javascript and scripts to lp. Dismantle
 mailnotification.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Event handlers that send email notifications."""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
from zope.component import getUtility
 
9
 
 
10
from canonical.config import config
 
11
from canonical.database.sqlbase import block_implicit_flushes
 
12
from canonical.launchpad.helpers import get_email_template
 
13
from canonical.launchpad.webapp.interfaces import ILaunchpadRoot
 
14
from canonical.launchpad.webapp.publisher import canonical_url
 
15
from lp.services.mail.mailwrapper import MailWrapper
 
16
from lp.services.mail.sendmail import (
 
17
    format_address,
 
18
    simple_sendmail,
 
19
    )
 
20
 
 
21
CC = "CC"
 
22
 
 
23
 
 
24
@block_implicit_flushes
 
25
def notify_new_ppa_subscription(subscription, event):
 
26
    """Notification that a new PPA subscription can be activated."""
 
27
    non_active_subscribers = subscription.getNonActiveSubscribers()
 
28
 
 
29
    archive = subscription.archive
 
30
 
 
31
    # We don't send notification emails for commercial PPAs as these
 
32
    # are purchased via software center (and do not mention Launchpad).
 
33
    if archive.commercial:
 
34
        return
 
35
 
 
36
    registrant_name = subscription.registrant.displayname
 
37
    ppa_displayname = archive.displayname
 
38
    ppa_reference = "ppa:%s/%s" % (
 
39
        archive.owner.name, archive.name)
 
40
    ppa_description = archive.description
 
41
    subject = 'PPA access granted for ' + ppa_displayname
 
42
 
 
43
    template = get_email_template('ppa-subscription-new.txt', app='soyuz')
 
44
 
 
45
    for person, preferred_email in non_active_subscribers:
 
46
        to_address = [preferred_email.email]
 
47
        root = getUtility(ILaunchpadRoot)
 
48
        recipient_subscriptions_url = "%s~/+archivesubscriptions" % (
 
49
            canonical_url(root))
 
50
        description_blurb = '.'
 
51
        if ppa_description is not None and ppa_description != '':
 
52
            description_blurb = (
 
53
                ' and has the following description:\n\n%s' % ppa_description)
 
54
        replacements = {
 
55
            'recipient_name': person.displayname,
 
56
            'registrant_name': registrant_name,
 
57
            'registrant_profile_url': canonical_url(subscription.registrant),
 
58
            'ppa_displayname': ppa_displayname,
 
59
            'ppa_reference': ppa_reference,
 
60
            'ppa_description_blurb': description_blurb,
 
61
            'recipient_subscriptions_url': recipient_subscriptions_url,
 
62
            }
 
63
        body = MailWrapper(72).format(template % replacements,
 
64
                                      force_wrap=True)
 
65
 
 
66
        from_address = format_address(
 
67
            registrant_name, config.canonical.noreply_from_address)
 
68
 
 
69
        headers = {
 
70
            'Sender': config.canonical.bounce_address,
 
71
            }
 
72
 
 
73
        # If the registrant has a preferred email, then use it for the
 
74
        # Reply-To.
 
75
        if subscription.registrant.preferredemail:
 
76
            headers['Reply-To'] = format_address(
 
77
                registrant_name,
 
78
                subscription.registrant.preferredemail.email)
 
79
 
 
80
        simple_sendmail(from_address, to_address, subject, body, headers)