-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#343 Drop redundant logic from siblings fetching. High level arch for…
… breadcrumbs
- Loading branch information
Showing
5 changed files
with
74 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .page import * | ||
|
||
__all__ = ['page'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import typing | ||
|
||
from pages import models | ||
|
||
|
||
class Page: | ||
def __init__(self, model: models.Page): | ||
# "record" is much more good field name, i suppose. | ||
# But "model" is Django standard. | ||
self.model = model | ||
|
||
def breadcrumbs(self) -> 'Breadcrumbs': | ||
pass | ||
|
||
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.test import TestCase | ||
|
||
from pages import models | ||
|
||
|
||
class PageTests(TestCase): | ||
fixtures = ['catalog.json'] | ||
|
||
@property | ||
def page(self): | ||
return ( | ||
models.Page.objects | ||
.active() | ||
.filter(type=models.Page.MODEL_TYPE) | ||
.exclude(parent=None) | ||
.exclude(parent=models.CustomPage.objects.get(slug='catalog')) | ||
.first() | ||
) | ||
|
||
def test_breadcrumbs(self): | ||
... |