3 # Copyright 2008 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 """User (Model) query functions.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
25 from google
.appengine
.api
import users
26 from google
.appengine
.ext
import db
28 from soc
.cache
import sidebar
29 from soc
.logic
import accounts
30 from soc
.logic
.helper
import notifications
31 from soc
.logic
.models
import base
32 from soc
.logic
.models
.site
import logic
as site_logic
34 import soc
.models
.user
37 class Logic(base
.Logic
):
38 """Logic methods for the User model.
42 """Defines the name, key_name and model for this entity.
45 super(Logic
, self
).__init
__(soc
.models
.user
.User
,
46 skip_properties
=['former_accounts'])
48 def isFormerAccount(self
, account
):
49 """Returns true if account is a former account of some User.
52 # TODO(pawel.solyga): replace 1000 with solution that works for any
54 users_with_former_accounts
= soc
.models
.user
.User
.gql(
55 'WHERE former_accounts != :1', None).fetch(1000)
57 for former_account_user
in users_with_former_accounts
:
58 for former_account
in former_account_user
.former_accounts
:
59 if str(account
) == str(former_account
):
64 def getForCurrentAccount(self
):
65 """Retrieves the user entity for the currently logged in account.
67 If there is no user logged in, or they have no valid associated User
68 entity, None is returned.
71 account
= accounts
.getCurrentAccount()
76 return self
.getForAccount(account
)
78 def getForCurrentUserId(self
):
79 """Retrieves the user entity for the currently logged in user id.
81 If there is no user logged in, or they have no valid associated User
82 entity, None is returned.
85 user_id
= accounts
.getCurrentUserId()
90 return self
.getForUserId(user_id
)
92 def getForAccount(self
, account
):
93 """Retrieves the user entity for the specified account.
95 If there is no user logged in, or they have no valid associated User
96 entity, None is returned.
100 raise base
.InvalidArgumentError
102 account
= accounts
.normalizeAccount(account
)
109 return self
.getForFields(filter=fields
, unique
=True)
111 def getForUserId(self
, user_id
):
112 """Retrieves the user entity for the specified user id.
114 If there is no user logged in, or they have no valid associated User
115 entity, None is returned.
119 raise base
.InvalidArgumentError
126 return self
.getForFields(filter=fields
, unique
=True)
128 def isDeveloper(self
, account
=None, user
=None):
129 """Returns true iff the specified user is a Developer.
132 account: if not supplied, defaults to the current account
133 user: if not specified, defaults to the current user
136 current
= accounts
.getCurrentAccount()
139 # default account to the current logged in account
142 if account
and (not user
):
143 # default user to the current logged in user
144 user
= self
.getForAccount(account
)
146 # pylint: disable-msg=E1103
147 if user
and user
.is_developer
:
150 if account
and (account
== current
):
151 return users
.is_current_user_admin()
153 def agreesToSiteToS(self
, entity
):
154 """Returns indication of User's answer to the site-wide Terms of Service.
157 entity: User entity to check for agreement to site-wide ToS
160 True: no site-wide ToS is currently in effect on the site
161 True: site-wide ToS is in effect *and* User agrees to it
162 (User explicitly answered "Yes")
163 False: site-wide ToS is in effect but User did not agree to it yet
165 if not site_logic
.getToS(site_logic
.getSingleton()):
166 # no site-wide ToS in effect, so let the User slide for now
170 agreed_on
= entity
.agreed_to_tos_on
172 # return False indicating that answer is missing
175 # user has not agreed yet
181 def getKeyValuesFromEntity(self
, entity
):
182 """See base.Logic.getKeyValuesFromEntity.
185 return [entity
.link_id
]
187 def getSuffixValues(self
, entity
):
188 """See base.Logic.getSuffixValues.
191 return [entity
.link_id
]
193 def getKeyValuesFromFields(self
, fields
):
194 """See base.Logic.getKeyValuesFromFields.
197 return [fields
['link_id']]
199 def getKeyFieldNames(self
):
200 """See base.Logic.getKeyFieldNames.
205 def _createField(self
, entity_properties
, name
):
206 """Normalize the account before storing it.
209 value
= entity_properties
[name
]
211 if (name
== 'account'):
212 # normalize all accounts before doing anything with the value
213 value
= accounts
.normalizeAccount(value
)
214 entity_properties
[name
] = value
216 def _updateField(self
, entity
, entity_properties
, name
):
217 """Special case logic for account.
219 When the account is changed, the former_accounts field should be appended
220 with the old account.
221 Also, if either is_developer or agrees_to_tos change, the user's
222 rights have changed, so we need to flush the sidebar.
223 Make sure once the user agreed ToS, the ToS fields can't be changed.
226 value
= entity_properties
[name
]
228 # iff the agreed_to_tos is True and we want to set it to False
229 if (name
== 'agreed_to_tos') and (not value
) and entity
.agreed_to_tos
:
232 # iff the agreed_to_tos_on has a value and we want to change it
233 if (name
== 'agreed_to_tos_on') and entity
.agreed_to_tos_on
and (
234 value
!= entity
.agreed_to_tos_on
):
237 if (name
== 'is_developer') and (entity
.is_developer
!= value
):
238 sidebar
.flush(entity
.account
)
240 if (name
== 'agreed_to_tos') and (entity
.agreed_to_tos
!= value
):
241 sidebar
.flush(entity
.account
)
243 if (name
== 'account'):
244 # normalize all accounts before doing anything with the value
245 value
= accounts
.normalizeAccount(value
)
246 entity_properties
[name
] = value
248 if entity
.account
!= value
:
249 entity
.former_accounts
.append(entity
.account
)
253 def _onCreate(self
, entity
):
254 """Send out a message to welcome the new user.
257 notifications
.sendWelcomeMessage(entity
)
259 super(Logic
, self
)._onCreate
(entity
)