2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2016-05-26 22:44:10 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-05-21 23:57:36 +02:00
|
|
|
import csv
|
|
|
|
from django.http import HttpResponse, HttpResponseForbidden
|
|
|
|
from django.template.defaultfilters import slugify
|
2016-05-24 00:40:55 +02:00
|
|
|
from django.apps import apps
|
2016-05-21 23:57:36 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-21 23:57:36 +02:00
|
|
|
def export(qs, fields=None):
|
|
|
|
model = qs.model
|
2016-05-23 23:57:03 +02:00
|
|
|
response = HttpResponse(content_type='text/csv')
|
2016-07-10 00:26:02 +02:00
|
|
|
response['Content-Disposition'] = 'attachment; filename=%s.csv' \
|
|
|
|
% slugify(model.__name__)
|
2016-05-21 23:57:36 +02:00
|
|
|
writer = csv.writer(response)
|
|
|
|
# Write headers to CSV file
|
|
|
|
if fields:
|
|
|
|
headers = fields
|
|
|
|
else:
|
|
|
|
headers = []
|
|
|
|
for field in model._meta.fields:
|
|
|
|
headers.append(field.name)
|
|
|
|
writer.writerow(headers)
|
|
|
|
# Write data to CSV file
|
|
|
|
for obj in qs:
|
|
|
|
row = []
|
|
|
|
for field in headers:
|
|
|
|
if field in headers:
|
|
|
|
val = getattr(obj, field)
|
|
|
|
if callable(val):
|
|
|
|
val = val()
|
|
|
|
row.append(val)
|
|
|
|
writer.writerow(row)
|
|
|
|
# Return CSV file to browser as download
|
|
|
|
return response
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
def admin_list_export(request, model_name, app_label, queryset=None,
|
|
|
|
fields=None, list_display=True):
|
2016-05-21 23:57:36 +02:00
|
|
|
"""
|
|
|
|
Put the following line in your urls.py BEFORE your admin include
|
2016-07-09 22:31:56 +02:00
|
|
|
(r'^admin/(?P<app_label>[\d\w]+)/(?P<model_name>[\d\w]+)/csv/',
|
|
|
|
'util.csv_view.admin_list_export'),
|
2016-05-21 23:57:36 +02:00
|
|
|
"""
|
|
|
|
if not request.user.is_staff:
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
if not queryset:
|
2016-05-24 00:40:55 +02:00
|
|
|
model = apps.get_model(app_label, model_name)
|
2016-05-21 23:57:36 +02:00
|
|
|
queryset = model.objects.all()
|
2016-07-09 21:19:37 +02:00
|
|
|
queryset = queryset.filter(profile__is_cof=True)
|
2016-05-21 23:57:36 +02:00
|
|
|
if not fields:
|
|
|
|
if list_display and len(queryset.model._meta.admin.list_display) > 1:
|
|
|
|
fields = queryset.model._meta.admin.list_display
|
|
|
|
else:
|
|
|
|
fields = None
|
|
|
|
return export(queryset, fields)
|
|
|
|
"""
|
2016-07-09 22:31:56 +02:00
|
|
|
Create your own change_list.html for your admin view and put something
|
|
|
|
like this in it:
|
2016-05-21 23:57:36 +02:00
|
|
|
{% block object-tools %}
|
|
|
|
<ul class="object-tools">
|
2016-07-09 22:31:56 +02:00
|
|
|
<li><a href="csv/{%if request.GET%}?{{request.GET.urlencode}}
|
|
|
|
{%endif%}" class="addlink">Export to CSV</a></li>
|
2016-05-21 23:57:36 +02:00
|
|
|
{% if has_add_permission %}
|
2016-07-09 22:31:56 +02:00
|
|
|
<li><a href="add/{% if is_popup %}?_popup=1{% endif %}"
|
|
|
|
class="addlink">
|
|
|
|
{% blocktrans with cl.opts.verbose_name|escape as name %}
|
|
|
|
Add {{ name }}{% endblocktrans %}</a></li>
|
2016-05-21 23:57:36 +02:00
|
|
|
{% endif %}
|
|
|
|
</ul>
|
|
|
|
{% endblock %}
|
|
|
|
"""
|