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.
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.
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.
53 params: a dict with params for this View
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')]
72 ('checkIsAllowedToManageRole', [soc
.logic
.models
.mentor
.logic
,
73 soc
.logic
.models
.org_admin
.logic
])]
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
81 new_params
['scope_view'] = org_view
83 new_params
['name'] = "Mentor"
84 new_params
['module_name'] = "mentor"
85 new_params
['sidebar_grouping'] = 'Organizations'
87 new_params
['extra_dynaexclude'] = ['agreed_to_tos', 'program']
89 new_params
['create_dynafields'] = [
90 {'name': 'scope_path',
91 'base': forms
.fields
.CharField
,
92 'widget': forms
.HiddenInput
,
95 {'name': 'mentor_agreement',
96 'base': forms
.fields
.CharField
,
98 'widget': widgets
.AgreementField
,
99 'group': ugettext("5. Terms of Service"),
101 {'name': 'agreed_to_mentor_agreement',
102 'base': forms
.fields
.BooleanField
,
105 'label': ugettext('I agree to the Mentor Agreement'),
106 'group': ugettext("5. Terms of Service"),
110 new_params
['allow_requests_and_invites'] = True
111 new_params
['show_in_roles_overview'] = True
113 params
= dicts
.merge(params
, new_params
)
115 super(View
, self
).__init
__(params
=params
)
117 # register the role with the group_view
118 params
['group_view'].registerRole(params
['module_name'], self
)
120 # create and store the special form for invited users
123 'base': forms
.CharField
,
124 'widget': widgets
.ReadOnlyInput(),
127 {'name': 'mentor_agreement',
128 'base': forms
.fields
.Field
,
130 'widget': widgets
.AgreementField
,
131 'group': ugettext("5. Terms of Service"),
135 dynaproperties
= params_helper
.getDynaFields(dynafields
)
137 invited_create_form
= dynaform
.extendDynaForm(
138 dynaform
=self
._params
['create_form'],
139 dynaproperties
=dynaproperties
)
141 params
['invited_create_form'] = invited_create_form
143 def _editPost(self
, request
, entity
, fields
):
144 """See base.View._editPost().
147 fields
['user'] = fields
['link_id']
148 fields
['link_id'] = fields
['user'].link_id
150 group_logic
= self
._params
['group_logic']
151 group_entity
= group_logic
.getFromKeyName(fields
['scope_path'])
152 fields
['program'] = group_entity
.scope
154 fields
['agreed_to_tos'] = fields
['agreed_to_mentor_agreement']
156 super(View
, self
)._editPost
(request
, entity
, fields
)
158 def _acceptInvitePost(self
, fields
, request
, context
, params
, **kwargs
):
159 """Fills in the fields that were missing in the invited_created_form.
161 For params see base.View._acceptInvitePost()
164 # fill in the appropriate fields that were missing in the form
165 fields
['user'] = fields
['link_id']
166 fields
['link_id'] = fields
['user'].link_id
167 fields
['agreed_to_tos'] = fields
['agreed_to_mentor_agreement']
169 group_logic
= params
['group_logic']
170 group_entity
= group_logic
.getFromKeyName(fields
['scope_path'])
171 fields
['program'] = group_entity
.scope
173 def _editGet(self
, request
, entity
, form
):
174 """Sets the content of the agreed_to_tos_on field and replaces.
176 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed.
177 For params see base.View._editGet().
180 if entity
.agreed_to_tos
:
181 form
.fields
['agreed_to_mentor_agreement'] = forms
.fields
.BooleanField(
182 widget
=forms
.HiddenInput
, initial
=entity
.agreed_to_tos
,
185 super(View
, self
)._editGet
(request
, entity
, form
)
187 def _editContext(self
, request
, context
):
188 """See base.View._editContext.
191 entity
= context
['entity']
192 form
= context
['form']
194 if 'scope_path' in form
.initial
:
195 scope_path
= form
.initial
['scope_path']
196 elif 'scope_path' in request
.POST
:
197 scope_path
= request
.POST
['scope_path']
199 form
.fields
['mentor_agreement'] = None
202 entity
= org_logic
.logic
.getFromKeyName(scope_path
)
204 if not (entity
and entity
.scope
and entity
.scope
.mentor_agreement
):
207 agreement
= entity
.scope
.mentor_agreement
209 content
= agreement
.content
210 params
= {'url_name': 'document'}
212 widget
= form
.fields
['mentor_agreement'].widget
213 widget
.text
= content
214 widget
.url
= redirects
.getPublicRedirect(agreement
, params
)
219 accept_invite
= decorators
.view(view
.acceptInvite
)
220 admin
= decorators
.view(view
.admin
)
221 create
= decorators
.view(view
.create
)
222 delete
= decorators
.view(view
.delete
)
223 edit
= decorators
.view(view
.edit
)
224 invite
= decorators
.view(view
.invite
)
225 list = decorators
.view(view
.list)
226 manage
= decorators
.view(view
.manage
)
227 process_request
= decorators
.view(view
.processRequest
)
228 role_request
= decorators
.view(view
.request
)
229 public
= decorators
.view(view
.public
)
230 export
= decorators
.view(view
.export
)