Skip to content

Commit

Permalink
#701 page context (#702)
Browse files Browse the repository at this point in the history
* Create Page context class

Instantiate page context

Bump rf to temp version

fix missed import

Remove excess import

Fix Page context class

Bump refarm-site version

Remove dangling optional_context

* Fix name of context import
  • Loading branch information
ArtemijRodionov committed Jan 22, 2019
1 parent ce492b1 commit 2543dcd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ ua-parser==0.8.0
user-agents==1.1.0
sorl-thumbnail==12.5.0
https://github.com/selwin/django-user_agents/archive/master.zip
https://github.com/fidals/refarm-site/archive/0.4.26.zip
https://github.com/fidals/refarm-site/archive/0.4.27.zip
28 changes: 28 additions & 0 deletions shopelectro/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from functools import partial

from catalog.newcontext import Context, Tags


class Page(Context):

def __init__(self, page, tags: Tags):
self._page = page
self._tags = tags

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

tags_qs = self._tags.qs()
self._page.get_template_render_context = partial(
template_context, self._page, tags_qs.as_title(), tags_qs
)

return {
'page': self._page,
'skip_canonical': tags_qs.exists(),
}
43 changes: 11 additions & 32 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import typing
from functools import partial

from django import http
from django.conf import settings
Expand All @@ -12,7 +11,7 @@
from images.models import Image
from pages import views as pages_views

from shopelectro import models
from shopelectro import models, context as se_context
from shopelectro.views.helpers import set_csrf_cookie

PRODUCTS_ON_PAGE_PC = 48
Expand All @@ -34,7 +33,7 @@ def get_view_type(request):
# @todo #683:60m Create context class(es) for catalog page representation.
# Created class(es) will compose the context classes for catalog views.
# Remove get_catalog_context in favor of created class(es).
def get_catalog_context(request, category, raw_tags, page_number, per_page, sorting_index):
def get_catalog_context(request, page, category, raw_tags, page_number, per_page, sorting_index):
all_tags = newcontext.Tags(models.Tag.objects.all())
selected_tags = newcontext.tags.ParsedTags(
tags=all_tags,
Expand Down Expand Up @@ -70,17 +69,10 @@ def get_catalog_context(request, category, raw_tags, page_number, per_page, sort
images = newcontext.products.ProductImages(paginated_products, Image.objects.all())
brands = newcontext.products.ProductBrands(paginated_products, all_tags)
grouped_tags = newcontext.tags.GroupedTags(all_tags)
page = se_context.Page(page, selected_tags)

contexts = newcontext.Contexts(paginated_products, images, brands, grouped_tags)
# @todo #683:60m Create context class(es).
# Remove optional_context from get_catalog_context and
# patching of page.get_template_render_context in favor of created class(es).
optional_context = {
'skip_canonical': selected_tags.qs().exists(),
'total_products': products.qs().count(),
'selected_tags': selected_tags.qs(),
}
return contexts, optional_context
contexts = newcontext.Contexts(page, paginated_products, images, brands, grouped_tags)
return contexts


# CATALOG VIEWS
Expand Down Expand Up @@ -210,8 +202,9 @@ def get_context_data(self, **kwargs):
"""Add sorting options and view_types in context."""
sorting_index = int(self.kwargs.get('sorting', 0))

contexts, optional_context = get_catalog_context(
contexts = get_catalog_context(
request=self.request,
page=self.object,
category=self.object.model,
raw_tags=self.kwargs.get('tags'),
page_number=int(self.request.GET.get('page', 1)),
Expand All @@ -221,24 +214,9 @@ def get_context_data(self, **kwargs):
sorting_index=sorting_index,
)

selected_tags = optional_context['selected_tags']
if selected_tags:
def template_context(page, tag_titles, tags):
return {
'page': page,
'tag_titles': tag_titles,
'tags': tags,
}

page = self.object
page.get_template_render_context = partial(
template_context, page, selected_tags.as_title(), selected_tags
)

return {
**super().get_context_data(**kwargs),
**contexts.context(),
**optional_context,
'view_type': get_view_type(self.request),
'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),
'limits': settings.CATEGORY_STEP_MULTIPLIERS,
Expand Down Expand Up @@ -266,12 +244,13 @@ def load_more(request, slug, offset=0, limit=0, sorting=0, tags=None):
# 11 // 12 = 0, 0 // 12 = 0 but it should be the first page
# 12 // 12 = 1, 23 // 12 = 1, but it should be the second page
page_number = (offset // products_on_page) + 1
category = get_object_or_404(models.CategoryPage, slug=slug).model
page = get_object_or_404(models.CategoryPage, slug=slug)
sorting_index = int(sorting)

contexts, _ = get_catalog_context(
contexts = get_catalog_context(
request=request,
category=category,
page=page,
category=page.model,
raw_tags=tags,
page_number=page_number,
per_page=products_on_page,
Expand Down
4 changes: 2 additions & 2 deletions templates/catalog/category.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1 class="category-title" data-name="{{ category.name }}">{{ page.display_h1|ca
<p class="products-showed">
Показано товаров
<span class="font-bold js-products-showed-count"> {{ paginated.showed_count }} </span>
из <span class="font-bold js-total-products">{{ total_products }}</span>.
из <span class="font-bold js-total-products">{{ paginated.total_products }}</span>.
</p>

<div class="row product-card-wrapper" id="products-wrapper"
Expand All @@ -49,7 +49,7 @@ <h1 class="category-title" data-name="{{ category.name }}">{{ page.display_h1|ca
<p class="products-showed-text">
Показано товаров
<span class="js-products-showed-count">{{ paginated.showed_count }}</span> из
<span class="js-total-products">{{ total_products }}</span>
<span class="js-total-products">{{ paginated.total_products }}</span>
</p>
</div>
</div>
Expand Down

1 comment on commit 2543dcd

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 2543dcd Jan 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 683-b33e7e30 disappeared from shopelectro/views/catalog.py, that's why I closed #701. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.