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.
21 '"Lennard de Rijk" <ljvderijk@gmail.com>'
25 from django
import forms
26 from django
.utils
.translation
import ugettext
28 from soc
.logic
import cleaning
29 from soc
.logic
import dicts
30 from soc
.logic
.models
import program
as program_logic
31 from soc
.logic
.models
import student
as student_logic
32 from soc
.logic
.models
import user
as user_logic
33 from soc
.views
import out_of_band
34 from soc
.views
.helper
import access
35 from soc
.views
.helper
import decorators
36 from soc
.views
.helper
import dynaform
37 from soc
.views
.helper
import redirects
38 from soc
.views
.helper
import widgets
39 from soc
.views
.models
import program
as program_view
40 from soc
.views
.models
import role
42 import soc
.logic
.models
.student
45 class View(role
.View
):
46 """View methods for the Student model.
50 def __init__(self
, params
=None):
51 """Defines the fields and methods required for the base View class
52 to provide the user with list, public, create, edit and delete views.
55 params: a dict with params for this View
58 rights
= access
.Checker(params
)
59 rights
['create'] = ['checkIsDeveloper']
60 rights
['edit'] = [('checkHasActiveRoleForScope', student_logic
.logic
),
61 ('checkIsMyEntity', [student_logic
.logic
, 'user', True])]
62 rights
['delete'] = ['checkIsDeveloper']
65 ('checkIsActivePeriod', ['student_signup', 'scope_path']),
66 'checkIsNotParticipatingInProgramInScope',
69 ('checkIsAllowedToManageRole', [soc
.logic
.models
.host
.logic
])]
70 rights
['list_projects'] = [
71 ('checkHasActiveRoleForScope', student_logic
.logic
)]
74 new_params
['logic'] = soc
.logic
.models
.student
.logic
75 new_params
['group_logic'] = program_logic
.logic
76 new_params
['group_view'] = program_view
.view
77 new_params
['rights'] = rights
79 new_params
['scope_view'] = program_view
80 new_params
['scope_redirect'] = redirects
.getCreateRedirect
81 new_params
['manage_redirect'] = redirects
.getUserRolesRedirect
83 new_params
['name'] = "Student"
84 new_params
['module_name'] = "student"
85 new_params
['sidebar_grouping'] = 'Students'
88 patterns
= [(r
'^%(url_name)s/(?P<access_type>apply)/%(scope)s$',
89 'soc.views.models.%(module_name)s.apply',
91 (r
'^%(url_name)s/(?P<access_type>list_projects)/%(scope)s$',
92 'soc.views.models.%(module_name)s.list_projects',
93 'List of my Student Projects'),]
95 new_params
['extra_django_patterns'] = patterns
97 new_params
['extra_dynaexclude'] = ['agreed_to_tos', 'school']
99 new_params
['create_extra_dynaproperties'] = {
100 'expected_graduation': forms
.IntegerField(required
=True,
105 new_params
['create_dynafields'] = [
106 {'name': 'scope_path',
107 'base': forms
.fields
.CharField
,
108 'widget': forms
.HiddenInput
,
111 {'name': 'student_agreement',
112 'base': forms
.fields
.CharField
,
114 'widget': widgets
.AgreementField
,
115 'group': ugettext("5. Terms of Service"),
117 {'name': 'agreed_to_student_agreement',
118 'base': forms
.fields
.BooleanField
,
121 'label': ugettext('I agree to the Student Agreement'),
122 'group': ugettext("5. Terms of Service"),
126 new_params
['show_in_roles_overview'] = True
128 params
= dicts
.merge(params
, new_params
)
130 super(View
, self
).__init
__(params
=params
)
132 # create and store the special form for users
134 'link_id': forms
.CharField(widget
=forms
.HiddenInput
,
136 'clean_link_id': cleaning
.clean_user_is_current('link_id')
139 user_create_form
= dynaform
.extendDynaForm(
140 dynaform
= self
._params
['create_form'],
141 dynaproperties
= updated_fields
)
143 self
._params
['user_create_form'] = user_create_form
146 @decorators.merge_params
147 @decorators.check_access
148 def apply(self
, request
, access_type
,
149 page_name
=None, params
=None, **kwargs
):
150 """Handles student role creation for the current user.
153 user_entity
= user_logic
.logic
.getForCurrentAccount()
154 params
['create_form'] = params
['user_create_form']
156 return self
.create(request
, access_type
='unspecified', page_name
=page_name
,
157 params
=params
, link_id
=user_entity
.link_id
, **kwargs
)
159 @decorators.merge_params
160 @decorators.check_access
161 def listProjects(self
, request
, access_type
,
162 page_name
=None, params
=None, **kwargs
):
163 """View that lists all of the current user's Student Projects for the
164 Program given as Scope.
167 from soc
.views
.models
import student_project
as project_view
169 user_entity
= user_logic
.logic
.getForCurrentAccount()
171 fields
= {'link_id': user_entity
.link_id
,
172 'scope_path': kwargs
['scope_path']}
175 student_entity
= student_logic
.logic
.getFromKeyFieldsOr404(fields
)
176 except out_of_band
.Error
, error
:
177 return helper
.responses
.errorResponse(
178 error
, request
, template
=params
['error_public'])
180 # set the fields we need for the Student Project list
181 fields
= {'student': student_entity
}
183 list_params
= project_view
.view
.getParams().copy()
184 list_params
['list_description'] = ugettext(
185 'List of my Student Projects for %s') %(student_entity
.scope
.name
)
186 list_params
['list_action'] = (redirects
.getStudentEditRedirect
, list_params
)
188 return project_view
.view
.list(request
, access_type
, page_name
=page_name
,
189 params
=list_params
, filter=fields
)
191 def _editPost(self
, request
, entity
, fields
):
192 """See base.View._editPost().
196 fields
['user'] = fields
['link_id']
197 fields
['link_id'] = fields
['user'].link_id
199 fields
['agreed_to_tos'] = fields
['agreed_to_student_agreement']
201 super(View
, self
)._editPost
(request
, entity
, fields
)
203 def _editGet(self
, request
, entity
, form
):
204 """Sets the content of the agreed_to_tos_on field and replaces.
206 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed.
207 For params see base.View._editGet().
210 if entity
.agreed_to_tos
:
211 form
.fields
['agreed_to_student_agreement'] = forms
.fields
.BooleanField(
212 widget
=forms
.HiddenInput
, initial
=entity
.agreed_to_tos
,
215 super(View
, self
)._editGet
(request
, entity
, form
)
217 def _editContext(self
, request
, context
):
218 """See base.View._editContext().
221 # entity = context['entity']
222 form
= context
['form']
224 if 'scope_path' in form
.initial
:
225 scope_path
= form
.initial
['scope_path']
226 elif 'scope_path' in request
.POST
:
227 scope_path
= request
.POST
['scope_path']
229 form
.fields
['student_agreement'] = None
232 program
= program_logic
.logic
.getFromKeyName(scope_path
)
234 if not (program
and program
.student_agreement
):
237 agreement
= program
.student_agreement
239 content
= agreement
.content
240 params
= {'url_name': 'document'}
242 widget
= form
.fields
['student_agreement'].widget
243 widget
.text
= content
244 widget
.url
= redirects
.getPublicRedirect(agreement
, params
)
249 apply = decorators
.view(view
.apply)
250 create
= decorators
.view(view
.create
)
251 delete
= decorators
.view(view
.delete
)
252 edit
= decorators
.view(view
.edit
)
253 list = decorators
.view(view
.list)
254 list_projects
= decorators
.view(view
.listProjects
)
255 manage
= decorators
.view(view
.manage
)
256 public
= decorators
.view(view
.public
)
257 export
= decorators
.view(view
.export
)