-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_accounts.py
62 lines (50 loc) · 1.83 KB
/
update_accounts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Removes duplicate nicknames (issue99).
#
# To run this script:
# - Make sure App Engine library (incl. yaml) is in PYTHONPATH.
# - Make sure that the remote API is included in app.yaml.
# - Run "tools/appengine_console.py APP_ID".
# - Import this module.
# - update_accounts.run() updates accounts.
# - Use the other two functions to fetch accounts or find duplicates
# without any changes to the datastore.
from google.appengine.ext import db
from codereview import models
def fetch_accounts():
query = models.Account.all()
accounts = {}
results = query.fetch(100)
while results:
last = None
for account in results:
if account.lower_nickname in accounts:
accounts[account.lower_nickname].append(account)
else:
accounts[account.lower_nickname] = [account]
last = account
if last is None:
break
results = models.Account.all().filter('__key__ >',
last.key()).fetch(100)
return accounts
def find_duplicates(accounts):
tbd = []
while accounts:
_, entries = accounts.popitem()
if len(entries) > 1:
# update accounts, except the fist: it's the lucky one
for num, account in enumerate(entries[1:]):
account.nickname = '%s%d' % (account.nickname, num+1)
account.lower_nickname = account.nickname.lower()
account.fresh = True # display "change nickname..."
tbd.append(account)
return tbd
def run():
accounts = fetch_accounts()
print '%d accounts fetched' % len(accounts)
tbd = find_duplicates(accounts)
print 'Updating %d accounts' % len(tbd)
db.put(tbd)
print 'Updated accounts:'
for account in tbd:
print ' %s' % account.email