Style fixes and removal of unused imports in soc.views.models.
[Melange.git] / app / soc / views / models / student.py
blobe61f3c27e75563342bedc61c8ce8a6fcaee81364
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 Student.
18 """
20 __authors__ = [
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.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
41 import soc.logic.models.student
44 class View(role.View):
45 """View methods for the Student model.
46 """
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.
53 Params:
54 params: a dict with params for this View
55 """
57 rights = access.Checker(params)
58 rights['create'] = ['checkIsDeveloper']
59 rights['edit'] = [('checkHasActiveRoleForScope', student_logic.logic),
60 ('checkIsMyEntity', [student_logic.logic, 'user', True])]
61 rights['delete'] = ['checkIsDeveloper']
62 rights['apply'] = [
63 'checkIsUser',
64 ('checkIsActivePeriod', ['student_signup', 'scope_path']),
65 'checkIsNotParticipatingInProgramInScope',
67 rights['manage'] = [
68 ('checkIsAllowedToManageRole', [soc.logic.models.host.logic])]
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
76 new_params['scope_view'] = program_view
77 new_params['scope_redirect'] = redirects.getCreateRedirect
78 new_params['manage_redirect'] = redirects.getUserRolesRedirect
80 new_params['name'] = "Student"
81 new_params['module_name'] = "student"
82 new_params['sidebar_grouping'] = 'Students'
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
90 new_params['extra_dynaexclude'] = ['agreed_to_tos', 'school']
92 new_params['create_extra_dynaproperties'] = {
93 'expected_graduation': forms.IntegerField(required=True,
94 max_value=2030,
95 min_value=2009)
98 new_params['create_dynafields'] = [
99 {'name': 'scope_path',
100 'base': forms.fields.CharField,
101 'widget': forms.HiddenInput,
102 'required': True,
104 {'name': 'student_agreement',
105 'base': forms.fields.CharField,
106 'required': False,
107 'widget': widgets.AgreementField,
108 'group': ugettext("5. Terms of Service"),
110 {'name': 'agreed_to_student_agreement',
111 'base': forms.fields.BooleanField,
112 'initial': False,
113 'required':True,
114 'label': ugettext('I agree to the Student Agreement'),
115 'group': ugettext("5. Terms of Service"),
119 new_params['show_in_roles_overview'] = True
121 params = dicts.merge(params, new_params)
123 super(View, self).__init__(params=params)
125 # create and store the special form for users
126 updated_fields = {
127 'link_id': forms.CharField(widget=forms.HiddenInput,
128 required=True),
129 'clean_link_id': cleaning.clean_user_is_current('link_id')
132 user_create_form = dynaform.extendDynaForm(
133 dynaform = self._params['create_form'],
134 dynaproperties = updated_fields)
136 self._params['user_create_form'] = user_create_form
139 @decorators.merge_params
140 @decorators.check_access
141 def apply(self, request, access_type,
142 page_name=None, params=None, **kwargs):
143 """Handles student role creation for the current user.
146 user_entity = user_logic.logic.getForCurrentAccount()
147 params['create_form'] = params['user_create_form']
149 return self.create(request, access_type='unspecified', page_name=page_name,
150 params=params, link_id=user_entity.link_id, **kwargs)
152 def _editPost(self, request, entity, fields):
153 """See base.View._editPost().
156 if not entity:
157 fields['user'] = fields['link_id']
158 fields['link_id'] = fields['user'].link_id
160 fields['agreed_to_tos'] = fields['agreed_to_student_agreement']
162 super(View, self)._editPost(request, entity, fields)
164 def _editGet(self, request, entity, form):
165 """Sets the content of the agreed_to_tos_on field and replaces.
167 Also replaces the agreed_to_tos field with a hidden field when the ToS has been signed.
168 For params see base.View._editGet().
171 if entity.agreed_to_tos:
172 form.fields['agreed_to_student_agreement'] = forms.fields.BooleanField(
173 widget=forms.HiddenInput, initial=entity.agreed_to_tos,
174 required=True)
176 super(View, self)._editGet(request, entity, form)
178 def _editContext(self, request, context):
179 """See base.View._editContext().
182 # entity = context['entity']
183 form = context['form']
185 if 'scope_path' in form.initial:
186 scope_path = form.initial['scope_path']
187 elif 'scope_path' in request.POST:
188 scope_path = request.POST['scope_path']
189 else:
190 form.fields['student_agreement'] = None
191 return
193 program = program_logic.logic.getFromKeyName(scope_path)
195 if not (program and program.student_agreement):
196 return
198 agreement = program.student_agreement
200 content = agreement.content
201 params = {'url_name': 'document'}
203 widget = form.fields['student_agreement'].widget
204 widget.text = content
205 widget.url = redirects.getPublicRedirect(agreement, params)
208 view = View()
210 apply = decorators.view(view.apply)
211 create = decorators.view(view.create)
212 delete = decorators.view(view.delete)
213 edit = decorators.view(view.edit)
214 list = decorators.view(view.list)
215 manage = decorators.view(view.manage)
216 public = decorators.view(view.public)
217 export = decorators.view(view.export)