-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
58 lines (49 loc) · 2.1 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
# taken from http://weitlandt.com/theme/2010/05/wir-djangonauten-csv-export-nach-excel-mit-umlauten/
# + mucho amor de @julian_amaya y @votaguz
import csv
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.template import Context, Template
def get_csv_from_dict_list(field_list, data):
csv_line = ";".join(['{{ row.%s|addslashes }}' % field for field in field_list])
template = "{% for row in data %}" + csv_line + "\n{% endfor %}"
return Template(template).render(Context({"data": data}))
def export_as_csv(modeladmin, request, queryset):
if not request.user.is_staff:
raise PermissionDenied
replace_dc = {'\n': '* ', '\r': '', ';': ',', '\"': '|', '\'': '|', 'True': 'Si', 'False': 'No'}
opts = modeladmin.model._meta
response = HttpResponse(mimetype='text/csv; charset=utf-8')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
w = csv.writer(response, delimiter=';')
# import pdb; pdb.set_trace()
try:
field_names = modeladmin.model.get_csv_fields()
v_field_names = field_names
except:
field_names = [field.name for field in opts.fields]
v_field_names = [getattr(field, 'verbose_name') or field.name for field in opts.fields]
# print field_names
v_field_names = map(lambda x: x.encode('utf-8') if x != 'ID' else 'Id', v_field_names)
w.writerow(v_field_names)
ax = []
for obj in queryset:
acc = {}
for field in field_names:
try:
uf = unicode(getattr(obj, field)()).encode('utf-8')
except:
try:
uf = unicode(getattr(obj, field)).encode('utf-8')
except:
uf = ''
for i, j in replace_dc.iteritems():
uf = uf.replace(i, j)
if uf == 'None':
uf = ''
acc[field] = uf
ax.append(acc)
response.write(get_csv_from_dict_list(field_names, ax))
return response
export_as_csv.short_description = "Exportar como CSV"