Style fixes and removal of unused imports in soc.views.models.
[Melange.git] / app / soc / views / models / student_project.py
bloba492a4598c94068112f442530aa032c31fe9c82e
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 Project.
18 """
20 __authors__ = [
21 '"Lennard de Rijk" <ljvderijk@gmail.com>',
25 import time
27 from django import forms
29 from soc.logic import cleaning
30 from soc.logic import dicts
31 from soc.logic.models import mentor as mentor_logic
32 from soc.logic.models import student as student_logic
33 from soc.views.helper import access
34 from soc.views.helper import decorators
35 from soc.views.helper import redirects
36 from soc.views.helper import widgets
37 from soc.views.models import base
38 from soc.views.models import organization as org_view
40 import soc.logic.models.student_project
43 class View(base.View):
44 """View methods for the Student Project model.
45 """
47 def __init__(self, params=None):
48 """Defines the fields and methods required for the base View class
49 to provide the user with list, public, create, edit and delete views.
51 Params:
52 params: a dict with params for this View
53 """
55 rights = access.Checker(params)
56 rights['create'] = ['checkIsDeveloper']
57 # TODO who should be able to edit this?
58 rights['edit'] = ['checkIsDeveloper']
60 rights['delete'] = ['checkIsDeveloper']
61 rights['show'] = ['allow']
62 rights['list'] = ['checkIsDeveloper']
64 new_params = {}
65 new_params['logic'] = soc.logic.models.student_project.logic
66 new_params['rights'] = rights
67 new_params['name'] = "Student Project"
68 new_params['url_name'] = "student_project"
69 new_params['sidebar_grouping'] = 'Students'
71 new_params['scope_view'] = org_view
72 new_params['scope_redirect'] = redirects.getCreateRedirect
74 new_params['no_create_with_key_fields'] = True
76 new_params['extra_dynaexclude'] = ['program', 'status', 'link_id',
77 'mentor', 'student']
79 new_params['create_extra_dynaproperties'] = {
80 'scope_path': forms.CharField(widget=forms.HiddenInput,
81 required=True),
82 'student_id': forms.CharField(label='Student Link ID',
83 required=True),
84 'mentor_id': forms.CharField(label='Mentor Link ID',
85 required=True),
86 'clean_student': cleaning.clean_link_id('student'),
87 'clean_mentor': cleaning.clean_link_id('mentor'),
88 'clean_additional_info': cleaning.clean_url('additional_info'),
89 'clean': cleaning.validate_new_student_project('scope_path',
90 'mentor_id', 'student_id')
93 new_params['edit_extra_dynaproperties'] = {
94 'student_id': forms.CharField(label='Student Link ID',
95 widget=widgets.ReadOnlyInput),
96 'mentor_id': forms.CharField(label='Mentor Link ID',
97 widget=widgets.ReadOnlyInput),
98 'link_id': forms.CharField(widget=forms.HiddenInput),
99 'clean': (lambda x: x.cleaned_data)
102 # TODO(ljvderijk) OrgAdmins should be able to assign another Mentor
104 params = dicts.merge(params, new_params)
106 super(View, self).__init__(params=params)
108 def _editGet(self, request, entity, form):
109 """See base.View._editGet().
112 form.fields['link_id'].initial = entity.link_id
113 form.fields['student_id'].initial = entity.student.link_id
114 form.fields['mentor_id'].initial = entity.mentor.link_id
116 return super(View, self)._editGet(request, entity, form)
118 def _editPost(self, request, entity, fields):
119 """See base.View._editPost().
122 if not entity:
123 fields['link_id'] = 't%i' % (int(time.time()*100))
124 else:
125 fields['link_id'] = entity.link_id
127 # fill in the scope via call to super
128 super(View, self)._editPost(request, entity, fields)
130 if not entity:
131 # creating a new project so set the program, student and mentor field
132 fields['program'] = fields['scope'].scope
134 filter = {'scope': fields['program'],
135 'link_id': fields['student_id']}
136 fields['student'] = student_logic.logic.getForFields(filter, unique=True)
138 filter = {'scope': fields['scope'],
139 'link_id': fields['mentor_id'],
140 'status': 'active'}
141 fields['mentor'] = mentor_logic.logic.getForFields(filter, unique=True)
144 view = View()
146 admin = decorators.view(view.admin)
147 create = decorators.view(view.create)
148 delete = decorators.view(view.delete)
149 edit = decorators.view(view.edit)
150 list = decorators.view(view.list)
151 public = decorators.view(view.public)
152 export = decorators.view(view.export)
153 pick = decorators.view(view.pick)