Skip to content

Commit

Permalink
Merge pull request #26 from pstch/develop
Browse files Browse the repository at this point in the history
make_model_mixin detects instance view
  • Loading branch information
Hugo Geoffroy committed Apr 24, 2014
2 parents d3eed33 + 45cc70d commit 59bda18
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion django_crucrudile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

__title__ = 'django-crucrudile'
__version__ = '0.4.4'
__version__ = '0.4.5'
__author__ = 'Hugo Geoffroy'
__license__ = 'GNU General Public License V3.0'
__copyright__ = 'Copyright 2013-2014 Hugo Geoffroy'
15 changes: 12 additions & 3 deletions django_crucrudile/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
def make_model_mixin(view_class,
extra_args=None,
extra_funcs=None,
instance_view=False,
no_auto_view_mixin=False):
"""Return a generated Model mixin for a given view HAHA.
Expand All @@ -43,6 +44,11 @@ def make_model_mixin(view_class,
as argument)
:type extra_funcs: dict
:param instance_view: Does the view return a Model (List, Create)
or an instance of this Model (Detail,
Update, Delete) ? If instance, set to True.
:type instance_view: bool
:param no_auto_view_mixin: Disable autopatching of view with
``ModelActionMixin``. (When ``view_class``
is missing a method or attribute
Expand Down Expand Up @@ -82,20 +88,23 @@ def get_args_by_view(cls, view):
})
return args

def _get_url(cls, *args, **kwargs):
def _get_url(obj, *args, **kwargs):
"""Private function, patched as ``get_*_url`` to the model mixin.
"""
if instance_view or view_class.instance_view:
kwargs['args'].append(obj.id)
return reverse(
cls.get_url_name(view_class, prefix=True),
obj.get_url_name(view_class, prefix=True),
*args,
**kwargs
)

_get_url.__doc__ = "Get %s URL" % view_class.get_action_name()
# we make _get_url a class method only at this point to be able
# to change __doc__
_get_url = classmethod(_get_url)
if not (instance_view or view_class.instance_view):
_get_url = classmethod(_get_url)

setattr(ModelMixin,
'get_%s_url' % view_class.get_underscored_action_name(),
Expand Down
10 changes: 9 additions & 1 deletion django_crucrudile/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def auto_patterns_for_app(app_name, exclude_models=None):
"""Returns a list of URL patterns (Django URL objects) for the given
application, using content types.
Warning : auto_patterns_for_app will run ContentType queries. For
this reason, the database must be in a clean state when
calling. If you call this function from ``urls.py``, be sure to
catch exceptions caused by the incoherent state, otherwise you
won't be able to migrate.**This is why it is not recommended to
use auto_patterns_for_app, and recommended to set yourself the
Model list.**
:param app_name: Application name to get URL patterns for
:type app_name: str
Expand All @@ -18,7 +26,7 @@ def auto_patterns_for_app(app_name, exclude_models=None):
:raise ImproperlyConfigured: if failing to import
``django.contrib.contenttypes``
:raise ProgrammingError: Database in incoherent state
:return: URL patterns for the given application
:rtype: list
Expand Down
7 changes: 7 additions & 0 deletions django_crucrudile/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class ModelActionMixin(object):
class documentation)
:type url_args: list
"""
instance_view = False
"""
:attribute url_args: Set to true if the view show an instance
(detail, update, delete) rather than a model
(list, create)
:type url_args: bool
"""

@classmethod
def get_fallback_action_name(cls):
Expand Down
10 changes: 4 additions & 6 deletions docs/gettingstarted.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ To get the list of `url` objects for a specific Model (here ``Book``), you can u

Or you could loop through your models, and append the URL patterns to ``urlpatterns``::

from itertools import chain
from models import Book, Author

model_list = Book, Author
urlpatterns = [model.get_url_patterns() for model in model_list]

Also, if you have lots of different Models that all use model mixins, you can use :func:`django_crucrudile.urls.auto_patterns_for_app`, giving it an application name as argument, and it will gather all the URL patterns for all defined models in the application (here, ``'booklist'`` is the application name)::

from django_crucrudile.urls import auto_patterns_for_app
urlpatterns = auto_patterns_for_app('booklist')

Yes, your ``urls.py`` now contains two lines !
Yes, your ``urls.py`` now contains four lines !

0 comments on commit 59bda18

Please sign in to comment.