|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
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 |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
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. |
|
16 |
|
17 """Cron job handler for adding unique user id. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
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 |
|
30 from soc.models.user import User |
|
31 |
|
32 |
|
33 # amount of users to create jobs for before updating |
|
34 DEF_USER_STEP_SIZE = 10 |
|
35 |
|
36 |
|
37 class TempUserWithUniqueId(db.Model): |
|
38 """Helper model for temporary storing User Property with unique id. |
|
39 """ |
|
40 user = db.UserProperty(required=True) |
|
41 |
|
42 |
|
43 def emailToAccountAndUserId(address): |
|
44 """Return a stable user_id string based on an email address, or None if |
|
45 the address is not a valid/existing google account. |
|
46 """ |
|
47 user = users.User(address) |
|
48 key = TempUserWithUniqueId(user=user).put() |
|
49 obj = TempUserWithUniqueId.get(key) |
|
50 return (obj, obj.user.user_id()) |
|
51 |
|
52 |
|
53 def setupUniqueUserIdAdder(job_entity): |
|
54 """Job that setup jobs that will add unique user ids to all Users. |
|
55 |
|
56 Args: |
|
57 job_entity: a Job entity with key_data set to |
|
58 [last_completed_user] |
|
59 """ |
|
60 |
|
61 from soc.cron.job import FatalJobError |
|
62 |
|
63 user_fields = {'user_id': None} |
|
64 |
|
65 if len(key_data) == 1: |
|
66 # start where we left off |
|
67 user_fields['__key__ >'] = key_data[0] |
|
68 |
|
69 m_users = user_logic.getForFields(user_fields, |
|
70 limit=DEF_USER_STEP_SIZE) |
|
71 |
|
72 # set the default fields for the jobs we are going to create |
|
73 priority_group = priority_logic.getGroup(priority_logic.CONVERT) |
|
74 job_fields = { |
|
75 'priority_group': priority_group, |
|
76 'task_name': 'addUniqueUserIds'} |
|
77 |
|
78 job_query_fields = job_fields.copy() |
|
79 |
|
80 while m_users: |
|
81 # for each user create a adder job |
|
82 for user in m_users: |
|
83 |
|
84 job_query_fields['key_data'] = user.key() |
|
85 adder_job = job_logic.getForFields(job_query_fields, unique=True) |
|
86 |
|
87 if not adder_job: |
|
88 # this user doesn't have unique id yet |
|
89 job_fields['key_data'] = [user.key()] |
|
90 job_logic.updateOrCreateFromFields(job_fields) |
|
91 |
|
92 # update our own job |
|
93 last_user_key = m_users[-1].key() |
|
94 |
|
95 if len(key_data) == 1: |
|
96 key_data[0] = last_student_key |
|
97 else: |
|
98 key_data.append(last_student_key) |
|
99 |
|
100 updated_job_fields = {'key_data': key_data} |
|
101 job_logic.updateEntityProperties(job_entity, updated_job_fields) |
|
102 |
|
103 # rinse and repeat |
|
104 user_fields['__key__ >'] = last_user_key |
|
105 m_users = student_logic.getForFields(user_fields, |
|
106 limit=DEF_USER_STEP_SIZE) |
|
107 |
|
108 # we are finished |
|
109 return |
|
110 |
|
111 |
|
112 def addUniqueUserIds(job_entity): |
|
113 """Job that will add unique user id to a User. |
|
114 |
|
115 Args: |
|
116 job_entity: a Job entity with key_data set to [user_key] |
|
117 """ |
|
118 |
|
119 from soc.cron.job import FatalJobError |
|
120 |
|
121 user_keyname = job_entity.key_data[0].name() |
|
122 user_entity = user_logic.getFromKeyName(user_keyname) |
|
123 |
|
124 if not user_entity: |
|
125 raise FatalJobError('The User with keyname %s does not exist!' % ( |
|
126 user_keyname)) |
|
127 |
|
128 # add unique user id |
|
129 account, user_id = emailToAccountAndUserId(user_entity.account.email()) |
|
130 user_entity.account = account |
|
131 user_entity.user_id = user_id |
|
132 user_entity.put() |
|
133 |
|
134 # we are done here |
|
135 return |