Implemented the takeGet view for Surveys.
authorLennard de Rijk <ljvderijk@gmail.com>
Wed, 01 Jul 2009 21:53:20 +0200
changeset 2477 e5629995d118
parent 2476 c27c21beba35
child 2478 985fd974e095
Implemented the takeGet view for Surveys.
app/soc/templates/soc/survey/take.html
app/soc/views/models/survey.py
--- a/app/soc/templates/soc/survey/take.html	Wed Jul 01 17:28:44 2009 +0200
+++ b/app/soc/templates/soc/survey/take.html	Wed Jul 01 21:53:20 2009 +0200
@@ -20,10 +20,6 @@
 <script type="text/javascript" src="/jquery/jquery-growfield.js"></script>
 {% endblock %}
 
-{% block page_title %}
-{{ entity.title }}
-{% endblock %}
-
 {% block header_title %}
 {{ entity.title }}
 {% endblock %}
@@ -35,53 +31,32 @@
 
   {% load forms_helpers %}
 
-  {% if not read_only %}
-    <form method="post">
-  {% endif %}
-
-  {% if read_only %}
-    <form method="get" onsubmit="javascript:return false">
-  {% endif %}
-
   {{ help_text }}
 
-<div style="width:100%; float:left;">
-  <div class="{{ status }}" id="survey_widget">
-
-    <table>
-
-  {# Project Selected for this Survey #}
-  {% if project %}
- <div id="project_link" style="font-size:.8em; margin:3px; display:none;">
- view project:
- <a target="_blank" href="/student_project/show/{{ project.key.name }}" >
- {{ project.title }}
- </a>
- </div>
-  {% endif %}
-
-      {% block form_table %}
-        {% as_twoline_table survey_form %}
-      {% endblock %}
-
-    </table>
-  </div> {# end survey_widget #}
-  </div>
+  <form method="post">
+    <div style="width:100%; float:left;">
+      <div class="{{ status }}" id="survey_widget">
+        <table>
+          {% block form_table %}
+            {% as_twoline_table survey_form %}
+          {% endblock %}
+        </table>
+      </div> {# end survey_widget #}
+    </div>
 
 <br/><br/>
-  <table>
-    <tbody>
-      <tr>
-        <td>
-          {% if not read_only %}
-            <input type="submit" value="Submit" style="font-weight: bold;"/>
-          {% endif %}
-        </td>
-      </tr>
-    </tbody>
-  </table>
-
-    </form>
+    <table>
+      <tbody>
+        <tr>
+          <td>
+            {% if not read_only %}
+              <input type="submit" value="Submit" style="font-weight: bold;"/>
+            {% endif %}
+          </td>
+        </tr>
+      </tbody>
+    </table>
+  </form>
 {% endif %}
 
 {% endblock %}
--- a/app/soc/views/models/survey.py	Wed Jul 01 17:28:44 2009 +0200
+++ b/app/soc/views/models/survey.py	Wed Jul 01 21:53:20 2009 +0200
@@ -501,6 +501,9 @@
     For Args see base.View().public().
     """
 
+    survey_logic = params['logic']
+    record_logic = survey_logic.getRecordLogic()
+
     try:
       entity = self._logic.getFromKeyFieldsOr404(kwargs)
     except out_of_band.Error, error:
@@ -515,38 +518,82 @@
     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
     context['entity'] = entity
 
+    user_entity = user_logic.getForCurrentAccount()
+
+    # try to get an existing SurveyRecord for the current user
+    # TODO(ljvderijk) deal with the SurveyProperty name in subclasses
+    filter = {'survey': entity,
+              'user': user_entity}
+    survey_record = record_logic.getForFields(filter, unique=True)
+
     if request.POST:
       return self.takePost(request, template, context, params, entity,
-                           **kwargs)
+                           survey_record, **kwargs)
     else: #request.GET
       return self.takeGet(request, template, context, params, entity,
-                          **kwargs)
+                          survey_record, **kwargs)
 
-  def takeGet(self, request, template, context, params, entity, **kwargs):
+  def takeGet(self, request, template, context, params, entity, record,
+              **kwargs):
     """Handles the GET request for the Survey's take page.
 
     Args:
         template: the template used for this view
-        entity: the student project entity
+        entity: the Survey entity
         rest: see base.View.public()
     """
 
-    # TODO(ljvderijk) implement takeGet
+    survey_form = surveys.SurveyForm(survey_content=entity.survey_content,
+                                     survey_record=record,
+                                     survey_logic=self._params['logic'])
+
+    survey_form.getFields()
 
-    return http.HttpResponse("Work in Progress")
+    # fill context with the survey and additional information
+    context['survey_form'] = survey_form
+    self.setHelpAndStatus(context, entity, record)
 
-  def takePost(self, request, template, context, params, entity, **kwargs):
+    return responses.respond(request, template, context)
+
+  def takePost(self, request, template, context, params, entity, record,
+               **kwargs):
     """Handles the POST request for the Survey's take page.
 
     Args:
         template: the template used for this view
-        entity: the student project entity
+        entity: the Survey entity
         rest: see base.View.public()
     """
 
     # TODO(ljvderijk) implement takePost
 
-    return httpHttpResponse("Work in Progress")
+    return http.HttpResponse("Work in Progress")
+
+  def setHelpAndStatus(self, context, survey, survey_record):
+    """Get help_text and status for template use.
+
+    Args:
+        context: the context for the view to update
+        survey: a Survey entity
+        survey_record: a SurveyRecordEntity 
+    """
+
+    if not survey.survey_end:
+      survey_end_text = ""
+    else:
+      survey_end_text = " by " + str(
+          survey.survey_end.strftime("%A, %d. %B %Y %I:%M%p"))
+
+    if survey_record:
+      help_text = "Edit and re-submit this survey" + survey_end_text + "."
+      status = "edit"
+    else:
+      help_text = "Please complete this survey" + survey_end_text + "."
+      status = "create"
+
+    # update the context with the help_text and status
+    context_update = dict(status=status, help_text=help_text)
+    context.update(context_update)
 
   def activate(self, request, **kwargs):
     """This is a hack to support the 'Enable grades' button.