Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 564b054a9f8c9ac3f439ada6d04dec9be805ee3b Jonas Obrist committed May 26, 2010
3 .gitignore
@@ -0,0 +1,3 @@
+*.egg-info
+*.pyc
+.*project
24 LICENSE
@@ -0,0 +1,24 @@
+Copyright (c) 2010, Jonas Obrist
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of Jonas Obrist nor the names of its contributors may be
+ used to endorse or promote products derived from this software without
+ specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL JONAS OBRIST BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 MANIFEST.in
@@ -0,0 +1,2 @@
+recursive-include csvexport/templates *
+include LICENSE
1 csvexport/__init__.py
@@ -0,0 +1 @@
+__version__ = '0.0.1'
BIN csvexport/__init__.pyc
Binary file not shown.
70 csvexport/admin.py
@@ -0,0 +1,70 @@
+from datetime import datetime
+from django.contrib import admin
+from django.contrib.admin.util import unquote
+from django.http import HttpResponse
+from django.utils.functional import update_wrapper
+import csv
+import re
+
+class CSVExportableAdmin(admin.ModelAdmin):
+ csv_export_url = '~csv/'
+ csv_export_dialect = 'excel'
+ csv_export_fmtparam = {
+ 'delimiter': ',',
+ 'quotechar': '\\',
+ 'quoting': csv.QUOTE_MINIMAL,
+ }
+ change_list_template = 'csvexport/change_list.html'
+
+ def get_urls(self):
+ from django.conf.urls.defaults import patterns, url
+
+ def wrap(view):
+ def wrapper(*args, **kwargs):
+ return self.admin_site.admin_view(view)(*args, **kwargs)
+ return update_wrapper(wrapper, view)
+
+ info = self.model._meta.app_label, self.model._meta.module_name
+
+ urlpatterns = patterns('',
+ url(r'^%s$' % re.escape(self.csv_export_url),
+ wrap(self.csv_export),
+ name='%s_%s_csv_export' % info),
+ )
+ urlpatterns += super(CSVExportableAdmin, self).get_urls()
+ return urlpatterns
+
+ def csv_export(self, request):
+ fields = self.get_csv_export_fields(request)
+ headers = [f[1] for f in fields]
+ response = HttpResponse(mimetype='text/csv')
+ response['Content-Disposition'] = 'attachment; filename=%s' % self.get_csv_export_filename(request)
+ writer = csv.writer(response, self.csv_export_dialect, **self.csv_export_fmtparam)
+ writer.writerow(headers)
+ for row in self.model.objects.all():
+ csvrow = [f.encode('utf-8') if isinstance(f, unicode) else f for f in [self.csv_resolve_field(row, f) for f in fields]]
+ writer.writerow(csvrow)
+ return response
+
+ def get_csv_export_fields(self, request):
+ """
+ Return a sequence of tuples which should be included in the export.
+ """
+ return [(f.name, f.verbose_name) for f in self.model._meta.fields]
+
+ def get_csv_export_filename(self, request):
+ ts = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
+ return '%s_%s_%s_export.csv' % (ts, self.model._meta.app_label, self.model._meta.module_name)
+
+ def csv_resolve_field(self, row, fieldname):
+ if isinstance(row, basestring):
+ return getattr(row, fieldname)
+ else:
+ obj = row
+ for bit in fieldname:
+ obj = getattr(obj, bit)
+ return obj
+
+ def changelist_view(self, request, extra_context=None):
+ extra_context = {'csv_export_url': self.csv_export_url}
+ return super(CSVExportableAdmin, self).changelist_view(request, extra_context)
BIN csvexport/admin.pyc
Binary file not shown.
0 csvexport/models.py
No changes.
BIN csvexport/models.pyc
Binary file not shown.
16 csvexport/templates/csvexport/change_list.html
@@ -0,0 +1,16 @@
+{% extends "admin/change_list.html" %}
+
+{% load i18n %}
+
+{% block object-tools %}
+ <ul class="object-tools">
+ <li><a href="{{ csv_export_url }}" class="addlink">{% trans "Export as CSV" %}</a></li>
+ {% if has_add_permission %}
+ <li>
+ <a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">
+ {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
+ </a>
+ </li>
+ {% endif %}
+ </ul>
+{% endblock %}
18 setup.py
@@ -0,0 +1,18 @@
+from setuptools import setup, find_packages
+
+version = __import__('csvexport').__version__
+
+setup(
+ name = 'django-csv-export',
+ version = version,
+ description = 'Django CSV Export',
+ author = 'Jonas Obrist',
+ url = 'http://github.com/ojii/django-csv-export',
+ packages = find_packages(),
+ zip_safe=False,
+ package_data={
+ 'csvexport': [
+ 'templates/csvexport/*.html',
+ ],
+ },
+)

0 comments on commit 564b054

Please sign in to comment.
Something went wrong with that request. Please try again.