Fix user_self calling editGet with a wrong parameter
[Melange.git] / app / soc / views / models / mentor.py
blob10bbdfd6e2e9f0f6729f12242718c36a46957163
1 #!/usr/bin/python2.5
3 # Copyright 2009 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 Organization Mentors.
18 """
20 __authors__ = [
21 '"Lennard de Rijk" <ljvderijk@gmail.com>'
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
26 from django import forms
27 from django.utils.translation import ugettext
29 from soc.logic import dicts
30 from soc.logic.models import organization as org_logic
31 from soc.views.helper import access
32 from soc.views.helper import decorators
33 from soc.views.helper import dynaform
34 from soc.views.helper import redirects
35 from soc.views.helper import params as params_helper
36 from soc.views.helper import widgets
37 from soc.views.models import organization as org_view
38 from soc.views.models import role
40 import soc.logic.models.mentor
41 import soc.logic.models.org_admin
44 class View(role.View):
45 """View methods for the Organization Mentors model.
46 """
48 def __init__(self, params=None):
49 """Defines the fields and methods required for the base View class
50 to provide the user with list, public, create, edit and delete views.
52 Params:
53 params: a dict with params for this View
54 """
56 rights = access.Checker(params)
57 rights['create'] = ['checkIsDeveloper']
58 rights['edit'] = [('checkHasActiveRoleForScope',
59 soc.logic.models.mentor.logic),
60 ('checkIsMyEntity', [soc.logic.models.mentor.logic, 'user', True])]
61 rights['delete'] = ['checkIsDeveloper']
62 rights['invite'] = [('checkHasActiveRoleForScope',
63 soc.logic.models.org_admin.logic)]
64 rights['accept_invite'] = [('checkCanCreateFromRequest', 'mentor'),
65 'checkIsNotStudentForProgramOfOrg']
66 rights['request'] = ['checkIsNotStudentForProgramOfOrg',
67 ('checkCanMakeRequestToGroup', org_logic)]
68 rights['process_request'] = [
69 ('checkHasActiveRoleForScope', soc.logic.models.org_admin.logic),
70 ('checkCanProcessRequest', 'mentor')]
71 rights['manage'] = [
72 ('checkIsAllowedToManageRole', [soc.logic.models.mentor.logic,
73 soc.logic.models.org_admin.logic])]
75 new_params = {}
76 new_params['logic'] = soc.logic.models.mentor.logic
77 new_params['group_logic'] = org_logic.logic
78 new_params['group_view'] = org_view.view
79 new_params['rights'] = rights
80 new_params['pickable'] = True
81 new_params['cache_pick_order'] = ['link_id', 'name']
83 new_params['scope_view'] = org_view
85 new_params['name'] = "Mentor"
86 new_params['module_name'] = "mentor"
87 new_params['sidebar_grouping'] = 'Organizations'
89 new_params['extra_dynaexclude'] = ['agreed_to_tos', 'program']
91 new_params['create_dynafields'] = [
92 {'name': 'scope_path',
93 'base': forms.fields.CharField,
94 'widget': forms.HiddenInput,
95 'required': True,
97 {'name': 'mentor_agreement',
98 'base': forms.fields.CharField,
99 'required': False,
100 'widget': widgets.AgreementField,
101 'group': ugettext("5. Terms of Service"),
103 {'name': 'agreed_to_mentor_agreement',
104 'base': forms.fields.BooleanField,
105 'initial': False,
106 'required':True,
107 'label': ugettext('I agree to the Mentor Agreement'),
108 'group': ugettext("5. Terms of Service"),
112 new_params['allow_requests_and_invites'] = True
113 new_params['show_in_roles_overview'] = True
115 params = dicts.merge(params, new_params)
117 super(View, self).__init__(params=params)
119 # register the role with the group_view
120 self._params['group_view'].registerRole(self._params['module_name'], self)
122 # create and store the special form for invited users
123 dynafields = [
124 {'name': 'link_id',
125 'base': forms.CharField,
126 'widget': widgets.ReadOnlyInput(),
127 'required': False,
129 {'name': 'mentor_agreement',
130 'base': forms.fields.Field,
131 'required': False,
132 'widget': widgets.AgreementField,
133 'group': ugettext("5. Terms of Service"),
137 dynaproperties = params_helper.getDynaFields(dynafields)
139 invited_create_form = dynaform.extendDynaForm(
140 dynaform=self._params['create_form'],
141 dynaproperties=dynaproperties)
143 self._params['invited_create_form'] = invited_create_form
145 def _editPost(self, request, entity, fields):
146 """See base.View._editPost().
148 if not entity:
149 fields['user'] = fields['link_id']
150 fields['link_id'] = fields['user'].link_id
152 group_logic = self._params['group_logic']
153 group_entity = group_logic.getFromKeyName(fields['scope_path'])
154 fields['program'] = group_entity.scope
156 fields['agreed_to_tos'] = fields['agreed_to_mentor_agreement']
158 super(View, self)._editPost(request, entity, fields)
160 def _acceptInvitePost(self, fields, request, context, params, **kwargs):
161 """Fills in the fields that were missing in the invited_created_form.
163 For params see base.View._acceptInvitePost()
166 # fill in the appropriate fields that were missing in the form
167 fields['user'] = fields['link_id']
168 fields['link_id'] = fields['user'].link_id
169 fields['agreed_to_tos'] = fields['agreed_to_mentor_agreement']
171 group_logic = params['group_logic']
172 group_entity = group_logic.getFromKeyName(fields['scope_path'])
173 fields['program'] = group_entity.scope
175 def _editGet(self, request, entity, form):
176 """Sets the content of the agreed_to_tos_on field and replaces.
178 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed.
179 For params see base.View._editGet().
182 if entity.agreed_to_tos:
183 form.fields['agreed_to_mentor_agreement'] = forms.fields.BooleanField(
184 widget=forms.HiddenInput, initial=entity.agreed_to_tos,
185 required=True)
187 super(View, self)._editGet(request, entity, form)
189 def _editContext(self, request, context):
190 """See base.View._editContext.
193 entity = context['entity']
194 form = context['form']
196 if 'scope_path' in form.initial:
197 scope_path = form.initial['scope_path']
198 elif 'scope_path' in request.POST:
199 scope_path = request.POST['scope_path']
200 else:
201 form.fields['mentor_agreement'] = None
202 return
204 entity = org_logic.logic.getFromKeyName(scope_path)
206 if not (entity and entity.scope and entity.scope.mentor_agreement):
207 return
209 agreement = entity.scope.mentor_agreement
211 content = agreement.content
212 params = {'url_name': 'document'}
214 widget = form.fields['mentor_agreement'].widget
215 widget.text = content
216 widget.url = redirects.getPublicRedirect(agreement, params)
219 view = View()
221 accept_invite = decorators.view(view.acceptInvite)
222 admin = decorators.view(view.admin)
223 create = decorators.view(view.create)
224 delete = decorators.view(view.delete)
225 edit = decorators.view(view.edit)
226 invite = decorators.view(view.invite)
227 list = decorators.view(view.list)
228 manage = decorators.view(view.manage)
229 process_request = decorators.view(view.processRequest)
230 role_request = decorators.view(view.request)
231 public = decorators.view(view.public)
232 export = decorators.view(view.export)
233 pick = decorators.view(view.pick)