Redone the list_info function for the Project's manage page.
authorLennard de Rijk <ljvderijk@gmail.com>
Tue, 04 Aug 2009 21:46:03 +0200
changeset 2730 d1cfed8da027
parent 2729 f7d4e2c3b697
child 2731 4d143278f9a0
Redone the list_info function for the Project's manage page. This will reduce the overhead because Django calls the info method over and over again.
app/soc/views/helper/list_info.py
app/soc/views/models/student_project.py
--- a/app/soc/views/helper/list_info.py	Tue Aug 04 21:29:49 2009 +0200
+++ b/app/soc/views/helper/list_info.py	Tue Aug 04 21:46:03 2009 +0200
@@ -120,11 +120,15 @@
   return list_content
 
 
-def getProjectSurveyInfoForProject(project_entity, survey_params):
-  """Returns a function that returns info for listing Surveys and if possible
-  their accompanying record.
+def setProjectSurveyInfoForProject(list_content, project_entity,
+                                   survey_params):
+  """Sets the list info to a function for the given list.
+  
+  This function contains the information used for showing ProjectSurvey
+  records on the StudentProject manage page.
 
   Args:
+    list_content: list content for which to set the info
     project_entity: a StudentProject entity
     survey_params: params for the view of the type of Survey that is listed
   """
@@ -132,13 +136,11 @@
   survey_logic = survey_params['logic']
   record_logic = survey_logic.getRecordLogic()
 
-  def wrapper(survey_entity, _):
-    """Wrapper method.
+  # store the needed info since Django calls the wrapper method for every
+  # info call.
+  info_storage = {}
 
-    Args:
-      survey_entity: a ProjectSurvey (or subclass) entity
-    """
-
+  for survey_entity in list_content['data']:
     # try to retrieve the SurveyRecord for the given Survey and Project
     fields = {'survey': survey_entity,
               'project': project_entity}
@@ -161,5 +163,12 @@
     info['take_url'] = redirects.getTakeProjectSurveyRedirect(
         project_entity, take_redirect_info)
 
-    return info
-  return wrapper
+    info_storage[survey_entity.key()] = info
+
+  def wrapper(item, _):
+    """Wrapper method.
+    """
+    return info_storage[item.key()]
+
+  list_content['info'] = (wrapper, None)
+  return list_content
--- a/app/soc/views/models/student_project.py	Tue Aug 04 21:29:49 2009 +0200
+++ b/app/soc/views/models/student_project.py	Tue Aug 04 21:46:03 2009 +0200
@@ -489,8 +489,6 @@
     gps_params['list_key_order'] = None
     gps_params['list_heading'] = gps_params['manage_student_project_heading']
     gps_params['list_row'] = gps_params['manage_student_project_row']
-    gps_params['list_info'] = (
-        list_info.getProjectSurveyInfoForProject(entity, gps_params), None)
 
     # list all surveys for this Project's Program
     fields['scope_path'] = entity.program.key().id_or_name()
@@ -500,14 +498,13 @@
 
     gps_list = lists.getListContent(
         request, gps_params, fields, idx=0)
+    list_info.setProjectSurveyInfoForProject(gps_list, entity, gps_params)
 
     # get the ProjectSurvey list
     ps_params = project_survey_view.getParams().copy()
     ps_params['list_key_order'] = None
     ps_params['list_heading'] = ps_params['manage_student_project_heading']
     ps_params['list_row'] = ps_params['manage_student_project_row']
-    ps_params['list_info'] = (
-        list_info.getProjectSurveyInfoForProject(entity, ps_params), None)
 
     ps_params['list_description'] = \
         'List of all Student Evaluations for this Project'
@@ -517,6 +514,7 @@
     fields['scope_path'] = entity.program.key().id_or_name()
     ps_list = lists.getListContent(
         request, ps_params, fields, idx=1)
+    list_info.setProjectSurveyInfoForProject(ps_list, entity, ps_params)
 
     # store both lists in the content
     content = [gps_list, ps_list]