Skip to content

Commit

Permalink
#550 Fork AbstractPageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 committed Sep 3, 2018
1 parent ed7a5d5 commit c626fcd
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions shopelectro/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c626fcd

Please sign in to comment.