Factor download functionality out of export()
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 12 Mar 2009 13:38:24 +0000
changeset 1805 7204ec5ead23
parent 1804 e151c2039a98
child 1806 d321d5123928
Factor download functionality out of export() This way any kind of data can be offered as downloadable file. Patch by: Sverre Rabbelier
app/soc/templates/soc/document/export.html
app/soc/templates/soc/export.html
app/soc/views/helper/params.py
app/soc/views/models/base.py
app/soc/views/models/document.py
--- a/app/soc/templates/soc/document/export.html	Thu Mar 12 13:37:26 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-{% comment %}
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-  http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-{% endcomment %}
-{{ entity.content|safe }}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/export.html	Thu Mar 12 13:38:24 2009 +0000
@@ -0,0 +1,15 @@
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+
+{{ data|safe }}
--- a/app/soc/views/helper/params.py	Thu Mar 12 13:37:26 2009 +0000
+++ b/app/soc/views/helper/params.py	Thu Mar 12 13:38:24 2009 +0000
@@ -220,7 +220,7 @@
          'List %(name_plural)s')]
 
   new_params['public_template'] = 'soc/%(module_name)s/public.html' % params
-  new_params['export_template'] = 'soc/%(module_name)s/export.html' % params
+  new_params['export_template'] = 'soc/export.html' % params
   new_params['create_template'] = 'soc/models/edit.html'
   new_params['edit_template'] = 'soc/models/edit.html'
   new_params['admin_template'] = 'soc/models/admin.html'
--- 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.
 
--- a/app/soc/views/models/document.py	Thu Mar 12 13:37:26 2009 +0000
+++ b/app/soc/views/models/document.py	Thu Mar 12 13:38:24 2009 +0000
@@ -67,6 +67,7 @@
 
     new_params['export_content_type'] = 'text/text'
     new_params['export_extension'] = '.html'
+    new_params['export_function'] = lambda x: (x.content, x.link_id)
     new_params['delete_redirect'] = '/'
 
     new_params['no_create_raw'] = True