Skip to content

standardized/modernized view mixins #798

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
.idea/
__pycache__
.pytest_cache
*.pytest-deps
*.pyc
.tox/
build/
dist/

*.db
*.log

.DS_Store
.coverage
Expand Down
8 changes: 7 additions & 1 deletion view_breadcrumbs/generic/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ def __create_view_name(self):
def create_view_url(self):
return reverse(self.__create_view_name)

@property
def create_view_label(self):
if self.add_format_string:
return self.add_format_string % {"model": self.model_name_title}
return _("Add %(model)s") % {"model": self.model_name_title}

@property
def crumbs(self):
return super(CreateBreadcrumbMixin, self).crumbs + [
(
_(self.add_format_string % {"model": self.model_name_title}),
self.create_view_label,
self.create_view_url,
),
]
23 changes: 21 additions & 2 deletions view_breadcrumbs/generic/delete.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from django.urls import reverse
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _

from ..utils import action_view_name, classproperty
from .list import ListBreadcrumbMixin
from .detail import DetailBreadcrumbMixin


class DeleteBreadcrumbMixin(ListBreadcrumbMixin):
class DeleteBreadcrumbMixin(DetailBreadcrumbMixin):
# Home / object List / object / Delete object
delete_format_string = _("Delete %(instance)s")

@classproperty
def delete_view_name(self):
return action_view_name(
Expand All @@ -30,3 +35,17 @@ def delete_view_url(self, instance):
self.__delete_view_name,
kwargs={self.slug_url_kwarg: getattr(instance, self.slug_field)},
)

def delete_view_label(self, instance):
if self.delete_format_string:
return self.delete_format_string % {"instance": force_str(instance)}
return _("Delete %(instance)s") % {"instance": force_str(instance)}

@property
def crumbs(self):
return super(DeleteBreadcrumbMixin, self).crumbs + [
(
self.delete_view_label,
self.delete_view_url,
),
]
27 changes: 8 additions & 19 deletions view_breadcrumbs/generic/detail.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
from functools import partial

from django.urls import reverse
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _

from ..utils import action_view_name, classproperty
from .list import ListBreadcrumbMixin


def _detail_view_label(instance, format_string):
return _(force_str(format_string) % {"instance": force_str(instance)})


class DetailBreadcrumbMixin(ListBreadcrumbMixin):
# Home / object List / str(object)
detail_format_string = _("%(instance)s")
detail_format_string = "%s"

@classproperty
def detail_view_name(self):
Expand All @@ -32,21 +24,18 @@ def __detail_view_name(self):
)

def detail_view_url(self, instance):
if self.breadcrumb_use_pk:
return reverse(
self.__detail_view_name, kwargs={self.pk_url_kwarg: instance.pk}
)

return reverse(
self.__detail_view_name,
kwargs={self.slug_url_kwarg: getattr(instance, self.slug_field)},
)
return instance.get_absolute_url()

def detail_view_label(self, instance):
if self.detail_format_string:
return self.detail_format_string % force_str(instance)
return force_str(instance)

@property
def crumbs(self):
return super(DetailBreadcrumbMixin, self).crumbs + [
(
partial(_detail_view_label, format_string=self.detail_format_string),
self.detail_view_label,
self.detail_view_url,
),
]
6 changes: 5 additions & 1 deletion view_breadcrumbs/generic/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def __list_view_name(self):
def list_view_url(self):
return reverse(self.__list_view_name)

@property
def list_view_label(self):
return self.model_name_title_plural

@property
def crumbs(self):
return [(self.model_name_title_plural, self.list_view_url)]
return [(self.list_view_label, self.list_view_url)]
11 changes: 7 additions & 4 deletions view_breadcrumbs/generic/update.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from functools import partial

from django.urls import reverse
from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _
Expand All @@ -14,7 +12,7 @@ def _update_view_label(instance, format_string):

class UpdateBreadcrumbMixin(DetailBreadcrumbMixin):
# Home / object List / object / Update object
update_format_str = _("Update: %(instance)s")
update_format_string = _("Update %(instance)s")

@classproperty
def update_view_name(self):
Expand Down Expand Up @@ -42,11 +40,16 @@ def update_view_url(self, instance):
kwargs={self.slug_url_kwarg: getattr(instance, self.slug_field)},
)

def update_view_label(self, instance):
if self.update_format_string:
return self.update_format_string % {"instance": force_str(instance)}
return _("Update %(instance)s") % {"instance": force_str(instance)}

@property
def crumbs(self):
return super(UpdateBreadcrumbMixin, self).crumbs + [
(
partial(_update_view_label, format_string=self.update_format_str),
self.update_view_label,
self.update_view_url,
),
]
1 change: 0 additions & 1 deletion view_breadcrumbs/templatetags/view_breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
:contact: [email protected]
"""


from __future__ import unicode_literals

import logging
Expand Down
58 changes: 58 additions & 0 deletions view_breadcrumbs/tests/unit/test_breadcrumbs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import inspect

from django.conf import settings
from django.test import RequestFactory, TestCase, override_settings
from django.utils.encoding import force_str
Expand Down Expand Up @@ -68,6 +70,9 @@ class ActionTestMixin(object):

def _get_view(self):
# TODO: Move this to use the default django client.
TestModel = self.view_attrs["model"]
TestModel.get_absolute_url = lambda self: "test_model/%d" % self.pk

instance = TestModel.objects.create(name="Test")

TestViewClass = self.make_crumb_cls(
Expand Down Expand Up @@ -108,6 +113,59 @@ def test_valid_view_url(self):
else:
self.assertIsNotNone(view_url(view.object))

def test_valid_view_label(self):
view = self._get_view()
view_label_name = "{}_view_label".format(self.view_name)
view_label_func = getattr(view, view_label_name)

kwargs = {}

# check if it's a property and get the actual function
if isinstance(view_label_func, property):
view_label_func = view_label_func.fget

# use inspect to get the function signature if callable
if callable(view_label_func):
signature = inspect.signature(view_label_func)
if "instance" in signature.parameters:
kwargs = {"instance": view.object}

label = (
view_label_func(**kwargs) if callable(view_label_func) else view_label_func
)

match self.view_name:
case "list":
self.assertEqual(label, view.model_name_title_plural)
case "detail":
self.assertEqual(
label, view.detail_format_string % force_str(view.object)
)
case "create":
self.assertEqual(
label, view.add_format_string % {"model": view.model_name_title}
)
case "update":
self.assertEqual(
label,
view.update_format_string % {"instance": force_str(view.object)},
)
case "delete":
self.assertEqual(
label,
view.delete_format_string % {"instance": force_str(view.object)},
)

new_label = "TEST"

@property
def new_label_method(self, instance=None):
return new_label

# monkey patch view class method to override
setattr(type(view), view_label_name, new_label_method)
self.assertEqual(getattr(view, view_label_name), new_label)


class ListViewBreadcrumbTestCase(ActionTestMixin, BaseBreadcrumbTestCase):
breadcrumb_mixin_cls = ListBreadcrumbMixin
Expand Down
Loading