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 User profiles.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
23 '"Pawel Solyga" <pawel.solyga@gmail.com>',
27 from django
import forms
29 from soc
.logic
import cleaning
30 from soc
.logic
import dicts
31 from soc
.logic
.models
.site
import logic
as site_logic
32 from soc
.logic
.models
.user
import logic
as user_logic
33 from soc
.views
.helper
import access
34 from soc
.views
.helper
import decorators
35 from soc
.views
.helper
import redirects
36 from soc
.views
.helper
import widgets
37 from soc
.views
.models
import base
39 import soc
.models
.linkable
40 import soc
.logic
.models
.user
41 import soc
.views
.helper
44 class View(base
.View
):
45 """View methods for the User model.
49 def __init__(self
, params
=None):
50 """Defines the fields and methods required for the base View class
51 to provide the user with list, public, create, edit and delete views.
54 params: a dict with params for this View
57 rights
= access
.Checker(params
)
58 rights
['create'] = ['checkIsDeveloper']
59 rights
['edit'] = ['checkIsDeveloper']
60 rights
['delete'] = ['checkIsDeveloper']
61 rights
['show'] = ['allow']
62 rights
['list'] = ['checkIsDeveloper']
63 rights
['list_developers'] = ['checkIsDeveloper']
66 new_params
['logic'] = soc
.logic
.models
.user
.logic
67 new_params
['rights'] = rights
69 new_params
['name'] = "User"
71 new_params
['edit_template'] = 'soc/user/edit.html'
72 new_params
['pickable'] = True
73 new_params
['cache_pick'] = True
75 new_params
['sidebar_heading'] = 'Users'
77 new_params
['extra_dynaexclude'] = ['former_accounts', 'agreed_to_tos',
78 'agreed_to_tos_on', 'status']
79 new_params
['create_extra_dynaproperties'] = {
80 'clean_link_id': cleaning
.clean_user_not_exist('link_id'),
81 'clean_account': cleaning
.clean_user_account_not_in_use('account')}
83 # recreate the choices for the edit form
85 for choice
in user_logic
.getModel().status
.choices
:
86 status_choices
.append((choice
, choice
))
88 new_params
['edit_extra_dynaproperties'] = {
89 'link_id': forms
.CharField(widget
=widgets
.ReadOnlyInput(),
91 'clean_link_id': cleaning
.clean_link_id('link_id'),
92 'agreed_to_tos_on': forms
.DateTimeField(
93 widget
=widgets
.ReadOnlyInput(attrs
={'disabled':'true'}),
95 'status': forms
.ChoiceField(choices
=status_choices
),
96 'clean_account': cleaning
.clean_user_account('account'),
97 'clean': cleaning
.validate_user_edit('link_id', 'account'),
102 patterns
+= [(r
'^%(url_name)s/(?P<access_type>list_developers)$',
103 'soc.views.models.%(module_name)s.list_developers',
106 new_params
['extra_django_patterns'] = patterns
108 new_params
['sidebar_additional'] = [
109 ('/user/list_developers' % new_params
,
110 'List Developers', 'list_developers'),]
112 params
= dicts
.merge(params
, new_params
)
114 super(View
, self
).__init
__(params
=params
)
116 def listDevelopers(self
, request
, access_type
, page_name
=None, params
=None):
117 """See base.View.list.
120 filter = {'is_developer': True}
122 return self
.list(request
, access_type
, page_name
=page_name
,
123 params
=params
, filter=filter)
125 def _editGet(self
, request
, entity
, form
):
126 """See base.View._editGet().
129 # fill in the email field with the data from the entity
130 form
.fields
['account'].initial
= entity
.account
.email
131 form
.fields
['agreed_to_tos_on'].initial
= entity
.agreed_to_tos_on
132 form
.fields
['agreed_to_tos_on'].example_text
= self
._getToSExampleText
()
133 form
.fields
['status'].initial
= entity
.status
135 super(View
, self
)._editGet
(request
, entity
, form
)
138 def _getToSExampleText(self
):
139 """Returns example_text linking to site-wide ToS, or a warning message.
141 tos_link
= redirects
.getToSRedirect(site_logic
.getSingleton())
144 return ('<div class="notice"> <i>No site-wide</i> Terms of'
145 ' Service <i>are currently set!</i> </div>')
147 return ('<i>current site-wide <b><a href=%s>Terms of Service</a></b></i>'
153 admin
= decorators
.view(view
.admin
)
154 create
= decorators
.view(view
.create
)
155 delete
= decorators
.view(view
.delete
)
156 edit
= decorators
.view(view
.edit
)
157 list = decorators
.view(view
.list)
158 list_developers
= decorators
.view(view
.listDevelopers
)
159 public
= decorators
.view(view
.public
)
160 export
= decorators
.view(view
.export
)
161 pick
= decorators
.view(view
.pick
)