|
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 Proposal. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 import datetime |
|
26 import time |
|
27 |
|
28 from django import forms |
|
29 |
|
30 from soc.logic import cleaning |
|
31 from soc.logic import dicts |
|
32 from soc.logic.models import organization as org_logic |
|
33 from soc.logic.models import student as student_logic |
|
34 from soc.views.helper import access |
|
35 from soc.views.helper import decorators |
|
36 from soc.views.helper import redirects |
|
37 from soc.views.helper import widgets |
|
38 from soc.views.models import base |
|
39 from soc.views.models import student as student_view |
|
40 |
|
41 import soc.logic.models.student_proposal |
|
42 |
|
43 |
|
44 class View(base.View): |
|
45 """View methods for the Student Proposal 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 # TODO(ljvderijk) Access checks for different views |
|
57 rights = access.Checker(params) |
|
58 rights['create'] = ['checkIsDeveloper'] |
|
59 rights['edit'] = ['checkIsDeveloper'] |
|
60 rights['delete'] = ['checkIsDeveloper'] |
|
61 # TODO(ljvderijk) public should be host/org/student only |
|
62 rights['public'] = ['checkIsDeveloper'] |
|
63 rights['list'] = ['checkIsDeveloper'] |
|
64 rights['apply'] = ['checkIsDeveloper'] |
|
65 |
|
66 new_params = {} |
|
67 new_params['logic'] = soc.logic.models.student_proposal.logic |
|
68 new_params['rights'] = rights |
|
69 new_params['name'] = "Student Proposal" |
|
70 new_params['url_name'] = "student_proposal" |
|
71 new_params['sidebar_grouping'] = 'Student Proposal' |
|
72 |
|
73 new_params['scope_view'] = student_view |
|
74 new_params['scope_redirect'] = redirects.getCreateRedirect |
|
75 |
|
76 new_params['no_create_with_key_fields'] = True |
|
77 |
|
78 patterns = [ |
|
79 (r'^%(url_name)s/(?P<access_type>apply)/%(scope)s$', |
|
80 'soc.views.models.%(module_name)s.create', |
|
81 'Create a new %(name)s'), |
|
82 ] |
|
83 |
|
84 new_params['extra_django_patterns'] = patterns |
|
85 |
|
86 new_params['extra_dynaexclude'] = ['org', 'program', 'score', |
|
87 'status', 'mentor', 'link_id'] |
|
88 |
|
89 new_params['create_extra_dynafields'] = { |
|
90 'content': forms.fields.CharField(required=True, |
|
91 widget=widgets.FullTinyMCE(attrs={'rows': 25, 'cols': 100})), |
|
92 'scope_path': forms.CharField(widget=forms.HiddenInput, |
|
93 required=True), |
|
94 'organization': forms.CharField(label='Organization Link ID', |
|
95 required=True), |
|
96 'clean_organization': cleaning.clean_link_id('organization'), |
|
97 'clean_additional_info': cleaning.clean_url('additional_info'), |
|
98 'clean': cleaning.validate_student_proposal('organization', |
|
99 'scope_path', student_logic, org_logic), |
|
100 } |
|
101 |
|
102 new_params['edit_extra_dynafields'] = { |
|
103 'organization': forms.CharField(label='Organization Link ID', |
|
104 widget=widgets.ReadOnlyInput), |
|
105 'link_id': forms.CharField(widget=forms.HiddenInput) |
|
106 } |
|
107 |
|
108 # TODO(ljvderijk) students should be able to withdraw their proposal |
|
109 |
|
110 params = dicts.merge(params, new_params) |
|
111 |
|
112 super(View, self).__init__(params=params) |
|
113 |
|
114 def _editGet(self, request, entity, form): |
|
115 """See base.View._editGet(). |
|
116 """ |
|
117 |
|
118 form.fields['link_id'].initial = entity.link_id |
|
119 form.fields['organization'].initial = entity.org.link_id |
|
120 |
|
121 return super(View, self)._editGet(request, entity, form) |
|
122 |
|
123 def _editPost(self, request, entity, fields): |
|
124 """See base.View._editPost(). |
|
125 """ |
|
126 |
|
127 if not entity: |
|
128 fields['link_id'] = 't%i' % (time.time()) |
|
129 else: |
|
130 fields['link_id'] = entity.link_id |
|
131 |
|
132 # fill in the scope via call to super |
|
133 super(View, self)._editPost(request, entity, fields) |
|
134 |
|
135 if not entity: |
|
136 # creating a new application so set the program and org field |
|
137 fields['program'] = fields['scope'].scope |
|
138 |
|
139 filter = {'scope': fields['program'], |
|
140 'link_id': fields['organization']} |
|
141 fields['org'] = org_logic.logic.getForFields(filter, unique=True) |
|
142 |
|
143 # explicitly change the last_modified_on since the content has been edited |
|
144 fields['last_modified_on'] = datetime.datetime.now() |
|
145 |
|
146 def _public(self, request, entity, context): |
|
147 """See base.View._public(). |
|
148 """ |
|
149 |
|
150 context['student_name'] = entity.scope.name() |
|
151 |
|
152 if entity.mentor: |
|
153 context['mentor_name'] = entity.mentor.name() |
|
154 else: |
|
155 context['mentor_name'] = "No mentor assigned" |
|
156 |
|
157 view = View() |
|
158 |
|
159 admin = view.admin |
|
160 create = view.create |
|
161 delete = view.delete |
|
162 edit = view.edit |
|
163 list = view.list |
|
164 public = view.public |
|
165 export = view.export |
|
166 pick = view.pick |