diff --git a/shopelectro/context.py b/shopelectro/context.py index ae4b85ca..c8ee51cf 100644 --- a/shopelectro/context.py +++ b/shopelectro/context.py @@ -85,15 +85,9 @@ def __init__( :param url_kwargs: Came from `urls` module. :param request: Came from `urls` module """ - if url_kwargs: - assert 'slug' in url_kwargs self.url_kwargs_ = url_kwargs or {} self.request_ = request - @property - def slug(self) -> str: - return self.url_kwargs['slug'] - @property def url_kwargs(self) -> typing.Dict[str, str]: return self.url_kwargs_ or self.super.url_kwargs @@ -102,17 +96,39 @@ def url_kwargs(self) -> typing.Dict[str, str]: def request(self) -> http.HttpRequest: return self.request_ or self.super.request + @abstractmethod + def get_context_data(self) -> typing.Dict[str, typing.Any]: + ... + + +class AbstractPageContext(AbstractContext, ABC): + + super: 'AbstractPageContext' = None + + def __init__( + self, + url_kwargs: typing.Dict[str, str]=None, + request: http.HttpRequest=None + ): + """ + :param url_kwargs: Came from `urls` module. + :param request: Came from `urls` module + """ + if url_kwargs: + assert 'slug' in url_kwargs + super().__init__(url_kwargs, request) + + @property + def slug(self) -> str: + return self.url_kwargs['slug'] + @property @lru_cache(maxsize=1) def page(self): return ModelPage.objects.get(slug=self.slug) - @abstractmethod - def get_context_data(self) -> typing.Dict[str, typing.Any]: - ... - -class ProductsListContext(AbstractContext, ABC): +class ProductsListContext(AbstractPageContext, ABC): super: 'ProductsListContext' = None @@ -200,7 +216,7 @@ def get_context_data(self): } -class DBTemplateContext(AbstractContext): +class DBTemplateContext(AbstractPageContext): """Processes some page data fields as templates with each own context.""" @property