-
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.
Browse files
Browse the repository at this point in the history
* #240 Process DB templates with new page view class * #240 Create page context. Move base contexts to page module side * #240 Rename module `db_views` to `display` * #240 Add test for attribute access * #240 Merge fix * #240 Merge fix #2
- Loading branch information
Showing
11 changed files
with
124 additions
and
36 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
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
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
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,42 @@ | ||
""" | ||
Views processing db based templates. | ||
This view are outside of MTV concept. | ||
Responsible only for rendering given context data with db preserved text template. | ||
""" | ||
import typing | ||
|
||
from pages import models | ||
|
||
|
||
class Fields: | ||
# Fields stored in DB. See class `pages.models.PageTemplate` | ||
STORED = ['name', 'h1', 'keywords', 'description', 'title', 'seo_text'] | ||
|
||
def __init__(self, page_display: 'Page'): | ||
self.page_display = page_display | ||
|
||
def __getattr__(self, item): | ||
if item in self.STORED: | ||
return self.page_display.render(item) | ||
else: | ||
return super().__getattribute__(item) | ||
|
||
|
||
class Page: | ||
# @todo #240:30m Create usage doc for page view. | ||
|
||
def __init__(self, page: models.Page, context: typing.Dict[str, typing.Any]): | ||
""" | ||
Pass context at ctor, but not render method, | ||
because client code wants the same context for many different cases. | ||
""" | ||
self.page = page | ||
self.context = context | ||
self.fields = Fields(self) | ||
|
||
def render(self, field: str): | ||
return ( | ||
self.page.template.render_field(field, self.context) | ||
if self.page.template | ||
else getattr(self.page, field) | ||
) |
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
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 . import base, pages | ||
from .base import Context, Contexts | ||
from .pages import 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,23 @@ | ||
import abc | ||
import collections | ||
import typing | ||
|
||
|
||
class Context(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def context(self) -> typing.Dict[str, typing.Any]: | ||
... | ||
|
||
|
||
class Contexts(Context): | ||
|
||
# use context as list, not as args pack (`*context`) | ||
# to move attention on homogeneous nature of args | ||
def __init__(self, contexts: typing.List[Context]): | ||
self.contexts = contexts or [] | ||
|
||
def context(self): | ||
return dict(collections.ChainMap( | ||
*[ctx.context() for ctx in self.contexts] | ||
)) |
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,12 @@ | ||
from .base import Context | ||
|
||
|
||
class Page(Context): | ||
|
||
def __init__(self, page): | ||
self.page = page | ||
|
||
def context(self): | ||
return { | ||
'page': self.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
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,13 @@ | ||
from django.test import TestCase | ||
|
||
from pages import display | ||
|
||
|
||
class TestFields(TestCase): | ||
fixtures = [] | ||
|
||
def test_attribute_error(self): | ||
# noinspection PyTypeChecker | ||
page = display.Page(None, {}) | ||
with self.assertRaises(AttributeError): | ||
_ = page.fields.bad_attr |
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
f0a7334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
240-cb4ad3dc
discovered intests/pages/test_models.py
and submitted as #267. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.f0a7334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
240-ac87cf5b
discovered inpages/utils.py
and submitted as #268. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.f0a7334
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
240-41ca8b76
discovered inpages/display.py
and submitted as #269. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.