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.
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.
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.
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.
57 job_entity: a Job entity with key_data set to
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
)
74 'priority_group': priority_group
,
75 'task_name': 'addUniqueUserIds'}
77 job_query_fields
= job_fields
.copy()
80 # for each user create a adder job
83 job_query_fields
['key_data'] = user
.key()
84 adder_job
= job_logic
.getForFields(job_query_fields
, unique
=True)
87 # this user doesn't have unique id yet
88 job_fields
['key_data'] = [user
.key()]
89 job_logic
.updateOrCreateFromFields(job_fields
)
92 last_user_key
= m_users
[-1].key()
94 if len(key_data
) == 1:
95 key_data
[0] = last_user_key
97 key_data
.append(last_user_key
)
99 updated_job_fields
= {'key_data': key_data
}
100 job_logic
.updateEntityProperties(job_entity
, updated_job_fields
)
103 user_fields
['__key__ >'] = last_user_key
104 m_users
= user_logic
.getForFields(user_fields
,
105 limit
=DEF_USER_STEP_SIZE
)
111 def addUniqueUserIds(job_entity
):
112 """Job that will add unique user id to a User.
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
)
124 raise FatalJobError('The User with keyname %s does not exist!' % (
128 account
, user_id
= emailToAccountAndUserId(user_entity
.account
.email())
129 user_entity
.account
= account
130 user_entity
.user_id
= user_id