16 |
16 |
17 """Views for GradingSurveyGroup. |
17 """Views for GradingSurveyGroup. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
|
21 '"Daniel Diniz" <ajaksu@gmail.com>', |
21 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
22 ] |
23 ] |
23 |
24 |
24 |
25 |
|
26 import time |
|
27 |
|
28 from google.appengine.ext.db import djangoforms |
|
29 |
25 from soc.logic import dicts |
30 from soc.logic import dicts |
|
31 from soc.logic.models.program import logic as program_logic |
|
32 from soc.logic.models.survey import grading_logic |
|
33 from soc.logic.models.survey import project_logic |
|
34 from soc.logic.models.user import logic as user_logic |
|
35 from soc.logic.models.grading_survey_group import logic as survey_group_logic |
|
36 from soc.models.grading_survey_group import GradingSurveyGroup |
|
37 from soc.models.grading_project_survey import GradingProjectSurvey |
|
38 from soc.models.project_survey import ProjectSurvey |
26 from soc.views.helper import access |
39 from soc.views.helper import access |
|
40 from soc.views.helper import decorators |
|
41 from soc.views.helper import redirects |
27 from soc.views.models import base |
42 from soc.views.models import base |
28 |
43 |
29 import soc.logic.models.grading_survey_group |
44 |
|
45 class GroupForm(djangoforms.ModelForm): |
|
46 """Form for creating a GradingSurveyGroup. |
|
47 """ |
|
48 |
|
49 grading_survey = djangoforms.ModelChoiceField(GradingProjectSurvey) |
|
50 |
|
51 student_survey = djangoforms.ModelChoiceField(ProjectSurvey, required=False) |
|
52 |
|
53 def __init__(self, *args, **kwargs): |
|
54 """Process field names for readable display and initialize form. |
|
55 """ |
|
56 |
|
57 # use survey titles in drop-downs |
|
58 self.choiceTitles('grading_survey', grading_logic) |
|
59 self.choiceTitles('student_survey', project_logic) |
|
60 |
|
61 super(GroupForm, self).__init__(*args, **kwargs) |
|
62 |
|
63 def choiceTitles(self, field, logic): |
|
64 """Fetch entity titles for choice field entries. |
|
65 """ |
|
66 |
|
67 # TODO(ajaksu): subclass ModelChoiceField so we don't need this method |
|
68 choice_list = [] |
|
69 |
|
70 model = logic.getModel() |
|
71 |
|
72 for value, text in tuple(self.base_fields[field].choices): |
|
73 if value: |
|
74 entity = model.get(value) |
|
75 text = entity.title |
|
76 choice_list.append((value,text)) |
|
77 |
|
78 choices = tuple(choice_list) |
|
79 |
|
80 self.base_fields[field].choices = choices |
|
81 |
|
82 class Meta: |
|
83 """Inner Meta class for fetching fields from model. |
|
84 """ |
|
85 model = GradingSurveyGroup |
|
86 |
|
87 # exclude the necessary fields from the form |
|
88 exclude = ['link_id', 'scope', 'scope_path', 'last_update_started', |
|
89 'last_update_complete'] |
30 |
90 |
31 |
91 |
32 class View(base.View): |
92 class View(base.View): |
33 """View methods for the GradingSurveyGroup model. |
93 """View methods for the GradingSurveyGroup model. |
34 """ |
94 """ |
47 rights['delete'] = ['checkIsDeveloper'] |
107 rights['delete'] = ['checkIsDeveloper'] |
48 rights['show'] = ['checkIsDeveloper'] |
108 rights['show'] = ['checkIsDeveloper'] |
49 rights['list'] = ['checkIsDeveloper'] |
109 rights['list'] = ['checkIsDeveloper'] |
50 |
110 |
51 new_params = {} |
111 new_params = {} |
52 new_params['logic'] = soc.logic.models.grading_survey_group.logic |
112 new_params['logic'] = survey_group_logic |
53 new_params['rights'] = rights |
113 new_params['rights'] = rights |
54 new_params['name'] = "Grading Survey Group" |
114 new_params['name'] = "Grading Survey Group" |
55 |
115 |
56 new_params['no_admin'] = True |
116 new_params['no_admin'] = True |
57 new_params['no_create_raw'] = True |
117 new_params['no_create_raw'] = True |
58 new_params['no_create_with_key_fields'] = True |
118 new_params['no_create_with_key_fields'] = True |
59 |
119 |
60 patterns = [] |
120 new_params['create_form'] = GroupForm |
|
121 new_params['edit_form'] = GroupForm |
61 |
122 |
62 params = dicts.merge(params, new_params) |
123 params = dicts.merge(params, new_params) |
63 |
124 |
64 super(View, self).__init__(params=params) |
125 super(View, self).__init__(params=params) |
65 |
126 |
|
127 @decorators.merge_params |
|
128 @decorators.check_access |
|
129 def create(self, request, access_type, |
|
130 page_name=None, params=None, **kwargs): |
|
131 """Pass the correct survey queries to GroupForm. |
|
132 |
|
133 For params see base.View.create(). |
|
134 """ |
|
135 |
|
136 self.setQueries(kwargs['scope_path'], params) |
|
137 |
|
138 return super(View, self).create(request, access_type, page_name=page_name, |
|
139 params=params, **kwargs) |
|
140 |
|
141 @decorators.merge_params |
|
142 @decorators.check_access |
|
143 def edit(self, request, access_type, |
|
144 page_name=None, params=None, seed=None, **kwargs): |
|
145 """Pass the correct survey queries to GroupForm. |
|
146 |
|
147 For params see base.View.edit(). |
|
148 """ |
|
149 |
|
150 self.setQueries(kwargs['scope_path'], params) |
|
151 |
|
152 return super(View, self).edit(request, access_type, page_name=page_name, |
|
153 params=params, seed=seed, **kwargs) |
|
154 |
|
155 def _editPost(self, request, entity, fields): |
|
156 """See base.View._editPost(). |
|
157 """ |
|
158 |
|
159 if not entity: |
|
160 # generate a unique link_id |
|
161 fields['link_id'] = 't%i' % (int(time.time()*100)) |
|
162 |
|
163 # TODO: seriously redesign _editPost to pass along kwargs |
|
164 fields['scope_path'] = fields['grading_survey'].scope_path |
|
165 else: |
|
166 fields['link_id'] = entity.link_id |
|
167 |
|
168 # fill in the scope via call to super |
|
169 super(View, self)._editPost(request, entity, fields) |
|
170 |
|
171 def setQueries(self, program, params): |
|
172 """Add program filtering queries to the GroupForm. |
|
173 """ |
|
174 |
|
175 # fetch the program |
|
176 program = program_logic.getFromKeyNameOr404(program) |
|
177 |
|
178 # filter grading surveys by program and use title for display |
|
179 grading_query = grading_logic.getQueryForFields(filter={'scope':program}) |
|
180 |
|
181 # filter project surveys by program and use title for display |
|
182 student_query = project_logic.getQueryForFields(filter={'scope':program}) |
|
183 |
|
184 if params.get('edit_form'): |
|
185 params['edit_form'].base_fields['student_survey'].query = student_query |
|
186 params['edit_form'].base_fields['grading_survey'].query = grading_query |
|
187 |
|
188 if params.get('create_form'): |
|
189 params['create_form'].base_fields['student_survey'].query = student_query |
|
190 params['create_form'].base_fields['grading_survey'].query = grading_query |
|
191 |
66 |
192 |
67 view = View() |
193 view = View() |
68 |
194 |
69 create = decorators.view(view.create) |
195 create = decorators.view(view.create) |
|
196 edit = decorators.view(view.edit) |
70 delete = decorators.view(view.delete) |
197 delete = decorators.view(view.delete) |
71 edit = decorators.view(view.edit) |
|
72 list = decorators.view(view.list) |
198 list = decorators.view(view.list) |
73 public = decorators.view(view.public) |
199 public = decorators.view(view.public) |