Code Monkey home page Code Monkey logo

django-tables2-reports's Introduction

This project has been retired and we leave it here, as-is, for anybody still using it.

The proper way to export data to various formats now is django-tables2 package itself. See for yourself: http://django-tables2.readthedocs.io/en/latest/pages/export.html

Please do not use django-tables2-reports for new projects.

https://travis-ci.org/goinnn/django-tables2-reports.svg?branch=master

With django-tables2-reports you can get a report (CSV, XLS) of any table with minimal changes to your project

  • In your settings:
INSTALLED_APPS = (

    'django_tables2_reports',
)


TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.static',

)


# This is optional

EXCEL_SUPPORT = 'xlwt' # or 'openpyxl' or 'pyexcelerator'

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

############### Before ###################

import django_tables2 as tables


class MyTable(tables.Table):

    ...

############### Now ######################

from django_tables2_reports.tables import TableReport


class MyTable(TableReport):

    ...

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)

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:

############### Before ###################

from django_tables2 import RequestConfig


def my_view(request):
    objs = ....
    table = MyTable(objs)
    RequestConfig(request).configure(table)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

############### Now ######################

from django_tables2_reports.config import RequestConfigReport as RequestConfig
from django_tables2_reports.utils import create_report_http_response

def my_view(request):
    objs = ....
    table = MyTable(objs)
    table_to_report = RequestConfig(request).configure(table)
    if table_to_report:
        return create_report_http_response(table_to_report, request)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

If you have a lot of tables in your project, you can activate the middleware, and you do not have to change your views, only the RequestConfig import

# In your settings

MIDDLEWARE_CLASSES = (

    'django_tables2_reports.middleware.TableReportMiddleware',
)

############### Now (with middleware) ######################

from django_tables2_reports.config import RequestConfigReport as RequestConfig

def my_view(request):
    objs = ....
    table = MyTable(objs)
    RequestConfig(request).configure(table)
    return render_to_response('app1/my_view.html',
                              {'table': table},
                              context_instance=RequestContext(request))

2.b. If you use a Class-based views:

############### Before ###################

from django_tables2.views import SingleTableView


class PhaseChangeView(SingleTableView):
    table_class = MyTable
    model = MyModel


############### Now ######################

from django_tables2_reports.views import ReportTableView


class PhaseChangeView(ReportTableView):
    table_class = MyTable
    model = MyModel

Under the table appear a CSV icon (and XLS icon if you have xlwt, openpyxl or pyExcelerator in your python path), if you click in this icon, you get a CSV report (or xls report) with every item of the table (without pagination). The ordering works!

You can get the last bleeding edge version of django-tables2-reports by doing a clone of its git repository:

git clone https://github.com/goinnn/django-tables2-reports

In the source tree, you will find a directory called 'test_project'. It contains a readily setup project that uses django-tables2-reports. You can run it as usual:

cd test_project
export PYTHONPATH=..
python manage.py syncdb --noinput
python manage.py runserver

django-tables2-reports's People

Contributors

armicron avatar bitdeli-chef avatar daaray avatar goinnn avatar mpasternak avatar paulgueltekin avatar sramana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-tables2-reports's Issues

Bug in template with new Django version

Hello creator :)
I founded a bug at the table.html template.
It don't work anymore with
"{{ STATIC_URL }}img/{{ format }}_icon.png" title="{{ label }}"

i fixed it bye using this

{% load static %}
"{% get_static_prefix %}img/{{ format }}_icon.png" title="{{ label }}"

Now it works again

Issue in template columns

Template columns doesn't respect the request.session, django-tables2 respects them so the template working under django-tables2 doesn't work under django-tables2-reports

Invalid column index 0

ValueError at /index_function_view_middleware/
Invalid column index 0
Request Method: GET
Request URL: http://127.0.0.1:8000/index_function_view_middleware/?my-prefix-report-testtable=xls
Django Version: 1.7
Exception Type: ValueError
Exception Value:
Invalid column index 0
Exception Location: D:\Program Files\Python33\lib\site-packages\openpyxl\cell\cell.py in get_column_letter, line 109
Python Executable: D:\Program Files\Python33\python.exe
Python Version: 3.3.4
Python Path:
['D:\Users\Hui\PycharmProjects\test_project',
'D:\Program Files\Python33\lib\site-packages\setuptools-1.1.5-py3.3.egg',
'D:\Program Files\Python33\lib\site-packages\pip-1.4.1-py3.3.egg',
'D:\Program Files\Python33\lib\site-packages\django_tables2_reports-0.0.10-py3.3.egg',
'D:\Users\Hui\PycharmProjects\test_project',
'C:\Windows\SYSTEM32\python33.zip',
'D:\Program Files\Python33\DLLs',
'D:\Program Files\Python33\lib',
'D:\Program Files\Python33',
'D:\Program Files\Python33\lib\site-packages']
Server time: Fri, 19 Sep 2014 10:22:33 +0800

when I test the test_project,i get the error.
this is my opion:
openpyxl==2.0.5
django-tables2-reports==0.0.10
excel 2013

Add support for formatting xlwt cell background color style

This is a suggestion for an improvement. I'd like the xls format output to set the cell background color when using xlwt.

I forked the project and made a solution that works for my needs. I'm not going to post a PR since I don't think it's a good enough solution for widespread use and I only changed the code related to xlwt so it will break with the other xls libraries.

Here's my branch with my solution :
https://github.com/samaust/django-tables2-reports/tree/xlwt_background-color

When creating my table, I pass additional columns that contain hex color codes. Those column are set to not show up on the table. If I have a column called "columnname", I also add another column called "columnname_color". My branch searches for those color columns and adds the colors to the custom Excel palette.

500 error when staticfiles returns a 304 response

Django Staticfiles removes the content type from the response object when returning a 304. This causes the middleware to throw a KeyError over response['Content-Type'].

I have a fix for this already. Will send the pull request in a few minutes.

Please add Mixin

A good idea would be to have a ReportTableMixin which would inherit from SingleTableMixin this would allow more flexibility to add django-tables2-reports into our CBV.

XLS table and name of report

Hello! Search engine bot find problem in generate XLS reports with some names:

Exception at /table/
invalid worksheet name u'report-realtytablewocontracttype'
Request Method: GET
Request URL: http://srv/table/?report-realtytablewocontracttype=xls&next=
Django Version: 1.5.4
Exception Type: Exception
Exception Value:
invalid worksheet name u'report-realtytablewocontracttype'
Exception Location: /devel/venv/local/lib/python2.7/site-packages/xlwt/Workbook.py in add_sheet, line 345

VERSION issue

Python 2.7.10
Django 1.8.16

Code below gives issue:

return ".".join(VERSION)
TypeError: sequence item 0: expected string, int found

Code: init.py:
VERSION = (0, 1, 0)
def get_version():
return ".".join(VERSION)

Package installation with pip is broken

When trying to install the latest version of this package through pip, I get the following error:

(netell) ubuntu@ubuntu-xenial:~/netell$ pip install django-tables2-reports
Collecting django-tables2-reports
  Using cached django-tables2-reports-0.1.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-5ia7w4_r/django-tables2-reports/setup.py", line 42, in <module>
        install_requires=[x.strip() for x in open("requirements.txt").readlines()],
    FileNotFoundError: [Errno 2] No such file or directory: 'requirements.txt'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-5ia7w4_r/django-tables2-reports/

I took a look at the package uploaded to PyPI and requirements.txt is indeed missing, and it's also missing from MANIFEST.in.

Context is lost in reports

Current user in django-tables2 is accessible through:

self.context["request"].user

This might be useful for data display depending on permissions.
It still works when extending TableReport, but it's lost when generating CSV/XLS and breaks.

Large number of NULL characters prepend every line after the heading

Not sure what is causing this, as the XLS reports work fine. Excel will not open the CSV files correctly because of the following:

image

I've trimmed most of the NUL chars from this screenshot sot it's readable, but you get the idea. The number of them per line is also variable.

ValueError when text in column is too long

When creating a xls report and the the text inside one of the cell in the table is too long, you get a ValueError because column width (602112) not an int in range(65536).

The function get_xls_col_width in the xlwt_converter must not return a value higher than 65535. The set_width method of the Column class of xlwt will raise the ValueError.

A possible fix to this would be to return the min value between the current approximation, and the max value allowed by xlwt.

Refactor the csv_to_excel module

This module have a mix of the logic to pyexcelerator, xlwt, and openpyxl. I want to do a package.

In this release this will be a package within django-tables2-reports, but the idea is to do a new python egg, this can use a lot of people. Even this will not depend of django.

Fields with commas are split into multiple columns

When exporting to csv or xls text fields with commas are split into multiple columns. While the csv spec isn't the best and has trouble dealing with this sort of ambiguity the xls version should not be affected. However, as in the code the xls export is a post-processing of the csv this carries over to the xls.

Specify columns to report

Guys, thanks for your project !
But there is a question, how to specify (narrow down) the range of columns for csv/xsl report? It is a pretty difficult as I understand now. Do you think to add support for this?
This is helpful if some of columns are not data (i.e. buttons)

Error trying to render the table

I have a model based table, DeploymentsTable, with some manual added Columns.
using version 0.07, trying to render it I get the following exception,
could you please help.

Thanks,
Shay

File "/home/shayh/.virtualenvs/cec/lib/python2.7/site-packages/django_tables2/views.py" in get_context_data

  1.     table = self.get_table()
    
    File "/home/shayh/.virtualenvs/cec/lib/python2.7/site-packages/django_tables2_reports/views.py" in get_table
  2.     self.table_to_report = RequestConfigReport(self.request, **options).configure(table)
    
    File "/home/shayh/.virtualenvs/cec/lib/python2.7/site-packages/django_tables2_reports/config.py" in configure
  3.     param_report = table.param_report
    

Exception Type: AttributeError at /deployments/
Exception Value: 'DeploymentsTable' object has no attribute 'param_report'

โ–ผ Local vars
Variable Value
table
reports.views.DeploymentsTable object at 0x7f5c0825ea50
self
django_tables2_reports.config.RequestConfigReport object at 0x7f5c0825e9d0
extra_context
None

compatibility with django-tables2 > 2.0

Hello,
could you please modify tables.py as follow:

88,89c88,89
<         if not 'template' in kwargs:
<             kwargs['template'] = 'django_tables2_reports/table.html'
---
>         if not 'template_name' in kwargs:
>             kwargs['template_name'] = 'django_tables2_reports/table.html'

It is needed to have compatibility with django-tables2 >2.0

Thank you

Simone

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.