|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
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 |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
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. |
|
16 |
|
17 """Views for Student. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Lennard de Rijk" <ljvderijk@gmail.com>' |
|
22 ] |
|
23 |
|
24 |
|
25 from django import forms |
|
26 from django.utils.translation import ugettext |
|
27 |
|
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.helper import access |
|
34 from soc.views.helper import decorators |
|
35 from soc.views.helper import dynaform |
|
36 from soc.views.helper import redirects |
|
37 from soc.views.helper import widgets |
|
38 from soc.views.models import program as program_view |
|
39 from soc.views.models import role |
|
40 |
|
41 import soc.logic.models.student |
|
42 |
|
43 |
|
44 class View(role.View): |
|
45 """View methods for the Student model. |
|
46 """ |
|
47 |
|
48 |
|
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. |
|
52 |
|
53 Params: |
|
54 params: a dict with params for this View |
|
55 """ |
|
56 |
|
57 rights = access.Checker(params) |
|
58 rights['create'] = ['checkIsDeveloper'] |
|
59 rights['edit'] = [('checkHasActiveRoleForScope', student_logic.logic)] |
|
60 rights['delete'] = ['checkIsDeveloper'] |
|
61 rights['apply'] = [ |
|
62 'checkIsUser', |
|
63 ('checkIsActivePeriod', ['program', 'scope_path']), |
|
64 'checkIsNotParticipatingInProgramInScope', |
|
65 ] |
|
66 rights['manage'] = [ |
|
67 ('checkIsAllowedToManageRole', [soc.logic.models.host.logic, |
|
68 soc.logic.models.org_admin.logic])] |
|
69 |
|
70 new_params = {} |
|
71 new_params['logic'] = soc.logic.models.student.logic |
|
72 new_params['group_logic'] = program_logic.logic |
|
73 new_params['group_view'] = program_view.view |
|
74 new_params['rights'] = rights |
|
75 |
|
76 new_params['scope_view'] = program_view |
|
77 new_params['scope_redirect'] = redirects.getCreateRedirect |
|
78 new_params['manage_redirect'] = redirects.getUserRolesRedirect |
|
79 |
|
80 new_params['name'] = "Student" |
|
81 new_params['module_name'] = "student" |
|
82 new_params['sidebar_grouping'] = 'Students' |
|
83 |
|
84 # add apply pattern |
|
85 patterns = [(r'^%(url_name)s/(?P<access_type>apply)/%(scope)s$', |
|
86 'soc.views.models.%(module_name)s.apply', |
|
87 'Become a %(name)s'),] |
|
88 new_params['extra_django_patterns'] = patterns |
|
89 |
|
90 new_params['extra_dynaexclude'] = ['agreed_to_tos'] |
|
91 |
|
92 new_params['create_extra_dynafields'] = { |
|
93 'scope_path': forms.fields.CharField(widget=forms.HiddenInput, |
|
94 required=True), |
|
95 'student_agreement': forms.fields.CharField(required=False, |
|
96 widget=widgets.AgreementField), |
|
97 'agreed_to_student_agreement': forms.fields.BooleanField( |
|
98 initial=False, required=True, |
|
99 label=ugettext('I agree to the Student Agreement')), |
|
100 } |
|
101 |
|
102 params = dicts.merge(params, new_params) |
|
103 |
|
104 super(View, self).__init__(params=params) |
|
105 |
|
106 # create and store the special form for users |
|
107 updated_fields = { |
|
108 'link_id': forms.CharField(widget=forms.HiddenInput, |
|
109 required=True), |
|
110 'clean_link_id': cleaning.clean_user_is_current('link_id') |
|
111 } |
|
112 |
|
113 user_create_form = dynaform.extendDynaForm( |
|
114 dynaform = self._params['create_form'], |
|
115 dynafields = updated_fields) |
|
116 |
|
117 self._params['user_create_form'] = user_create_form |
|
118 |
|
119 @decorators.merge_params |
|
120 @decorators.check_access |
|
121 def apply(self, request, access_type, |
|
122 page_name=None, params=None, **kwargs): |
|
123 """Handles student role creation for the current user. |
|
124 """ |
|
125 |
|
126 user_entity = user_logic.logic.getForCurrentAccount() |
|
127 params['create_form'] = params['user_create_form'] |
|
128 |
|
129 return self.create(request, access_type='unspecified', page_name=page_name, |
|
130 params=params, link_id=user_entity.link_id, **kwargs) |
|
131 |
|
132 def _editPost(self, request, entity, fields): |
|
133 """See base.View._editPost(). |
|
134 """ |
|
135 |
|
136 if not entity: |
|
137 fields['user'] = fields['link_id'] |
|
138 fields['link_id'] = fields['user'].link_id |
|
139 |
|
140 fields['agreed_to_tos'] = fields['agreed_to_student_agreement'] |
|
141 |
|
142 super(View, self)._editPost(request, entity, fields) |
|
143 |
|
144 def _editGet(self, request, entity, form): |
|
145 """Sets the content of the agreed_to_tos_on field and replaces. |
|
146 |
|
147 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed. |
|
148 For params see base.View._editGet(). |
|
149 """ |
|
150 |
|
151 if entity.agreed_to_tos: |
|
152 form.fields['agreed_to_student_agreement'] = forms.fields.BooleanField( |
|
153 widget=forms.HiddenInput, initial=entity.agreed_to_tos, |
|
154 required=True) |
|
155 |
|
156 super(View, self)._editGet(request, entity, form) |
|
157 |
|
158 def _editContext(self, request, context): |
|
159 """See base.View._editContext(). |
|
160 """ |
|
161 |
|
162 entity = context['entity'] |
|
163 form = context['form'] |
|
164 |
|
165 if 'scope_path' in form.initial: |
|
166 scope_path = form.initial['scope_path'] |
|
167 elif 'scope_path' in request.POST: |
|
168 # TODO do this nicely |
|
169 scope_path = request.POST['scope_path'] |
|
170 else: |
|
171 # TODO is this always sufficient? |
|
172 form.fields['student_agreement'] = None |
|
173 return |
|
174 |
|
175 program = program_logic.logic.getFromKeyName(scope_path) |
|
176 |
|
177 if not (program and program.student_agreement): |
|
178 return |
|
179 |
|
180 content = entity.scope.student_agreement.content |
|
181 form.fields['student_agreement'].widget.text = content |
|
182 |
|
183 |
|
184 view = View() |
|
185 |
|
186 apply = view.apply |
|
187 create = view.create |
|
188 delete = view.delete |
|
189 edit = view.edit |
|
190 list = view.list |
|
191 manage = view.manage |
|
192 public = view.public |
|
193 export = view.export |