Skip to content
Snippets Groups Projects
Commit 2ee08799 authored by Fynn Becker's avatar Fynn Becker :crab:
Browse files

Improve type hinting for the BreadCrumbMixin

parent 2b9165a4
Branches
Tags
No related merge requests found
from django.urls import reverse, resolve
from typing import Optional
from django.urls import resolve, reverse
from django.views.generic.base import ContextMixin
class BreadCrumbMixin(ContextMixin):
breadcrumb_parent = None
breadcrumb_name = None
breadcrumb_parent = None # type: Optional[str]
breadcrumb_name = None # type: str
def get_breadcrumb_path(self, breadcrumb_path, url_name=''):
if self.breadcrumb_parent:
parent_view_class = resolve(reverse(self.breadcrumb_parent)).func.view_class
breadcrumb_path.extend(parent_view_class().get_breadcrumb_path(breadcrumb_path, self.breadcrumb_parent))
return breadcrumb_path + [{'name': self.breadcrumb_name, 'url': reverse(url_name) if url_name else '#'}]
def get_context_data(self, *args, **kwargs):
def get_context_data(self, **kwargs):
assert self.breadcrumb_name, 'Missing attribute "breadcrumb_name" on {}'.format(self.__class__)
context = super().get_context_data(*args, **kwargs)
context = super().get_context_data(**kwargs)
context.update({
'breadcrumb_path': self.get_breadcrumb_path([])
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment