~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/answers/notification.py

  • Committer: Curtis Hovey
  • Date: 2011-04-29 17:46:12 UTC
  • mto: This revision was merged to the branch mainline in revision 12976.
  • Revision ID: curtis.hovey@canonical.com-20110429174612-n39hrsoleimarw2j
Remove obsolete email methods.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
from zope.component import getUtility
14
14
 
15
15
from canonical.config import config
16
 
from canonical.launchpad.mail import (
17
 
    format_address,
18
 
    simple_sendmail,
19
 
    )
20
16
from canonical.launchpad.webapp.publisher import canonical_url
21
17
from lp.answers.enums import (
22
18
    QuestionAction,
25
21
from lp.answers.interfaces.questionjob import IQuestionEmailJobSource
26
22
from lp.registry.interfaces.person import IPerson
27
23
from lp.services.mail.mailwrapper import MailWrapper
28
 
from lp.services.mail.notificationrecipientset import NotificationRecipientSet
29
24
from lp.services.propertycache import cachedproperty
30
25
 
31
26
 
68
63
        """Return the user from the event. """
69
64
        return self._user
70
65
 
71
 
    def getFromAddress(self):
72
 
        """Return a formatted email address suitable for user in the From
73
 
        header of the question notification.
74
 
 
75
 
        Default is Event Person Display Name <question#@answertracker_domain>
76
 
        """
77
 
        return format_address(
78
 
            self.user.displayname,
79
 
            'question%s@%s' % (
80
 
                self.question.id, config.answertracker.email_domain))
81
 
 
82
66
    def getSubject(self):
83
67
        """Return the subject of the notification.
84
68
 
123
107
 
124
108
        return headers
125
109
 
126
 
    def getRecipients(self):
127
 
        """Return the recipient of the notification.
128
 
 
129
 
        Default to the question's subscribers that speaks the request
130
 
        languages. If the question owner is subscribed, he's always consider
131
 
        to speak the language.
132
 
 
133
 
        :return: A `INotificationRecipientSet` containing the recipients and
134
 
                 rationale.
135
 
        """
136
 
        return self.question.getRecipients()
137
 
 
138
110
    def initialize(self):
139
111
        """Initialization hook for subclasses.
140
112
 
153
125
        """
154
126
        return True
155
127
 
156
 
    def buildBody(self, body, rationale):
157
 
        """Wrap the body and ensure the rationale is is separated."""
158
 
        wrapper = MailWrapper()
159
 
        body_parts = [body, wrapper.format(rationale)]
160
 
        if '\n-- ' not in body:
161
 
            body_parts.insert(1, '-- ')
162
 
        return '\n'.join(body_parts)
163
 
 
164
128
    def enqueue(self):
165
129
        """Create a job to send email about the event."""
166
130
        subject = self.getSubject()
172
136
            subject, body, headers)
173
137
        return job
174
138
 
175
 
    def send(self):
176
 
        """Sends the notification to all the notification recipients.
177
 
 
178
 
        This method takes care of adding the rationale for contacting each
179
 
        recipient and also sets the X-Launchpad-Message-Rationale header on
180
 
        each message.
181
 
        """
182
 
        from_address = self.getFromAddress()
183
 
        subject = self.getSubject()
184
 
        body = self.getBody()
185
 
        headers = self.getHeaders()
186
 
        recipients = self.getRecipients()
187
 
        for email in recipients.getEmails():
188
 
            rationale, header = recipients.getReason(email)
189
 
            headers['X-Launchpad-Message-Rationale'] = header
190
 
            formatted_body = self.buildBody(body, rationale)
191
 
            simple_sendmail(
192
 
                from_address, email, subject, formatted_body, headers)
193
 
 
194
139
    @property
195
140
    def unsupported_language(self):
196
141
        """Whether the question language is unsupported or not."""
370
315
 
371
316
        return get_email_template(self.body_template) % replacements
372
317
 
373
 
    def getRecipients(self):
374
 
        """The default notification goes to all question subscribers that
375
 
        speak the request language, except the owner.
376
 
        """
377
 
        original_recipients = QuestionNotification.getRecipients(self)
378
 
        recipients = NotificationRecipientSet()
379
 
        for person in original_recipients:
380
 
            if person != self.question.owner:
381
 
                rationale, header = original_recipients.getReason(person)
382
 
                recipients.add(person, rationale, header)
383
 
        return recipients
384
 
 
385
318
    # Header template used when a new message is added to the question.
386
319
    action_header_template = {
387
320
        QuestionAction.REQUESTINFO:
450
383
            self.body_template = self.body_template_by_action.get(
451
384
                self.new_message.action, self.body_template)
452
385
 
453
 
    def getRecipients(self):
454
 
        """Return the owner of the question if he's still subscribed."""
455
 
        recipients = NotificationRecipientSet()
456
 
        owner = self.question.owner
457
 
        original_recipients = self.question.direct_recipients
458
 
        if owner in self.question.direct_recipients:
459
 
            rationale, header = original_recipients.getReason(owner)
460
 
            recipients.add(owner, rationale, header)
461
 
        return recipients
462
 
 
463
386
    def getBody(self):
464
387
        """See QuestionNotification."""
465
388
        body = QuestionModifiedDefaultNotification.getBody(self)
483
406
        """Return True when the question is in an unsupported language."""
484
407
        return self.unsupported_language
485
408
 
486
 
    def getRecipients(self):
487
 
        """Notify only the answer contacts."""
488
 
        return self.question.target.getAnswerContactRecipients(None)
489
 
 
490
409
    def getBody(self):
491
410
        """See QuestionNotification."""
492
411
        question = self.question