Some small fixes to unique_user_id_adder module.
[Melange.git] / app / soc / cron / unique_user_id_adder.py
blob2e9e42c27e8cc361b017c0c61528c69375b0b3c8
1 #!/usr/bin/python2.5
3 # Copyright 2009 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Cron job handler for adding unique user id.
18 """
20 __authors__ = [
21 '"Pawel Solyga" <pawel.solyga@gmail.com>',
25 from google.appengine.ext import db
26 from google.appengine.api import users
27 from soc.logic.models.job import logic as job_logic
28 from soc.logic.models.priority_group import logic as priority_logic
29 from soc.logic.models.user import logic as user_logic
32 # amount of users to create jobs for before updating
33 DEF_USER_STEP_SIZE = 10
36 class TempUserWithUniqueId(db.Model):
37 """Helper model for temporary storing User Property with unique id.
38 """
39 user = db.UserProperty(required=True)
42 def emailToAccountAndUserId(address):
43 """Return a stable user_id string based on an email address, or None if
44 the address is not a valid/existing google account.
45 """
46 user = users.User(address)
47 key = TempUserWithUniqueId(user=user).put()
48 obj = TempUserWithUniqueId.get(key)
49 # pylint: disable-msg=E1103
50 return (obj, obj.user.user_id())
53 def setupUniqueUserIdAdder(job_entity):
54 """Job that setup jobs that will add unique user ids to all Users.
56 Args:
57 job_entity: a Job entity with key_data set to
58 [last_completed_user]
59 """
61 key_data = job_entity.key_data
62 user_fields = {'user_id': None}
64 if len(key_data) == 1:
65 # start where we left off
66 user_fields['__key__ >'] = key_data[0]
68 m_users = user_logic.getForFields(user_fields,
69 limit=DEF_USER_STEP_SIZE)
71 # set the default fields for the jobs we are going to create
72 priority_group = priority_logic.getGroup(priority_logic.CONVERT)
73 job_fields = {
74 'priority_group': priority_group,
75 'task_name': 'addUniqueUserIds'}
77 job_query_fields = job_fields.copy()
79 while m_users:
80 # for each user create a adder job
81 for user in m_users:
83 job_query_fields['key_data'] = user.key()
84 adder_job = job_logic.getForFields(job_query_fields, unique=True)
86 if not adder_job:
87 # this user doesn't have unique id yet
88 job_fields['key_data'] = [user.key()]
89 job_logic.updateOrCreateFromFields(job_fields)
91 # update our own job
92 last_user_key = m_users[-1].key()
94 if len(key_data) == 1:
95 key_data[0] = last_user_key
96 else:
97 key_data.append(last_user_key)
99 updated_job_fields = {'key_data': key_data}
100 job_logic.updateEntityProperties(job_entity, updated_job_fields)
102 # rinse and repeat
103 user_fields['__key__ >'] = last_user_key
104 m_users = user_logic.getForFields(user_fields,
105 limit=DEF_USER_STEP_SIZE)
107 # we are finished
108 return
111 def addUniqueUserIds(job_entity):
112 """Job that will add unique user id to a User.
114 Args:
115 job_entity: a Job entity with key_data set to [user_key]
118 from soc.cron.job import FatalJobError
120 user_keyname = job_entity.key_data[0].name()
121 user_entity = user_logic.getFromKeyName(user_keyname)
123 if not user_entity:
124 raise FatalJobError('The User with keyname %s does not exist!' % (
125 user_keyname))
127 # add unique user id
128 account, user_id = emailToAccountAndUserId(user_entity.account.email())
129 user_entity.account = account
130 user_entity.user_id = user_id
131 user_entity.put()
133 # we are done here
134 return