Some more pylint fixes in different Melange modules.
[Melange.git] / app / soc / logic / models / user.py
blobf44b1d3114d89975a71e3b63d7f440e7ba70ea9c
1 #!/usr/bin/python2.5
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.
18 """
20 __authors__ = [
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.
39 """
41 def __init__(self):
42 """Defines the name, key_name and model for this entity.
43 """
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.
50 """
52 # TODO(pawel.solyga): replace 1000 with solution that works for any
53 # number of queries
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):
60 return True
62 return False
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.
69 """
71 account = accounts.getCurrentAccount()
73 if not account:
74 return None
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.
83 """
85 user_id = accounts.getCurrentUserId()
87 if not user_id:
88 return None
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.
97 """
99 if not account:
100 raise base.InvalidArgumentError
102 account = accounts.normalizeAccount(account)
104 fields = {
105 'account': account,
106 'status':'valid',
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.
118 if not user_id:
119 raise base.InvalidArgumentError
121 fields = {
122 'user_id': user_id,
123 'status':'valid',
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.
131 Args:
132 account: if not supplied, defaults to the current account
133 user: if not specified, defaults to the current user
136 current = accounts.getCurrentAccount()
138 if not account:
139 # default account to the current logged in account
140 account = current
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:
148 return True
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.
156 Args:
157 entity: User entity to check for agreement to site-wide ToS
159 Returns:
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
167 return True
169 try:
170 agreed_on = entity.agreed_to_tos_on
171 except db.Error:
172 # return False indicating that answer is missing
173 return False
175 # user has not agreed yet
176 if not agreed_on:
177 return False
179 return True
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.
203 return ['link_id']
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:
230 return False
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):
235 return False
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)
251 return True
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)
262 logic = Logic()