~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
2770.1.47 by Guilherme Salgado
New cronscript to flag expired team memberships and some other cleanups.
7
4621.5.11 by Curtis Hovey
Merge from RF. Resolved sampledata conflict. Fixed imports, lines, and docstrings.
8
"""Flag expired team memberships and warn about impending expiration."""
9
2770.1.47 by Guilherme Salgado
New cronscript to flag expired team memberships and some other cleanups.
10
import _pythonpath
11
14612.2.8 by William Grant
cronscripts
12
from datetime import (
13
    datetime,
14
    timedelta,
15
    )
16
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
17
import pytz
2770.1.47 by Guilherme Salgado
New cronscript to flag expired team memberships and some other cleanups.
18
from zope.component import getUtility
19
13130.1.21 by Curtis Hovey
Fixed cronscript imports.
20
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
11882.2.2 by Jonathan Lange
Clear up a heck of a lot of imports from canonical.launchpad.interfaces.
21
from lp.registry.interfaces.teammembership import (
22
    DAYS_BEFORE_EXPIRATION_WARNING_IS_SENT,
23
    ITeamMembershipSet,
24
    )
14612.2.8 by William Grant
cronscripts
25
from lp.services.config import config
8356.1.1 by Leonard Richardson
Partial move.
26
from lp.services.scripts.base import (
14612.2.8 by William Grant
cronscripts
27
    LaunchpadCronScript,
28
    LaunchpadScriptFailure,
29
    )
4264.2.1 by James Henstridge
add a LaunchpadCronScript subclass, and make cronscripts/*.py use it
30
31
32
class ExpireMemberships(LaunchpadCronScript):
4621.5.11 by Curtis Hovey
Merge from RF. Resolved sampledata conflict. Fixed imports, lines, and docstrings.
33
    """A script for expired team memberships."""
34
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
35
    def flag_expired_memberships_and_send_warnings(self):
4621.5.11 by Curtis Hovey
Merge from RF. Resolved sampledata conflict. Fixed imports, lines, and docstrings.
36
        """Flag expired team memberships and warn about impending expiration.
37
38
        Flag expired team memberships and send warnings for members whose
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
39
        memberships are going to expire in one week (or less) from now.
40
        """
41
        membershipset = getUtility(ITeamMembershipSet)
42
        self.txn.begin()
4621.5.28 by Curtis Hovey
Renamed Launchpan Janitor per review.
43
        reviewer = getUtility(ILaunchpadCelebrities).janitor
4108.4.9 by Guilherme Salgado
Fix the flag-expired-memberships.py cronscript to auto renew memberships of teams which have a renewal policy set to AUTOMATIC
44
        membershipset.handleMembershipsExpiringToday(reviewer)
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
45
        self.txn.commit()
46
4108.4.13 by Guilherme Salgado
Second half of the fix for https://launchpad.net/bugs/70519: Allow members of teams with an ONDEMAND renewal policy to renew their own memberships
47
        min_date_for_warning = datetime.now(pytz.timezone('UTC')) + timedelta(
48
            days=DAYS_BEFORE_EXPIRATION_WARNING_IS_SENT)
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
49
        self.txn.begin()
4108.4.9 by Guilherme Salgado
Fix the flag-expired-memberships.py cronscript to auto renew memberships of teams which have a renewal policy set to AUTOMATIC
50
        for membership in membershipset.getMembershipsToExpire(
11128.7.1 by Edwin Grubbs
Do not send membership expiration emails for teams with automatic membership renewal.
51
            min_date_for_warning, exclude_autorenewals=True):
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
52
            membership.sendExpirationWarningEmail()
11128.7.1 by Edwin Grubbs
Do not send membership expiration emails for teams with automatic membership renewal.
53
            self.logger.debug("Sent warning email to %s in %s team."
54
                          % (membership.person.name, membership.team.name))
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
55
        self.txn.commit()
56
57
    def main(self):
4621.5.11 by Curtis Hovey
Merge from RF. Resolved sampledata conflict. Fixed imports, lines, and docstrings.
58
        """Flag expired team memberships."""
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
59
        if self.args:
60
            raise LaunchpadScriptFailure(
61
                "Unhandled arguments %s" % repr(self.args))
62
        self.logger.info("Flagging expired team memberships.")
63
        self.flag_expired_memberships_and_send_warnings()
64
        self.logger.info("Finished flagging expired team memberships.")
3691.272.22 by Guilherme Salgado
Fix https://launchpad.net/launchpad/+bug/70518: Notify team members when their membership is going to expire
65
2770.1.47 by Guilherme Salgado
New cronscript to flag expired team memberships and some other cleanups.
66
67
if __name__ == '__main__':
4621.5.7 by Curtis Hovey
Replaced answer_tracker_janitor and team_membership_janitor with
68
    script = ExpireMemberships('flag-expired-memberships',
4108.4.13 by Guilherme Salgado
Second half of the fix for https://launchpad.net/bugs/70519: Allow members of teams with an ONDEMAND renewal policy to renew their own memberships
69
                               dbuser=config.expiredmembershipsflagger.dbuser)
3691.348.13 by kiko
Convert a couple of cronscripts over to LaunchpadScript
70
    script.lock_and_run()