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

Added table_factory #532

Merged
merged 8 commits into from
Jan 23, 2018
Merged

Added table_factory #532

merged 8 commits into from
Jan 23, 2018

Conversation

ZuluPro
Copy link
Contributor

@ZuluPro ZuluPro commented Jan 17, 2018

In the same idea of django.forms.modelform_factory, I created a table_factory.
It helps to create quickly Tables without declare a full class.

I plan to use it in django_tables2.views.SingleTableMixin if table_class isn"t specified.
Creating automaticaly a Table like done in django-filters or Django's ModelFormViews.
(I can do it in this PR ;) )

@jieter
Copy link
Owner

jieter commented Jan 17, 2018

Interesting idea! It might also be possible to replace (some of) the render_table template tag code generating the on the fly table.

What will be an interesting detail is deciding on what kwargs we support. Ideally, not having to define every kwarg in the argument list of modelform_factory would be nice, but I see django doesn't have that (which I assume is for a reason).

For reference: django modelform_factory implementation

Please also take some time to write documentation for this (at least a docstring).

@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 17, 2018

OK! Let's do this with kwargs.
I add:

  • kwargs
  • Usage in View
  • Documentation

And I poke you

@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 18, 2018

@jieter Poke !

Copy link
Owner

@jieter jieter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've added some comments to your diff, please share your opinion if you do not agree.

The function also has to be added to https://github.com/jieter/django-tables2/blob/master/docs/pages/api-reference.rst in order to appear in the documentation.

@@ -1,5 +1,5 @@
# coding: utf-8
from .tables import Table, TableBase
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the string 'table_factory' should be added to __all__, the # noqa comment should be obsolete then.

@@ -620,3 +620,39 @@ class Table(TableBase):
__doc__ = TableBase.__doc__

# Table = DeclarativeColumnsMetaclass(str('Table'), (TableBase, ), {})


def table_factory(model, table=Table, **kwargs):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say, if **kwargs doesn't allow passing arbitrary argument to the metaclass, it provides not much benefit to not explicitly define the arguments this function expects.

So either allow pass-through of arguments in some way, or be explicit about what arguments the function takes.

Meta = type(str('Meta'), parent, attrs)
# Give this new form class a reasonable name.
class_name = model.__name__ + str('Table')
# Class attributes for the new form class.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a leftover from copying modelform_factory?

)
if self.table_class is None:
self.table_class = tables.table_factory(self.model)
return self.table_class
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this is not used with ListView? I'd say if it is mixed in a class not defining self.model, it should still raise the exception.

@@ -35,6 +35,10 @@ class SimpleView(DispatchHookMixin, tables.SingleTableView):
model = Region # required for ListView


class SimpleNoTableClassView(DispatchHookMixin, tables.SingleTableView):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't seem to use the functionality DispatchHookMixin provides, if so, please remove.

@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 18, 2018

OK NP ;)

@ZuluPro ZuluPro force-pushed the factory branch 3 times, most recently from 4c48767 to 53c3139 Compare January 19, 2018 01:09
@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 19, 2018

Hello @jieter

I'd say, if **kwargs doesn't allow passing arbitrary argument to the metaclass, it provides not much benefit to not explicitly define the arguments this function expects.

Yes, I think to declare Meta's attributes explicitly is the best idea. Maybe in future I'll declare Table's attributes too.

@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 21, 2018

@jieter Any news ?

Copy link
Owner

@jieter jieter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! I've added another couple of comments.

"{0}.table_class".format(klass)
)
self.table_class = tables.table_factory(self.model)
return self.table_class
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you undo the nesting here:

if self.table_class:
   return self.table_class
if self.model:
   return table_factory(self.model)

raise ImproperlyConfigured(...)

I think saving to self.table_class is not needed.

Also: the error message has table_class twice, one must be model.

@@ -13,7 +13,8 @@ template.

The following view parameters are supported:

- ``table_class`` –- the table class to use, e.g. ``SimpleTable``
- ``table_class`` –- the table class to use, e.g. ``SimpleTable``, if not specfied
a default table will be provided.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence needs to mention the default table will be based on self.model and having a self.model defined is required.

class Meta:
fields = ('first_name',)

tables.table_factory(Person, localize=localize)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertions missing.

self.assertEqual(table_class, view.table_class)

def test_get_tables_class_auto(self):
view = SimpleNoTableClassView()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having the definition of tables/views in the test as much as possible, so if only used in one test, please put SimpleNoTableClassView in this method.

@ZuluPro
Copy link
Contributor Author

ZuluPro commented Jan 23, 2018

Poke @jieter

@jieter jieter merged commit ffb0c86 into jieter:master Jan 23, 2018
@jieter
Copy link
Owner

jieter commented Jan 23, 2018

Merged, thanks.

@jieter
Copy link
Owner

jieter commented Jan 27, 2018

released 1.18.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants