madhusudancs / melange (http://code.google.com/p/soc)

I have setup my branch of Melange. Melange, Spice of Creation, is a framework for representing Open Source contribution workflows. It is the webapp powering Google Summer of Code and Google Highly Open Participation.

Clone this repository (size: 15.4 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/madhusudancs/melange/
commit 2585: 37584af2420e
parent 2584: 10858e5d5712
branch: default
Completed the Create and Edit view for GradingSurveyGroup. Note that I've added a TODO to redo _editPost to give us kwargs. Would also get rid of hidden fields elsewhere. Reviewed by: Lennard de Rijk
Daniel Diniz / ajaksu
8 months ago

Changed (Δ4.1 KB):

Up to file-list app/soc/views/models/grading_survey_group.py:

18
18
"""
19
19
20
20
__authors__ = [
21
    '"Daniel Diniz" <ajaksu@gmail.com>',
21
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
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
39
from soc.views.helper import access
40
from soc.views.helper import decorators
41
from soc.views.helper import redirects
27
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
92
class View(base.View):
@@ -49,7 +109,7 @@ class View(base.View):
49
109
    rights['list'] = ['checkIsDeveloper']
50
110
51
111
    new_params = {}
52
    new_params['logic'] = soc.logic.models.grading_survey_group.logic
112
    new_params['logic'] = survey_group_logic
53
113
    new_params['rights'] = rights
54
114
    new_params['name'] = "Grading Survey Group"
55
115
@@ -57,17 +117,83 @@ class View(base.View):
57
117
    new_params['no_create_raw'] = True
58
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
123
    params = dicts.merge(params, new_params)
63
124
64
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
193
view = View()
68
194
69
195
create = decorators.view(view.create)
196
edit = decorators.view(view.edit)
70
197
delete = decorators.view(view.delete)
71
edit = decorators.view(view.edit)
72
198
list = decorators.view(view.list)
73
199
public = decorators.view(view.public)