-
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.
#240 Process DB templates with new page view class
- Loading branch information
Showing
4 changed files
with
67 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
""" | ||
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_view: 'Page'): | ||
self.page_view = page_view | ||
|
||
def __getattr__(self, item): | ||
if item in self.STORED: | ||
return self.page_view.render(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
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