Pre-storing the information for setStudentProjectSurveyInfo.
authorLennard de Rijk <ljvderijk@gmail.com>
Thu, 30 Jul 2009 10:49:19 +0200
changeset 2691 f8e0a007d99b
parent 2690 730676c00907
child 2692 b57572c43b32
Pre-storing the information for setStudentProjectSurveyInfo. This is a rename from getStudentProjectSurveyInfo because it now sets the info directly in the list content. This is done because Django queries the wrapper method every time the info is called in the template.
app/soc/views/helper/list_info.py
app/soc/views/models/student_project.py
--- a/app/soc/views/helper/list_info.py	Thu Jul 30 10:05:22 2009 +0200
+++ b/app/soc/views/helper/list_info.py	Thu Jul 30 10:49:19 2009 +0200
@@ -47,18 +47,28 @@
   return wrapper
 
 
-def getStudentProjectSurveyInfo(program_entity):
-  """Returns a function that returns information used in a Student Project
-  table to show how many evaluations have been available and how
+def setStudentProjectSurveyInfo(list_content, program_entity):
+  """Sets the list info to a method that returns information used in a Student
+  Project table to show how many evaluations have been available and how
   many have been taken.
 
   Args:
-    program_entity: the program to check the total amount of
+    list_content: list content for which to set the info
+    program_entity: the Program to check the total amount of
                     (Grading)ProjctSurveys for
+
+  Returns:
+    The original list_content with info set
   """
 
   from soc.logic.models.survey import grading_logic as grading_survey_logic
   from soc.logic.models.survey import project_logic as project_survey_logic
+  from soc.logic.models.survey_record import grading_logic
+  from soc.logic.models.survey_record import project_logic
+
+  if not list_content:
+    # this can happen because of the need_content parameter for getListContent
+    return list_content
 
   fields = {'scope_path': program_entity.key().id_or_name()}
 
@@ -78,13 +88,11 @@
     if not grading_survey_logic.hasRecord(grading_survey):
       grading_survey_count = grading_survey_count - 1
 
-  def wrapper(item, _):
-    """Wrapper method.
-    """
+  # Pre-store the needed info since Django calls the wrapper method for every
+  # info call.
+  info_storage = {}
 
-    from soc.logic.models.survey_record import grading_logic
-    from soc.logic.models.survey_record import project_logic
-
+  for item in list_content['data']:
     fields = {'project': item}
 
     # count the amount of records we have on store for this project
@@ -96,8 +104,15 @@
             'grading_project_surveys_total': grading_survey_count,
             'grading_project_surveys_completed': grading_record_count}
 
-    return info
-  return wrapper
+    info_storage[item.key()] = info
+
+  def wrapper(item, _):
+    """Wrapper method.
+    """
+    return info_storage[item.key()]
+
+  list_content['info'] = (wrapper, None)
+  return list_content
 
 
 def getProjectSurveyInfoForProject(project_entity, survey_params):
--- a/app/soc/views/models/student_project.py	Thu Jul 30 10:05:22 2009 +0200
+++ b/app/soc/views/models/student_project.py	Thu Jul 30 10:49:19 2009 +0200
@@ -570,8 +570,6 @@
     list_params = params.copy()
     list_params['list_heading'] = params['manage_overview_heading']
     list_params['list_row'] = params['manage_overview_row']
-    list_params['list_info'] = (
-        list_info.getStudentProjectSurveyInfo(org_entity.scope), None)
 
     #list all active projects
     fields['status'] = 'accepted'
@@ -582,6 +580,9 @@
 
     active_list = lists.getListContent(
         request, active_params, fields, idx=0)
+    # set the needed info
+    active_list = list_info.setStudentProjectSurveyInfo(active_list,
+                                                        org_entity.scope)
 
     # list all failed projects
     fields['status'] = 'failed'
@@ -592,6 +593,9 @@
 
     failed_list = lists.getListContent(
         request, failed_params, fields, idx=1, need_content=True)
+    # set the needed info
+    failed_list = list_info.setStudentProjectSurveyInfo(failed_list,
+                                                        org_entity.scope)
 
     #list all completed projects
     fields['status'] = 'completed'
@@ -603,6 +607,9 @@
 
     completed_list = lists.getListContent(
         request, completed_params, fields, idx=2, need_content=True)
+    # set the needed info
+    completed_list = list_info.setStudentProjectSurveyInfo(completed_list,
+                                                           org_entity.scope)
 
     # always show the list with active projects
     content = [active_list]