diff --git a/README.rst b/README.rst index ce8032c..5043658 100644 --- a/README.rst +++ b/README.rst @@ -19,6 +19,8 @@ desire. This should be an iterable of 2-tuples, each containing::: By default, up to 10,000 rows will be exported, though this can be changed easily by setting the ``export_queryset_limit`` attribute on your ModelAdmin. +You may set up custom csv fields at ``csv_list_display`` deiferent from default ``list_display``. + Users of Django > 1.3 should use the 2.x release line or the master branch in the git repository (https://github.com/jwineinger/django-exportable-admin). @@ -68,7 +70,8 @@ Complex example: from models import MyModel class MyModelAdmin(ExportableAdmin): - list_display = ('field1','field2','calculated_field') + list_display = ['field1','field2','calculated_field'] + csv_list_display = list_display + ["email"] # only output 100 rows max export_queryset_limit = 100 diff --git a/django_exportable_admin/admin.py b/django_exportable_admin/admin.py index 35cf95f..67dfa08 100644 --- a/django_exportable_admin/admin.py +++ b/django_exportable_admin/admin.py @@ -1,3 +1,4 @@ +import datetime from django.contrib import admin from django.conf.urls.defaults import patterns, url from django.template.defaultfilters import slugify @@ -10,16 +11,21 @@ class ExportableAdmin(admin.ModelAdmin): changelist export. Subclassing this class itself will do nothing unless you set export_formats on your ModelAdmin instance. See the other provided subclasses which are already setup for CSV, Pipe, and both. - + Note: do not override change_list_template or you will not get the "Export ..." button on your changelist page. """ + # + is_csv_export = False # use a custom changelist template which adds "Export ..." button(s) change_list_template = 'django_exportable_admin/change_list_exportable.html' + # + csv_list_display = [] + # export 10,000 results by default export_queryset_limit = 10000 - + # an iterable of 2-tuples of (format-name, format-delimiter), such as: # ((u'CSV', u','), (u'Pipe', u'|'),) export_formats = tuple() @@ -33,6 +39,17 @@ def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_firs return self.paginator(queryset, self.export_queryset_limit, 0, True) return self.paginator(queryset, per_page, orphans, allow_empty_first_page) + def get_list_display(self, request): + """ + Return a sequence containing the fields to be displayed on the + changelist if there is csv export and csv_list_display if it is defined + """ + + if self.is_csv_export: + return getattr(self, "csv_list_display", self.list_display) + else: + return self.list_display + def get_export_buttons(self, request): """ Returns a iterable of 2-tuples which contain: @@ -42,9 +59,12 @@ def get_export_buttons(self, request): button for each export format. """ app, mod = self.model._meta.app_label, self.model._meta.module_name + + query = request.META.get("QUERY_STRING", "") + return ( ('Export %s' % format_name, - reverse("admin:%s_%s_export_%s" % (app, mod, format_name.lower()))) + reverse("admin:%s_%s_export_%s" % (app, mod, format_name.lower())) + "?%s" % query) for format_name, delimiter in self.export_formats ) @@ -56,23 +76,29 @@ def changelist_view(self, request, extra_context=None): it after we get the TemplateResponse back. """ if extra_context and extra_context['export_delimiter']: + self.is_csv_export = True # set this attr for get_paginator() request.is_export_request = True response = super(ExportableAdmin, self).changelist_view(request, extra_context) # response is a TemplateResponse so we can change the template + filename = "%s-%s-%s.csv" % ( + slugify(self.model._meta.verbose_name), + slugify(request.META.get("QUERY_STRING", "")), + slugify(datetime.date.today()) + ) response.template_name = 'django_exportable_admin/change_list_csv.html' response['Content-Type'] = 'text/csv' - response['Content-Disposition'] = 'attachment; filename=%s.csv' % slugify(self.model._meta.verbose_name) + response['Content-Disposition'] = 'attachment; filename=%s' % filename return response extra_context = extra_context or {} extra_context.update({ - 'export_buttons' : self.get_export_buttons(request), + 'export_buttons': self.get_export_buttons(request), }) return super(ExportableAdmin, self).changelist_view(request, extra_context) def get_urls(self): """ - Add URL patterns for the export formats. Really all these URLs do are + Add URL patterns for the export formats. Really all these URLs do are set extra_context to contain the export_delimiter for the template which actually generates the "CSV". """ @@ -91,10 +117,11 @@ def get_urls(self): my_urls = patterns('', *new_urls) return my_urls + urls + class CSVExportableAdmin(ExportableAdmin): """ ExportableAdmin subclass which adds export to CSV functionality. - + Note: do not override change_list_template or you will not get the "Export ..." button on your changelist page. """ @@ -102,10 +129,11 @@ class CSVExportableAdmin(ExportableAdmin): (u'CSV', u','), ) + class PipeExportableAdmin(ExportableAdmin): """ ExportableAdmin subclass which adds export to Pipe functionality. - + Note: do not override change_list_template or you will not get the "Export ..." button on your changelist page. """ @@ -113,11 +141,12 @@ class PipeExportableAdmin(ExportableAdmin): (u'Pipe', u'|'), ) + class MultiExportableAdmin(ExportableAdmin): """ ExportableAdmin subclass which adds export to CSV and Pipe functionality. - + Note: do not override change_list_template or you will not get the "Export ..." buttons on your changelist page. """