Skip to content

Commit

Permalink
#550 Fork DBTemplateContext class. Improve pipe mech
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 committed Sep 2, 2018
1 parent 6e38729 commit 3e2533a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
55 changes: 49 additions & 6 deletions shopelectro/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ def merge_products_context(products):

class ObjectsComposition:

super = object()
super: 'ObjectsComposition' = None

# TODO - resolve objects good piping
def __or__(self, other):
self.super = other
return self
def __or__(self, other: 'ObjectsComposition'):
other.super = self
return other


class AbstractContext(ObjectsComposition):

super: 'AbstractContext' = None

def __init__(
self,
# TODO slug maybe part of url_kwargs
Expand Down Expand Up @@ -108,7 +110,11 @@ def get_context_data(self) -> typing.Dict[str, typing.Any]:
raise NotImplementedError


# TODO make it ABC or smth
class ProductsListContext(AbstractContext):

super: 'ProductsListContext' = None

@property
def products(self) -> QuerySet:
raise NotImplementedError
Expand Down Expand Up @@ -188,12 +194,49 @@ def get_context_data(self):
}


class DBTemplateContext(AbstractContext):
"""Processes some page data fields as templates with each own context."""

@property
@lru_cache(maxsize=1)
def page(self):
page = self.super.page
context = self.get_super_context_data()

def template_context(page, tag_titles, tags):
return {
'page': page,
'tag_titles': tag_titles,
'tags': tags,
}

tags = context['tags']
if tags:
# TODO - move to QuerySet
tag_titles = models.serialize_tags_to_title(tags)
page.get_template_render_context = partial(
template_context, page, tag_titles, tags
)

return page

@lru_cache(maxsize=1)
def get_super_context_data(self):
"""Just for cache."""
return self.super.get_context_data()

@lru_cache(maxsize=1)
def get_context_data(self):
return {
**self.get_super_context_data(),
'page': self.page,
}


# class PaginatedCatalogContext:
# ...
#
#
# class DBTemplateContext:
# ...
#
#
# class CatalogView:
Expand Down
3 changes: 3 additions & 0 deletions shopelectro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ def serialize_tags(
type_delimiter: str,
group_delimiter: str,
) -> str:
if not tags:
return ''

group_tags_map = tags.get_group_tags_pairs()

_, tags_by_group = zip(*group_tags_map)
Expand Down
46 changes: 7 additions & 39 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,52 +189,20 @@ def get_products(self) -> QuerySet:

def get_context_data(self, **kwargs):
"""Add sorting options and view_types in context."""
context_ = context.TaggedCategoryContext() | context.CategoryContext(
self.object.slug, kwargs, self.request
context_ = (
context.CategoryContext(
self.object.slug, self.kwargs, self.request
)
| context.TaggedCategoryContext()
| context.DBTemplateContext()
)
return {
**super().get_context_data(**kwargs),
**context_.get_context_data(),
}


class DBContextCategoryPage(CategoryPage):
"""Process db page data as db context."""

def get_tags(self) -> typing.Optional[models.TagQuerySet]:

@lru_cache(maxsize=64)
def get_tags(request_tags_: str):
slugs = models.Tag.parse_url_tags(request_tags_)
return models.Tag.objects.filter(slug__in=slugs)

request_tags = self.kwargs.get('tags')
if not request_tags:
return None
return get_tags(request_tags)

def get_object(self, queryset=None):
page = super().get_object(queryset)

def template_context(page, tag_titles, tags):
return {
'page': page,
'tag_titles': tag_titles,
'tags': tags,
}

tags = self.get_tags()
if tags:
# TODO - move to QuerySet
tag_titles = models.serialize_tags_to_title(tags)
page.get_template_render_context = partial(
template_context, page, tag_titles, tags
)

return page


class SortingCategoryPage(DBContextCategoryPage):
class SortingCategoryPage(CategoryPage):

def get_products(self) -> QuerySet:
sorting_index = int(self.kwargs.get('sorting', 0))
Expand Down

0 comments on commit 3e2533a

Please sign in to comment.