Skip to content

Commit

Permalink
Little fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed Jan 23, 2018
1 parent 1221c70 commit 135391f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions django_tables2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
from .tables import Table, TableBase, table_factory # noqa: F401
from .tables import Table, TableBase, table_factory
from .columns import (BooleanColumn, Column, CheckBoxColumn, DateColumn,
DateTimeColumn, EmailColumn, FileColumn, JSONColumn,
LinkColumn, ManyToManyColumn, RelatedLinkColumn, TemplateColumn,
Expand All @@ -12,7 +12,7 @@
__version__ = '1.17.1'

__all__ = (
'Table', 'TableBase',
'Table', 'TableBase', 'table_factory',
'BooleanColumn', 'Column', 'CheckBoxColumn', 'DateColumn', 'DateTimeColumn',
'EmailColumn', 'FileColumn', 'JSONColumn', 'LinkColumn', 'ManyToManyColumn',
'RelatedLinkColumn', 'TemplateColumn', 'TimeColumn', 'URLColumn',
Expand Down
19 changes: 10 additions & 9 deletions django_tables2/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ class Table(TableBase):
# Table = DeclarativeColumnsMetaclass(str('Table'), (TableBase, ), {})


def table_factory(model, table=Table, **kwargs):
def table_factory(model, table=Table, fields=None, exclude=None,
localize=None):
"""
Arguments:
Expand All @@ -637,21 +638,21 @@ def table_factory(model, table=Table, **kwargs):
localize (list of str): Fields to localize
"""
attrs = {'model': model}
if kwargs.get('fields'):
attrs['fields'] = kwargs['fields']
if kwargs.get('exclude'):
attrs['exclude'] = kwargs['exclude']
if kwargs.get('localize'):
attrs['localize'] = kwargs['localize']
if fields is not None:
attrs['fields'] = fields
if exclude is not None:
attrs['exclude'] = exclude
if localize is not None:
attrs['localize'] = localize
# If parent form class already has an inner Meta, the Meta we're
# creating needs to inherit from the parent's inner meta.
parent = (object,)
if hasattr(table, 'Meta'):
parent = (table.Meta, object)
Meta = type(str('Meta'), parent, attrs)
# Give this new form class a reasonable name.
# Give this new table class a reasonable name.
class_name = model.__name__ + str('Table')
# Class attributes for the new form class.
# Class attributes for the new table class.
table_class_attrs = {
'Meta': Meta,
}
Expand Down
12 changes: 9 additions & 3 deletions django_tables2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ def get_table_class(self):
'''
Return the class to use for the table.
'''
if self.table_class is None:
self.table_class = tables.table_factory(self.model)
return self.table_class
if self.table_class:
return self.table_class
if self.model:
return tables.table_factory(self.model)
klass = type(self).__name__
raise ImproperlyConfigured(
"You must either specify {0}.table_class or"
"{0}.model".format(klass)
)

def get_context_table_name(self, table):
'''
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/generic-mixins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ template.
The following view parameters are supported:

- ``table_class`` –- the table class to use, e.g. ``SimpleTable``, if not specfied
a default table will be provided.
and ``model`` is provided, a default table will be created on-the-fly.
- ``table_data`` (or ``get_table_data()``) -- the data used to populate the table
- ``context_table_name`` -- the name of template variable containing the table object
- ``table_pagination`` (or ``get_table_pagination``) -- pagination
Expand Down
11 changes: 7 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,12 @@ def test_factory_localize_argument(self):
self.assertEqual(Table._meta.localize, localize)

def test_factory_with_meta(self):
localize = ('username',)
fields = ('first_name',)

class Meta:
fields = ('first_name',)
class TableWithMeta(tables.Table):
first_name = tables.Column()
class Meta:
fields = ('first_name',)

tables.table_factory(Person, localize=localize)
Table = tables.table_factory(Person, table=TableWithMeta)
self.assertEqual(Table.Meta.fields, fields)
16 changes: 12 additions & 4 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class SimpleView(DispatchHookMixin, tables.SingleTableView):
model = Region # required for ListView


class SimpleNoTableClassView(DispatchHookMixin, tables.SingleTableView):
model = Region # required for ListView


class SimplePaginatedView(DispatchHookMixin, tables.SingleTableView):
table_class = SimpleTable
table_pagination = {'per_page': 1}
Expand Down Expand Up @@ -200,10 +196,22 @@ def test_get_tables_class(self):
self.assertEqual(table_class, view.table_class)

def test_get_tables_class_auto(self):
class SimpleNoTableClassView(tables.SingleTableView):
model = Region

view = SimpleNoTableClassView()
table_class = view.get_table_class()
self.assertEqual(table_class.__name__, 'RegionTable')

def test_get_tables_class_raises_no_model(self):
class SimpleNoTableClassNoModelView(tables.SingleTableView):
model = None
table_class = None

view = SimpleNoTableClassNoModelView()
with self.assertRaises(ImproperlyConfigured):
view.get_table_class()


class SingleTableMixinTest(TestCase):
def test_with_non_paginated_view(self):
Expand Down

0 comments on commit 135391f

Please sign in to comment.