~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
3361.2.1 by Bjorn Tillenius
make send-bug-notifications.py connect as a specific db user.
20
from canonical.config import config
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
21
from canonical.database.constants import UTC_NOW
22
from canonical.launchpad.mail 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
8523.3.1 by Gavin Panella
Bugs tree reorg after automated migration.
25
from lp.bugs.scripts.bugnotification import get_email_notifications
8356.1.1 by Leonard Richardson
Partial move.
26
from lp.services.scripts.base import LaunchpadCronScript
4264.2.1 by James Henstridge
add a LaunchpadCronScript subclass, and make cronscripts/*.py use it
27
28
29
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...
30
    def main(self):
31
        notifications_sent = False
32
        pending_notifications = get_email_notifications(getUtility(
33
            IBugNotificationSet).getNotificationsToSend())
12366.6.12 by Gary Poster
set omitted notifications as handled in the database.
34
        for (bug_notifications,
35
             omitted_notifications,
36
             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.
37
            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...
38
                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.
39
                    message['To'], bug_notifications[0].bug.id))
40
                sendmail(message)
41
                self.logger.debug(message.as_string())
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
42
            for notification in bug_notifications:
43
                notification.date_emailed = UTC_NOW
12366.6.20 by Gary Poster
convert is_omitted to status enum on BugNotification, per review.
44
                notification.status = BugNotificationStatus.SENT
12366.6.12 by Gary Poster
set omitted notifications as handled in the database.
45
            for notification in omitted_notifications:
46
                notification.date_emailed = UTC_NOW
12366.6.20 by Gary Poster
convert is_omitted to status enum on BugNotification, per review.
47
                notification.status = BugNotificationStatus.OMITTED
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
48
            notifications_sent = True
49
            # Commit after each batch of email sent, so that we won't
50
            # re-mail the notifications in case of something going wrong
51
            # 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...
52
            self.txn.commit()
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
53
54
        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...
55
            self.logger.debug("No notifications are pending to be sent.")
3254.1.21 by Bjorn Tillenius
add the actual cronscript.
56
57
58
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...
59
    script = SendBugNotifications('send-bug-notifications',
60
        dbuser=config.malone.bugnotification_dbuser)
61
    script.lock_and_run()
62