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