Skip to content

Commit

Permalink
fixes arteria#70
Browse files Browse the repository at this point in the history
  • Loading branch information
tehfink committed Jun 7, 2022
1 parent 6f284f3 commit 4ed91f0
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions cmsplugin_contact_plus/actions.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import csv
import csv
import six
from six import text_type

from django.http import HttpResponse


class LUT(object):

def __init__(self):
self.lut = []

def add_field(self, field_name):
""" """
if field_name not in self.lut:
self.lut.append(field_name)

def get_idx(self, field_name):
""" """
return self.lut.index(field_name)



def export_as_csv_action(description="Export selected objects as CSV file",
fields=None, exclude=None, header=True, json_fields=None):
"""
This function returns an export csv action
'fields' and 'exclude' work like in django ModelForm
'header' is whether or not to output the column names as the first row
json_fields will be exploaded to rows.
"""

Expand All @@ -50,7 +50,7 @@ def export_as_csv(modeladmin, request, queryset):

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % text_type(opts).replace('.', '_')

writer = csv.writer(response)
"""
if header:
Expand All @@ -74,13 +74,13 @@ def export_as_csv(modeladmin, request, queryset):
j = getattr(obj, field)
for l in j:
try:
for k, v in l.iteritems():
for k, v in l.items():
lut.add_field(k)
except AttributeError:
pass
else:
lut.add_field(field)

if header:
writer.writerow(lut.lut)
for obj in queryset:
Expand All @@ -89,7 +89,7 @@ def export_as_csv(modeladmin, request, queryset):
if field in json_fields:
j = getattr(obj, field)
for l in j:
for k, v in l.iteritems():
for k, v in l.items():
try:
s = text_type(v)
if six.PY2:
Expand All @@ -102,8 +102,8 @@ def export_as_csv(modeladmin, request, queryset):
for field in many_to_many_field_names:
row[lut.get_idx(field)] = text_type(getattr(obj, field).all())
writer.writerow(row)


return response
export_as_csv.short_description = description
return export_as_csv
return export_as_csv

0 comments on commit 4ed91f0

Please sign in to comment.