40 from soc.views.helper import decorators |
42 from soc.views.helper import decorators |
41 from soc.views.helper import redirects |
43 from soc.views.helper import redirects |
42 from soc.views.models import base |
44 from soc.views.models import base |
43 |
45 |
44 |
46 |
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'] |
|
90 |
|
91 |
|
92 class View(base.View): |
47 class View(base.View): |
93 """View methods for the GradingSurveyGroup model. |
48 """View methods for the GradingSurveyGroup model. |
94 """ |
49 """ |
95 |
50 |
96 def __init__(self, params=None): |
51 def __init__(self, params=None): |
116 |
71 |
117 new_params['no_admin'] = True |
72 new_params['no_admin'] = True |
118 new_params['no_create_raw'] = True |
73 new_params['no_create_raw'] = True |
119 new_params['no_create_with_key_fields'] = True |
74 new_params['no_create_with_key_fields'] = True |
120 |
75 |
121 new_params['create_form'] = GroupForm |
76 new_params['create_extra_dynaproperties'] = { |
122 new_params['edit_form'] = GroupForm |
77 'grading_survey': djangoforms.ModelChoiceField( |
|
78 GradingProjectSurvey, required=True), |
|
79 'student_survey': djangoforms.ModelChoiceField(ProjectSurvey, |
|
80 required=False), |
|
81 } |
|
82 |
|
83 new_params['extra_dynaexclude'] = ['link_id', 'scope', 'scope_path', |
|
84 'last_update_started', |
|
85 'last_update_complete'] |
|
86 |
|
87 new_params['edit_extra_dynaproperties'] = { |
|
88 'link_id': forms.CharField(widget=forms.HiddenInput), |
|
89 } |
123 |
90 |
124 params = dicts.merge(params, new_params) |
91 params = dicts.merge(params, new_params) |
125 |
92 |
126 super(View, self).__init__(params=params) |
93 super(View, self).__init__(params=params) |
127 |
94 |
132 """Pass the correct survey queries to GroupForm. |
99 """Pass the correct survey queries to GroupForm. |
133 |
100 |
134 For params see base.View.create(). |
101 For params see base.View.create(). |
135 """ |
102 """ |
136 |
103 |
137 self.setQueries(kwargs['scope_path'], params) |
104 self.setQueries(kwargs['scope_path'], params['create_form']) |
138 |
105 |
139 return super(View, self).create(request, access_type, page_name=page_name, |
106 return super(View, self).create(request, access_type, page_name=page_name, |
140 params=params, **kwargs) |
107 params=params, **kwargs) |
141 |
108 |
142 @decorators.merge_params |
109 @decorators.merge_params |
146 """Pass the correct survey queries to GroupForm. |
113 """Pass the correct survey queries to GroupForm. |
147 |
114 |
148 For params see base.View.edit(). |
115 For params see base.View.edit(). |
149 """ |
116 """ |
150 |
117 |
151 self.setQueries(kwargs['scope_path'], params) |
118 self.setQueries(kwargs['scope_path'], params['edit_form']) |
152 |
119 |
153 return super(View, self).edit(request, access_type, page_name=page_name, |
120 return super(View, self).edit(request, access_type, page_name=page_name, |
154 params=params, seed=seed, **kwargs) |
121 params=params, seed=seed, **kwargs) |
|
122 |
|
123 def _editGet(self, request, entity, form): |
|
124 """Performs any required processing on the form to get its edit page. |
|
125 |
|
126 Args: |
|
127 request: the django request object |
|
128 entity: the entity to get |
|
129 form: the django form that will be used for the page |
|
130 """ |
|
131 |
|
132 form.fields['link_id'].initial = entity.link_id |
|
133 |
|
134 return super(View,self)._editGet(request, entity,form) |
155 |
135 |
156 def _editPost(self, request, entity, fields): |
136 def _editPost(self, request, entity, fields): |
157 """See base.View._editPost(). |
137 """See base.View._editPost(). |
158 """ |
138 """ |
159 |
139 |
165 fields['scope_path'] = fields['grading_survey'].scope_path |
145 fields['scope_path'] = fields['grading_survey'].scope_path |
166 else: |
146 else: |
167 fields['link_id'] = entity.link_id |
147 fields['link_id'] = entity.link_id |
168 |
148 |
169 # fill in the scope via call to super |
149 # fill in the scope via call to super |
170 super(View, self)._editPost(request, entity, fields) |
150 return super(View, self)._editPost(request, entity, fields) |
171 |
151 |
172 def setQueries(self, program, params): |
152 def setQueries(self, program_keyname, group_form): |
173 """Add program filtering queries to the GroupForm. |
153 """Add program filtering queries to the GroupForm. |
|
154 |
|
155 Args: |
|
156 program_keyname: keyname of the program to filter on |
|
157 group_form: DynaForm instance to set the queries for |
174 """ |
158 """ |
175 |
159 |
176 # fetch the program |
160 # fetch the program |
177 program = program_logic.getFromKeyNameOr404(program) |
161 program = program_logic.getFromKeyNameOr404(program_keyname) |
178 |
162 |
179 # filter grading surveys by program and use title for display |
163 # filter grading surveys by program and use title for display |
180 grading_query = grading_logic.getQueryForFields(filter={'scope':program}) |
164 grading_query = grading_logic.getQueryForFields( |
|
165 filter={'scope_path':program_keyname}) |
181 |
166 |
182 # filter project surveys by program and use title for display |
167 # filter project surveys by program and use title for display |
183 student_query = project_logic.getQueryForFields(filter={'scope':program}) |
168 student_query = project_logic.getQueryForFields( |
184 |
169 filter={'scope_path':program_keyname}) |
185 if params.get('edit_form'): |
170 |
186 params['edit_form'].base_fields['student_survey'].query = student_query |
171 group_form.base_fields['grading_survey'].query = grading_query |
187 params['edit_form'].base_fields['grading_survey'].query = grading_query |
172 group_form.base_fields['student_survey'].query = student_query |
188 |
173 |
189 if params.get('create_form'): |
174 # use survey titles in drop-downs |
190 params['create_form'].base_fields['student_survey'].query = student_query |
175 self.choiceTitles(group_form, 'grading_survey', grading_logic) |
191 params['create_form'].base_fields['grading_survey'].query = grading_query |
176 self.choiceTitles(group_form, 'student_survey', project_logic) |
|
177 |
|
178 |
|
179 def choiceTitles(self, group_form, field, logic): |
|
180 """Fetch entity titles for choice field entries. |
|
181 |
|
182 Args: |
|
183 group_form: The form to set the choice field entries for |
|
184 field: the field_name to set the choice entries for |
|
185 logic: the logic for the model to set the choice entries for |
|
186 """ |
|
187 |
|
188 # TODO(ajaksu): subclass ModelChoiceField so we don't need this method |
|
189 choice_list = [] |
|
190 |
|
191 model = logic.getModel() |
|
192 |
|
193 for value, text in tuple(group_form.base_fields[field].choices): |
|
194 if value: |
|
195 entity = model.get(value) |
|
196 text = '%s (%s)' % (entity.title, entity.link_id) |
|
197 choice_list.append((value,text)) |
|
198 |
|
199 choices = tuple(choice_list) |
|
200 |
|
201 group_form.base_fields[field].choices = choices |
192 |
202 |
193 |
203 |
194 view = View() |
204 view = View() |
195 |
205 |
196 create = decorators.view(view.create) |
206 create = decorators.view(view.create) |