Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude-some-columns-from-report #20

Merged
merged 4 commits into from
Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Installation
Changes in your project
=======================

1. Now your table should extend of 'TableReport'
1.a Now your table should extend of 'TableReport'

::

Expand All @@ -81,6 +81,15 @@ Changes in your project

...

1.b If you want to exclude some columns from report (e.g. if it is a column of buttons), you should set 'exclude_from_report' - the names of columns (as well as property 'exclude' in `table <http://pypi.python.org/pypi/django-tables2/>`_)

::

class MyTable(TableReport):

class Meta:
exclude_from_report = ('column1', ...)
...

2.a. If you use a traditional views, now you should use other RequestConfig and change a little your view:

Expand Down
19 changes: 19 additions & 0 deletions django_tables2_reports/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import csv
import codecs
import sys
from functools import wraps

PY3 = sys.version_info[0] == 3

Expand Down Expand Up @@ -81,6 +82,8 @@ def writerow(self, row):

class TableReport(tables.Table):

exclude_from_report = () # the names of columns that should be excluded from report

def __init__(self, *args, **kwargs):
if not 'template' in kwargs:
kwargs['template'] = 'django_tables2_reports/table.html'
Expand All @@ -90,6 +93,20 @@ def __init__(self, *args, **kwargs):
self.formats = [(_('CSV Report'), 'csv')]
if get_excel_support():
self.formats.append((_('XLS Report'), 'xls'))
if hasattr(self, 'Meta'):
self.exclude_from_report = getattr(self.Meta, 'exclude_from_report', ())

def _with_exclude_from_report(method):
""" Put to 'exclude' columns from 'exclude_from_report', and revert this after method's call """
@wraps(method)
def with_exclude(self, *args, **kwargs):
origin_exclude = self.exclude
self.exclude = self.exclude_from_report
try:
return method(self, *args, **kwargs)
finally:
self.exclude = origin_exclude
return with_exclude

def as_report(self, request, report_format='csv'):
if report_format == 'csv':
Expand All @@ -98,6 +115,7 @@ def as_report(self, request, report_format='csv'):
return self.as_xls(request)
raise ValueError("This format %s is not accepted" % report_format)

@_with_exclude_from_report
def as_csv(self, request):
response = HttpResponse()
csv_writer = UnicodeWriter(response, encoding=settings.DEFAULT_CHARSET)
Expand All @@ -116,6 +134,7 @@ def as_csv(self, request):
csv_writer.writerow(csv_row)
return response

@_with_exclude_from_report
def as_xls(self, request):
return self.as_csv(request)

Expand Down
35 changes: 35 additions & 0 deletions django_tables2_reports/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,41 @@ def test_csv_no_pagination(self):
'page 2,2\r\n')
)

def test_exclude_from_report(self):
"""Ensure that exclude-some-columns-from-report works."""
data = [
{
'name': 'page 1',
'item_num': 1,
},
{
'name': 'page 2',
'item_num': 2,
},
]

class TableWithExclude(TableReportForTesting):
class Meta:
exclude_from_report = ('item_num',)

table = TableWithExclude(data)
table.exclude = ('name', )
self.assertEqual(table.exclude_from_report, ('item_num',))

response = table.as_csv(HttpRequest())
self.assertEqual(response.status_code, 200)
content = response.content
if PY3:
content = content.decode(settings.DEFAULT_CHARSET).replace('\x00', '')

self.assertEqual(table.exclude, ('name',)) # Attribute 'exclude_from_report' shouldn't overwrite 'exclude'
self.assertEqual(
content,
('Name\r\n'
'page 1\r\n'
'page 2\r\n')
)


@skipIf(
not django_tables2_reports.utils.get_excel_support(),
Expand Down