~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to scripts/close-account.py

  • Committer: Steve Kowalik
  • Date: 2011-08-07 04:05:52 UTC
  • mto: This revision was merged to the branch mainline in revision 13626.
  • Revision ID: stevenk@ubuntu.com-20110807040552-mwnxo0flmhvl35e8
Correct the notification based on review comments, and remove request{,ed}
from the function names, switching to create{,d}.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python -S
2
2
#
3
 
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
 
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
5
 
6
6
"""Remove personal details of a user from the database, leaving a stub."""
13
13
from optparse import OptionParser
14
14
import sys
15
15
 
 
16
from canonical.database.sqlbase import connect, sqlvalues
 
17
from canonical.launchpad.scripts import db_options, logger_options, logger
16
18
from lp.answers.enums import QuestionStatus
17
19
from lp.registry.interfaces.person import PersonCreationRationale
18
 
from lp.services.database.sqlbase import (
19
 
    connect,
20
 
    sqlvalues,
21
 
    )
22
 
from lp.services.scripts import (
23
 
    db_options,
24
 
    logger,
25
 
    logger_options,
26
 
    )
27
20
 
28
21
 
29
22
def close_account(con, log, username):
34
27
    cur = con.cursor()
35
28
    cur.execute("""
36
29
        SELECT Person.id, Person.account, name, teamowner
37
 
        FROM Person
38
 
        LEFT OUTER JOIN EmailAddress ON Person.id = EmailAddress.person
39
 
        WHERE name = %(username)s OR lower(email) = lower(%(username)s)
 
30
        FROM Person LEFT OUTER JOIN EmailAddress
 
31
            ON Person.id = EmailAddress.person
 
32
        WHERE name=%(username)s or lower(email)=lower(%(username)s)
40
33
        """, vars())
41
34
    try:
42
35
        person_id, account_id, username, teamowner = cur.fetchone()
71
64
    unknown_rationale = PersonCreationRationale.UNKNOWN.value
72
65
    cur.execute("""
73
66
        UPDATE Person
74
 
        SET
75
 
            displayname = 'Removed by request',
76
 
            name=%(new_name)s,
77
 
            language = NULL,
78
 
            account = NULL,
79
 
            homepage_content = NULL,
80
 
            icon = NULL,
81
 
            mugshot = NULL,
82
 
            hide_email_addresses = TRUE,
83
 
            registrant = NULL,
84
 
            logo = NULL,
85
 
            creation_rationale = %(unknown_rationale)s,
86
 
            creation_comment = NULL
87
 
        WHERE id = %(person_id)s
 
67
        SET displayname='Removed by request',
 
68
            name=%(new_name)s, language=NULL, account=NULL,
 
69
            homepage_content=NULL, icon=NULL, mugshot=NULL,
 
70
            hide_email_addresses=TRUE, registrant=NULL, logo=NULL,
 
71
            creation_rationale=%(unknown_rationale)s, creation_comment=NULL
 
72
        WHERE id=%(person_id)s
88
73
        """, vars())
89
74
 
90
75
    # Remove the Account. We don't set the status to deactivated,
102
87
    # Reassign their bugs
103
88
    table_notification('BugTask')
104
89
    cur.execute("""
105
 
        UPDATE BugTask SET assignee = NULL WHERE assignee = %(person_id)s
 
90
        UPDATE BugTask SET assignee=NULL WHERE assignee=%(person_id)s
106
91
        """, vars())
107
92
 
108
93
    # Reassign questions assigned to the user, and close all their questions
136
121
        ('BranchSubscription', 'person'),
137
122
        ('BugSubscription', 'person'),
138
123
        ('QuestionSubscription', 'person'),
 
124
        ('POSubscription', 'person'),
139
125
        ('SpecificationSubscription', 'person'),
140
126
 
141
127
        # Personal stuff, freeing up the namespace for others who want to play
157
143
        ('TeamParticipation', 'person'),
158
144
 
159
145
        # Contacts
 
146
        ('PackageBugSupervisor', 'bug_supervisor'),
160
147
        ('AnswerContact', 'person'),
161
148
 
162
149
        # Pending items in queues
163
150
        ('POExportRequest', 'person'),
 
151
 
 
152
        # Access lists
 
153
        ('PushMirrorAccess', 'person'),
 
154
        ('DistroComponentUploader', 'uploader'),
164
155
        ]
165
156
    for table, person_id_column in removals:
166
157
        table_notification(table)
180
171
 
181
172
    return True
182
173
 
183
 
 
184
174
def main():
185
175
    parser = OptionParser(
186
176
            '%prog [options] (username|email) [...]'
198
188
    con = None
199
189
    try:
200
190
        log.debug("Connecting to database")
201
 
        con = connect()
 
191
        con = connect(options.dbuser)
202
192
        for username in args:
203
193
            if not close_account(con, log, username):
204
194
                log.debug("Rolling back")
214
204
            con.rollback()
215
205
        return 1
216
206
 
217
 
 
218
207
if __name__ == '__main__':
219
208
    sys.exit(main())