-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
61 lines (53 loc) · 1.91 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
59
60
61
# -*- coding: utf-8 -*-
import csv
import logging
import tablib
from datetime import datetime
from django.db.models import Model
from django.db.models.fields.files import FieldFile
from unicodedata import normalize
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.template import Context, Template
from django.conf import settings
from django.core.urlresolvers import reverse
def export_as_excel(modeladmin, request, queryset):
if not request.user.is_staff:
raise PermissionDenied
opts = modeladmin.model._meta
response = HttpResponse(mimetype='text/csv; charset=utf-8')
response['Content-Disposition'] = 'attachment; filename=%s.xls' % unicode(opts).replace('.', '_')
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]
v_field_names = map(lambda x: x.encode('utf-8') if x != 'ID' else 'Id', v_field_names)
ax = []
headers = v_field_names
data = []
data = tablib.Dataset(*data, headers=headers)
for obj in queryset:
acc = []
for field in field_names:
try:
uf = getattr(obj, field)()
except TypeError:
try:
uf = getattr(obj, field)
except:
uf = ' error obteniendo el dato'
if uf is None:
uf = ''
elif isinstance(uf, datetime):
uf = unicode(uf)
elif isinstance(uf, Model):
uf = unicode(uf)
elif isinstance(uf, FieldFile):
uf = uf.url
acc.append(uf)
data.append(acc)
response.write(data.xls)
return response
export_as_excel.short_description = "Export as Excel"