From 9cb261f90e27abd1e891d5033db3b94735d5e6a5 Mon Sep 17 00:00:00 2001 From: duker33 Date: Mon, 11 Feb 2019 20:52:39 +0300 Subject: [PATCH] #240 Create page context. Move base contexts to page module side --- catalog/newcontext/context.py | 20 ++------------------ pages/newcontext/__init__.py | 3 +++ pages/newcontext/base.py | 21 +++++++++++++++++++++ pages/newcontext/pages.py | 12 ++++++++++++ 4 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 pages/newcontext/__init__.py create mode 100644 pages/newcontext/base.py create mode 100644 pages/newcontext/pages.py diff --git a/catalog/newcontext/context.py b/catalog/newcontext/context.py index d51bf4e..c79ca54 100644 --- a/catalog/newcontext/context.py +++ b/catalog/newcontext/context.py @@ -1,15 +1,10 @@ import abc -import collections + import typing from django.db.models import QuerySet - -class Context(abc.ABC): - - @abc.abstractmethod - def context(self) -> typing.Dict[str, typing.Any]: - ... +from pages.newcontext import Context class ModelContext(abc.ABC): @@ -28,17 +23,6 @@ def context(self) -> typing.Dict[str, typing.Any]: Context.register(ModelContext) -class Contexts(Context): - - def __init__(self, *contexts: typing.List[Context]): - self.contexts = contexts - - def context(self): - return dict(collections.ChainMap( - *[ctx.context() for ctx in self.contexts] - )) - - class Tags(ModelContext): def context(self): diff --git a/pages/newcontext/__init__.py b/pages/newcontext/__init__.py new file mode 100644 index 0000000..b758344 --- /dev/null +++ b/pages/newcontext/__init__.py @@ -0,0 +1,3 @@ +from . import base, pages +from .base import Context, Contexts +from .pages import Page diff --git a/pages/newcontext/base.py b/pages/newcontext/base.py new file mode 100644 index 0000000..9a6ca06 --- /dev/null +++ b/pages/newcontext/base.py @@ -0,0 +1,21 @@ +import abc +import collections +import typing + + +class Context(abc.ABC): + + @abc.abstractmethod + def context(self) -> typing.Dict[str, typing.Any]: + ... + + +class Contexts(Context): + + def __init__(self, *contexts: typing.List[Context]): + self.contexts = contexts + + def context(self): + return dict(collections.ChainMap( + *[ctx.context() for ctx in self.contexts] + )) diff --git a/pages/newcontext/pages.py b/pages/newcontext/pages.py new file mode 100644 index 0000000..811ab5c --- /dev/null +++ b/pages/newcontext/pages.py @@ -0,0 +1,12 @@ +from .base import Context + + +class Page(Context): + + def __init__(self, page): + self.page = page + + def context(self): + return { + 'page': self.page, + }