diff --git a/pages/logic/page.py b/pages/logic/page.py index 90daf80..02d759a 100644 --- a/pages/logic/page.py +++ b/pages/logic/page.py @@ -9,36 +9,25 @@ def __init__(self, model: models.Page): # But "model" is Django standard. self.model = model - def breadcrumbs(self) -> 'Breadcrumbs': - pass + def __str__(self): + return f'' + + def __repr__(self): + return f'' @property def siblings(self) -> models.PageQuerySet: - return self.model.parent.children.exclude(id=self.model.id) - - -# @todo #343:60m Implement Breadcrumbs class. -# Use it instead of monolithic logic at the `breadcrumbs_with_siblings`. -# Create Breadcrumb class or named tuple to specify crumb data structure. -class Breadcrumbs: - def __init__(self, page_model: models.Page): - self.model = page_model - - def query(self, include_self: bool) -> models.PageQuerySet: - return ( - self.model - .get_ancestors(include_self) - .select_related(self.model.related_model_name) - .active() - ) - - def list(self, include_self=False) -> typing.List[typing.Tuple[str, str]]: - """Breadcrumbs list consists of current page ancestors.""" - return [ - (crumb.display_menu_title, crumb.url) - for crumb in self.query(include_self).iterator() - ] - - def list_with_self(self) -> list: - return self.list(include_self=True) + return self.model.parent.children.active().exclude(id=self.model.id) + + @property + def breadcrumbs(self) -> 'Pages': + return Pages(self.model.get_ancestors(include_self=True).active()) + + +class Pages: + def __init__(self, models: typing.Iterable[models.Page]): + self.models = models + def __iter__(self): + for model in self.models: + yield Page(model) diff --git a/pages/models.py b/pages/models.py index 9f94d97..bd3a1d0 100644 --- a/pages/models.py +++ b/pages/models.py @@ -137,10 +137,6 @@ class Meta: verbose_name=_('page template') ) - @classmethod - def get_index(cls): - return Page.objects.filter(type=cls.CUSTOM_TYPE, slug=cls.INDEX_PAGE_SLUG).first() - @property def url(self): return self.get_absolute_url() diff --git a/pages/templates/pages/breadcrumbs.html b/pages/templates/pages/breadcrumbs.html index ffa1f4b..bc336e5 100644 --- a/pages/templates/pages/breadcrumbs.html +++ b/pages/templates/pages/breadcrumbs.html @@ -1,19 +1,33 @@ {% load pages_extras %} diff --git a/pages/templates/pages/breadcrumbs_with_siblings.html b/pages/templates/pages/breadcrumbs_with_siblings.html deleted file mode 100644 index 880cf98..0000000 --- a/pages/templates/pages/breadcrumbs_with_siblings.html +++ /dev/null @@ -1,33 +0,0 @@ -{% load pages_extras %} - - diff --git a/pages/templatetags/pages_extras.py b/pages/templatetags/pages_extras.py index 356569f..a545e60 100644 --- a/pages/templatetags/pages_extras.py +++ b/pages/templatetags/pages_extras.py @@ -8,65 +8,30 @@ register = template.Library() -@register.inclusion_tag('pages/breadcrumbs.html') -def breadcrumbs(page: Page, separator='', base_url=''): - index = page.get_index() - - crumbs_list = ( - (index.display_menu_title, index.url) if index else ('Main', '/'), - *page.get_ancestors_fields('display_menu_title', 'url', include_self=False), - (page.display_menu_title, '') - ) - +def _base_breadcrumbs(page: Page, separator='', *, show_siblings=False): return { - 'crumbs_list': crumbs_list, + 'breadcrumbs': [ + # @todo #345:60m Refold catalog pages in DB. + # In both fixtures and local DB. + # Implement this pages structure: + # - each(category_roots).parent == CustomPage.get('catalog') + # - CustomPage.get('catalog').parent == CustomPage.get('index') + logic.Page(model=CustomPage.objects.get(slug='')), # index page + *list(logic.Page(model=page).breadcrumbs) + ], 'separator': separator, - 'base_url': base_url, + 'show_siblings': show_siblings, } -@register.inclusion_tag('pages/breadcrumbs_with_siblings.html') -def breadcrumbs_with_siblings( - page: Page, separator='', base_url='', include_self=False -): - def get_ancestors_crumbs() -> list: - ancestors_query = ( - page - .get_ancestors(include_self) - .select_related(page.related_model_name) - .active() - ) - - if not ancestors_query.exists(): - return [] - - catalog, *ancestors = ancestors_query - - return [ - (catalog.display_menu_title, catalog.url, []), - *[ - ( - crumb.display_menu_title, - crumb.url, - list(logic.Page(page).siblings) - ) for crumb in ancestors - ], - ] - - index = page.get_index() +@register.inclusion_tag('pages/breadcrumbs.html') +def breadcrumbs(page: Page, separator=''): + return _base_breadcrumbs(page, separator, show_siblings=False) - crumbs_list = [ - (index.display_menu_title, index.url, []) if index else ('Main', '/', []), - *get_ancestors_crumbs(), - (page.display_menu_title, '', logic.Page(page).siblings) - ] - return { - 'index_slug': index.url if index else '/', - 'crumbs_list': crumbs_list, - 'separator': separator, - 'base_url': base_url, - } +@register.inclusion_tag('pages/breadcrumbs.html') +def breadcrumbs_with_siblings(page: Page, separator=''): + return _base_breadcrumbs(page, separator, show_siblings=True) @register.inclusion_tag('pages/accordion.html') diff --git a/tests/catalog/test_views.py b/tests/catalog/test_views.py index 351f329..84d81b8 100644 --- a/tests/catalog/test_views.py +++ b/tests/catalog/test_views.py @@ -1,11 +1,10 @@ """ Defines tests for views in Catalog app """ -from django.test import TestCase from django.core.urlresolvers import reverse +from django.test import TestCase from pages.models import CustomPage - from tests.catalog.models import MockCategory, MockProduct @@ -87,6 +86,7 @@ def test_product_page(self): response = self.client.get(self.test_product.url) self.assertEqual(response.status_code, 200) + # @todo #345:30m Test crumbs siblings. def test_category_crumbs(self): """Category should have valid crumbs""" page = self.test_last.page