~launchpad-pqm/launchpad/devel

4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
1
#!/usr/bin/python2.4
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py 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).
4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
5
6
"""Script to convert a person into a team.
7
8
Only people whose account_status is NOACCOUNT can be turned into teams.
9
"""
10
11
12
import _pythonpath
13
14
from zope.component import getUtility
15
8356.1.9 by Leonard Richardson
Renamed the base script module in scripts/, which module_rename.py didn't touch because it wasn't under lib/.
16
from lp.services.scripts.base import (
4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
17
    LaunchpadScript, LaunchpadScriptFailure)
18
from canonical.launchpad.interfaces import AccountStatus, IPersonSet
19
20
21
class ConvertPersonToTeamScript(LaunchpadScript):
22
23
    usage = '%prog <person-to-convert> <team-owner>'
24
25
    def main(self):
26
        if len(self.args) != 2:
27
            raise LaunchpadScriptFailure(
28
                "You must specify the name of the person to be converted "
4962.6.8 by Guilherme Salgado
Some changes suggested by Michael
29
                "and the person/team who should be its teamowner.")
4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
30
31
        person_set = getUtility(IPersonSet)
32
        person_name, owner_name = self.args
33
        person = person_set.getByName(person_name)
4962.6.8 by Guilherme Salgado
Some changes suggested by Michael
34
        if person is None:
35
            raise LaunchpadScriptFailure(
36
                "There's no person named '%s'." % person_name)
4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
37
        if person.account_status != AccountStatus.NOACCOUNT:
38
            raise LaunchpadScriptFailure(
39
                "Only people which have no account can be turned into teams.")
4962.6.8 by Guilherme Salgado
Some changes suggested by Michael
40
        owner = person_set.getByName(owner_name)
41
        if owner is None:
42
            raise LaunchpadScriptFailure(
43
                "There's no person named '%s'." % owner_name)
4962.6.4 by Guilherme Salgado
Add a script to convert people into teams.
44
45
        person.convertToTeam(owner)
46
        self.txn.commit()
47
48
49
if __name__ == '__main__':
50
    script = ConvertPersonToTeamScript('convert-person-to-team')
51
    script.lock_and_run()
52