Fix user_self calling editGet with a wrong parameter
[Melange.git] / app / soc / views / models / user_self.py
blobc10f50cbabe2a0c8d988698db23487a8ee973967
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 """Views for the User's own profiles.
18 """
20 __authors__ = [
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
23 '"Pawel Solyga" <pawel.solyga@gmail.com>',
27 import datetime
29 from google.appengine.api import users
31 from django import forms
32 from django.utils.encoding import force_unicode
33 from django.utils.safestring import mark_safe
34 from django.utils.translation import ugettext
36 from soc.logic import cleaning
37 from soc.logic import dicts
38 from soc.logic import models as model_logic
39 from soc.logic.models.user import logic as user_logic
40 from soc.logic.models.site import logic as site_logic
41 from soc.views import helper
42 from soc.views.helper import access
43 from soc.views.helper import decorators
44 from soc.views.helper import redirects
45 from soc.views.helper import widgets
46 from soc.views.models import base
47 from soc.views.models import role as role_view
50 class View(base.View):
51 """Views for User own profiles.
52 """
54 DEF_ROLE_LIST_MSG_FMT = ugettext("Your roles as %(name)s.")
55 DEF_NO_ROLES_MSG_FMT = ugettext("You don't have any Roles in %s.")
57 def __init__(self, params=None):
58 """Defines the fields and methods required for the base View class
59 to provide the user with list, public, create, edit and delete views.
61 Params:
62 params: a dict with params for this View
63 """
65 rights = access.Checker(params)
66 rights['unspecified'] = ['deny']
67 rights['any_access'] = ['allow']
68 rights['create_profile'] = ['checkIsUnusedAccount']
69 rights['edit_profile'] = ['checkHasUserEntity']
70 rights['roles'] = ['checkIsUser']
71 rights['requests'] = ['checkIsUser']
72 rights['signIn'] = ['checkNotLoggedIn']
73 rights['notification'] = ['checkIsUser']
75 new_params = {}
76 new_params['rights'] = rights
77 new_params['logic'] = user_logic
79 new_params['name'] = "User"
80 new_params['module_name'] = "user_self"
81 new_params['url_name'] = "user"
83 new_params['create_template'] = 'soc/user/edit_profile.html'
84 new_params['edit_template'] = 'soc/user/edit_profile.html'
85 new_params['save_message'] = [ugettext('Profile saved.')]
86 new_params['edit_redirect'] = '/%(url_name)s/edit_profile'
88 # set the specific fields for the users profile page
89 new_params['extra_dynaexclude'] = ['former_accounts',
90 'account', 'is_developer', 'status', 'agreed_to_tos_on']
92 new_params['create_extra_dynaproperties'] = {
93 'clean_agreed_to_tos': cleaning.clean_agrees_to_tos('agreed_to_tos'),
94 'clean_link_id': cleaning.clean_user_not_exist('link_id'),}
96 new_params['edit_extra_dynaproperties'] = {
97 'clean_link_id': cleaning.clean_user_is_current('link_id', False),
98 'agreed_to_tos_on': forms.DateTimeField(
99 widget=widgets.ReadOnlyInput(attrs={'disabled':'true'}),
100 required=False),
103 new_params['sidebar_heading'] = 'User (self)'
104 new_params['sidebar'] = [
105 (users.create_login_url("/"), 'Sign In', 'signIn'),
106 ('/' + new_params['url_name'] + '/create_profile',
107 'Create Profile', 'create_profile'),
108 ('/' + new_params['url_name'] + '/edit_profile',
109 'Edit Profile', 'edit_profile'),
110 ('/' + new_params['url_name'] + '/roles', 'Roles', 'roles'),
111 ('/' + new_params['url_name'] + '/requests', 'Requests', 'requests'),
114 patterns = []
116 page_name = ugettext("Create your profile")
117 patterns += [(r'^%(url_name)s/(?P<access_type>create_profile)$',
118 'soc.views.models.%(module_name)s.create', page_name)]
120 page_name = ugettext("Edit your profile")
121 patterns += [(r'^%(url_name)s/(?P<access_type>edit_profile)$',
122 'soc.views.models.%(module_name)s.edit', page_name)]
124 page_name = ugettext("List of your roles")
125 patterns += [(r'^%(url_name)s/(?P<access_type>roles)$',
126 'soc.views.models.user_self.roles', page_name)]
128 page_name = ugettext("List of your requests")
129 patterns += [(r'^%(url_name)s/(?P<access_type>requests)$',
130 'soc.views.models.request.list_self', page_name)]
132 new_params['django_patterns_defaults'] = patterns
134 params = dicts.merge(params, new_params)
136 super(View, self).__init__(params=params)
138 @decorators.merge_params
139 @decorators.check_access
140 def editProfile(self, request, access_type,
141 page_name=None, params=None, **kwargs):
142 """Displays User profile edit page for the current user.
144 Args:
145 request: the standard Django HTTP request object
146 page_name: the page name displayed in templates as page and header title
147 params: a dict with params for this View
148 kwargs: The Key Fields for the specified entity
151 # set the link_id to the current user's link_id
152 user_entity = user_logic.getForCurrentAccount()
153 link_id = user_entity.link_id
155 return self.edit(request, access_type, page_name=page_name,
156 params=params, link_id=link_id, **kwargs)
158 def editGet(self, request, entity, context, params=None):
159 """Overwrite so we can add the contents of the ToS.
160 For params see base.View.editGet().
163 s_logic = model_logic.site.logic
164 site_tos = s_logic.getToS(s_logic.getSingleton())
165 if site_tos:
166 context['tos_contents'] = site_tos.content
168 return super(View, self).editGet(request, entity, context, params=params)
170 def _editGet(self, request, entity, form):
171 """Sets the content of the agreed_to_tos_on field and replaces.
173 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed.
174 For params see base.View._editGet().
177 if entity.agreed_to_tos:
178 form.fields['agreed_to_tos_on'].initial = entity.agreed_to_tos_on
179 # replace the 'agreed_to_tos' field with a hidden field so
180 # that the form checks still pass
181 form.fields['agreed_to_tos'] = forms.fields.BooleanField(
182 widget=forms.HiddenInput, initial=entity.agreed_to_tos, required=True)
184 def editPost(self, request, entity, context, params=None):
185 """Overwrite so we can add the contents of the ToS.
186 For params see base.View.editPost().
189 s_logic = model_logic.site.logic
190 site_tos = s_logic.getToS(s_logic.getSingleton())
191 if site_tos:
192 context['tos_contents'] = site_tos.content
194 return super(View, self).editPost(request, entity, context, params=params)
196 def _editPost(self, request, entity, fields):
197 """See base.View._editPost().
200 # fill in the account field with the current User
201 fields['account'] = users.User()
203 # special actions if there is no ToS present
204 s_logic = model_logic.site.logic
205 site_tos = s_logic.getToS(s_logic.getSingleton())
206 if not site_tos:
207 # there is no Terms of Service set
208 if not entity:
209 # we are a new user so set the agrees_to_tos field to None
210 fields['agreed_to_tos'] = None
211 else:
212 # editing an existing user so no value changes allowed
213 fields['agreed_to_tos'] = entity.agreed_to_tos
214 else:
215 if not entity or not entity.agreed_to_tos:
216 # set the time of agreement
217 fields['agreed_to_tos_on'] = datetime.datetime.now()
219 super(View, self)._editPost(request, entity, fields)
221 @decorators.merge_params
222 @decorators.check_access
223 def roles(self, request, access_type,
224 page_name=None, params=None, **kwargs):
225 """Displays the valid roles for this user.
227 Args:
228 request: the standard Django HTTP request object
229 access_type : the name of the access type which should be checked
230 page_name: the page name displayed in templates as page and header title
231 params: a dict with params for this View
232 kwargs: not used
235 user = user_logic.getForCurrentAccount()
237 # only select the roles for the current user
238 filter = {
239 'link_id': user.link_id,
240 'status': ['active', 'inactive']
243 contents = []
245 i = 0
247 for _, loop_view in sorted(role_view.ROLE_VIEWS.iteritems()):
248 list_params = loop_view.getParams().copy()
249 list_params['list_action'] = (redirects.getEditRedirect, list_params)
250 list_params['list_description'] = self.DEF_ROLE_LIST_MSG_FMT % list_params
252 list = helper.lists.getListContent(request, list_params, filter,
253 idx=i, need_content=True)
255 if list:
256 contents.append(list)
257 i += 1
259 site = site_logic.getSingleton()
260 site_name = site.site_name
262 params = params.copy()
263 params['no_lists_msg'] = self.DEF_NO_ROLES_MSG_FMT % site_name
265 return self._list(request, params, contents, page_name)
267 def getSidebarMenus(self, id, user, params=None):
268 """See base.View.getSidebarMenus().
271 link_title = ugettext('Notifications')
273 filter = {
274 'scope': user,
275 'unread': True,
278 notifications = model_logic.notification.logic.getForFields(filter)
279 count = len(list(notifications))
281 if count > 0:
282 link_title = '<span class="unread">%s (%d)</span>' % (
283 force_unicode(link_title), count)
284 link_title = mark_safe(link_title)
286 items = [('/' + 'notification/list', link_title, 'notification')]
287 if user:
288 items += [(redirects.getCreateDocumentRedirect(user, 'user'),
289 "Create a New Document", 'any_access')]
291 items += [(redirects.getListDocumentsRedirect(user, 'user'),
292 "List Documents", 'any_access')]
294 new_params = {}
295 new_params['sidebar_additional'] = items
297 params = dicts.merge(params, new_params)
299 return super(View, self).getSidebarMenus(id, user, params=params)
302 view = View()
304 create = decorators.view(view.create)
305 edit = decorators.view(view.editProfile)
306 export = decorators.view(view.export)
307 roles = decorators.view(view.roles)