app/soc/views/models/base.py
changeset 1805 7204ec5ead23
parent 1801 747b0d1115ea
child 1806 d321d5123928
--- a/app/soc/views/models/base.py	Thu Mar 12 13:37:26 2009 +0000
+++ b/app/soc/views/models/base.py	Thu Mar 12 13:38:24 2009 +0000
@@ -185,13 +185,8 @@
       error_export: The error_export value is used as template when
         the key values (as defined by the page's url) do not
         correspond to an existing entity.
-      name: The name value is used to set the entity_type in the
-        context so that the template can refer to it.
-      export_template: The export_template value is used as template
-        to display the export page of the found entity.
-      export_content_type: The export_content_type value is used to set
-        the Content-Type header of the HTTP response.  If empty (or None),
-        public() is called instead.
+      Params is passed to download, refer to it's docstring for more
+      details on how it uses it.
 
     Args:
       request: the standard Django HTTP request object
@@ -201,14 +196,11 @@
       kwargs: the Key Fields for the specified entity
     """
 
-    if not params.get('export_content_type'):
+    if not ('export_content_type' in params) and ('export_function' in params):
       return self.public(request, access_type, page_name=page_name,
                          params=params, **kwargs)
 
     # create default template context for use with any templates
-    context = helper.responses.getUniversalContext(request)
-    helper.responses.useJavaScript(context, params['js_uses_all'])
-    context['page_name'] = page_name
     entity = None
     logic = params['logic']
 
@@ -220,12 +212,34 @@
       entity = logic.getFromKeyFieldsOr404(kwargs)
     except out_of_band.Error, error:
       return helper.responses.errorResponse(
-          error, request, template=params['error_export'], context=context)
+          error, request, template=params['error_export'])
+
+    export_function = params['export_function']
+    data, filename = export_function(entity)
+
+    return self.download(request, data, filename, params)
+
+  def download(self, request, data, filename, params):
+    """Returns data as a downloadable file with the specified name.
 
-    self._export(request, entity, context)
+    Params usage:
+      export_template: The export_template value is used as template
+        to display the export page of the found entity.
+      export_content_type: The export_content_type value is used to set
+        the Content-Type header of the HTTP response.  If empty (or None),
+        public() is called instead.
+      export_extension: The export_extension value is used as the suffix
+        of the file that will be offered for download.
 
-    context['entity'] = entity
-    context['entity_type'] = params['name']
+    Args:
+      request: the standard Django HTTP request object
+      data: the data that should be offered as file content
+      filename: the name the file should have
+      params: a dict with params for this View
+    """
+
+    context = {}
+    context['data'] = data
 
     template = params['export_template']
 
@@ -235,7 +249,7 @@
 
     response_headers = {
         'Content-Disposition': 'attachment; filename=%s%s' % (
-            entity.link_id, export_extension),
+            filename, export_extension),
         }
 
     return helper.responses.respond(request, template, context=context,
@@ -731,16 +745,6 @@
     """
     pass
 
-  def _export(self, request, entity, context):
-    """Performs any required processing to get an entity's export page.
-
-    Args:
-      request: the django request object
-      entity: the entity to export
-      context: the context object
-    """
-    pass
-
   def _editGet(self, request, entity, form):
     """Performs any required processing on the form to get its edit page.