~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.7 by Karl Fogel
Add the copyright header block to more files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
4935.3.7 by Curtis Hovey
Added bad name suppression to cronscripts.
6
# pylint: disable-msg=C0103,W0403
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
7
8
"""Send bug notifications.
9
10
This script sends out all the pending bug notifications, and sets
11
date_emailed to the current date.
12
"""
13
14
__metaclass__ = type
15
16
import _pythonpath
17
18
from zope.component import getUtility
19
14605.1.1 by Curtis Hovey
Moved canonical.config to lp.services.
20
from lp.services.config import config
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
21
from canonical.database.constants import UTC_NOW
13668.1.23 by Curtis Hovey
Fixed imports that relied on globs.
22
from lp.services.mail.sendmail import sendmail
12366.6.20 by Gary Poster
convert is_omitted to status enum on BugNotification, per review.
23
from lp.bugs.enum import BugNotificationStatus
11882.2.2 by Jonathan Lange
Clear up a heck of a lot of imports from canonical.launchpad.interfaces.
24
from lp.bugs.interfaces.bugnotification import IBugNotificationSet
13506.10.2 by Brad Crittenden
Horrible checkpoint
25
from lp.bugs.scripts.bugnotification import (
26
    get_email_notifications,
13506.10.3 by Brad Crittenden
Working version of deferred bug duplicate notifications
27
    process_deferred_notifications,
13506.10.2 by Brad Crittenden
Horrible checkpoint
28
    )
8356.1.1 by Leonard Richardson
Partial move.
29
from lp.services.scripts.base import LaunchpadCronScript
4264.2.1 by James Henstridge
add a LaunchpadCronScript subclass, and make cronscripts/*.py use it
30
31
32
class SendBugNotifications(LaunchpadCronScript):
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
33
    def main(self):
34
        notifications_sent = False
13506.10.2 by Brad Crittenden
Horrible checkpoint
35
        bug_notification_set = getUtility(IBugNotificationSet)
13627.2.9 by Brad Crittenden
Additional test coverage for deferred bug notifications
36
        deferred_notifications = \
37
            bug_notification_set.getDeferredNotifications()
13506.10.2 by Brad Crittenden
Horrible checkpoint
38
        process_deferred_notifications(deferred_notifications)
39
        pending_notifications = get_email_notifications(
40
            bug_notification_set.getNotificationsToSend())
12366.6.12 by Gary Poster
set omitted notifications as handled in the database.
41
        for (bug_notifications,
42
             omitted_notifications,
43
             messages) in pending_notifications:
3945.2.1 by kiko
Implement first cut of a rationale-gathering mail system. We supply a BugNotificationRationale instance to the relevant IBug methods, which add to the rationale the person and reason for mailing him. Support teams and dupes rather nicely, though not 100% perfectly.
44
            for message in messages:
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
45
                self.logger.info("Notifying %s about bug %d." % (
3945.2.1 by kiko
Implement first cut of a rationale-gathering mail system. We supply a BugNotificationRationale instance to the relevant IBug methods, which add to the rationale the person and reason for mailing him. Support teams and dupes rather nicely, though not 100% perfectly.
46
                    message['To'], bug_notifications[0].bug.id))
47
                sendmail(message)
48
                self.logger.debug(message.as_string())
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
49
            for notification in bug_notifications:
50
                notification.date_emailed = UTC_NOW
12366.6.20 by Gary Poster
convert is_omitted to status enum on BugNotification, per review.
51
                notification.status = BugNotificationStatus.SENT
12366.6.12 by Gary Poster
set omitted notifications as handled in the database.
52
            for notification in omitted_notifications:
53
                notification.date_emailed = UTC_NOW
12366.6.20 by Gary Poster
convert is_omitted to status enum on BugNotification, per review.
54
                notification.status = BugNotificationStatus.OMITTED
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
55
            notifications_sent = True
56
            # Commit after each batch of email sent, so that we won't
57
            # re-mail the notifications in case of something going wrong
58
            # in the middle.
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
59
            self.txn.commit()
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
60
61
        if not notifications_sent:
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
62
            self.logger.debug("No notifications are pending to be sent.")
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
63
64
65
if __name__ == '__main__':
3691.348.18 by kiko
Convert most other scripts; 6 remain, of which one looks like it's going to be a bit tricky...
66
    script = SendBugNotifications('send-bug-notifications',
67
        dbuser=config.malone.bugnotification_dbuser)
68
    script.lock_and_run()