Skip to content

Commit

Permalink
Stb187 tags feature (#187)
Browse files Browse the repository at this point in the history
* stb#187  Interface to implement stb side

* stb#187  Minor self-review fixes

* stb#187  Review#1 fix. Rm unused defaultdict
  • Loading branch information
duker33 authored Sep 22, 2018
1 parent 471fe2d commit 8d25b32
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions catalog/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import typing
from abc import ABC, abstractmethod
from collections import defaultdict
from functools import lru_cache, partial

from django import http
Expand Down Expand Up @@ -85,22 +84,25 @@ def number_url_map(self):
def prepare_tile_products(
products: ProductQuerySet, product_pages: QuerySet, tags: TagQuerySet=None
):
"""
This method works on STB, but not on SE.
This problem will gone when task below will be fixed.
"""

# @todo #550:60m Move prepare_tile_products func to context
# Now it's separated function with huge of inconsistent queryset deps.
assert isinstance(products, ProductQuerySet)

images = Image.objects.get_main_images_by_pages(
product_pages.filter(shopelectro_product__in=products)
)

brands = (
tags
.filter_by_products(products)
.get_brands(products)
) if tags else defaultdict(lambda: None)
images = {}
if product_pages:
images = Image.objects.get_main_images_by_pages(
# TODO - customize `prepare_tile_products` for every site
product_pages.filter(stroyprombeton_product__in=products)
)

return [
(product, images.get(product.page), brands.get(product))
(product, images.get(product.page, None))
for product in products
]

Expand Down Expand Up @@ -156,25 +158,25 @@ class AbstractPageContext(AbstractContext, ABC):
def __init__( # Ignore PyDocStyleBear
self,
url_kwargs: typing.Dict[str, str]=None,
request: http.HttpRequest=None
request: http.HttpRequest=None,
page: ModelPage=None
):
"""
:param url_kwargs: Came from `urls` module.
:param request: Came from `urls` module
"""

if url_kwargs:
assert 'slug' in url_kwargs
self.page_ = page
super().__init__(url_kwargs, request)

@property
def slug(self) -> str:
return self.url_kwargs['slug']

@property
@lru_cache(maxsize=1)
def page(self):
return ModelPage.objects.get(slug=self.slug)
return (
self.page_
if self.page_ is not None
else self.super.page
)


class AbstractProductsListContext(AbstractPageContext, ABC):
Expand All @@ -185,6 +187,7 @@ def __init__( # Ignore PyDocStyleBear
self,
url_kwargs: typing.Dict[str, str]=None,
request: http.HttpRequest=None,
page: ModelPage=None,
products: ProductQuerySet=None,
product_pages: QuerySet=None,
):
Expand All @@ -199,13 +202,17 @@ def __init__( # Ignore PyDocStyleBear

@property
def product_pages(self) -> QuerySet:
return self.product_pages_ or self.super.product_pages
return (
self.product_pages_
if self.product_pages_ is not None
else self.super.product_pages
)

@property
def products(self) -> ProductQuerySet:
if self.super:
return self.super.products
elif self.products_:
elif isinstance(self.products_, ProductQuerySet):
return self.products_
else:
raise NotImplementedError('Set products queryset')
Expand Down Expand Up @@ -250,12 +257,16 @@ def __init__( # Ignore PyDocStyleBear
# it's not good. Arg should not be default.
# That's how we'll prevent assertion.
# But we'll throw away inheritance in se#567.
assert tags, 'tags is required arg'
assert isinstance(tags, QuerySet), 'tags is required arg'
self.tags_ = tags

def get_sorting_index(self):
return int(self.url_kwargs.get('sorting', 0))

def get_undirected_sorting_options(self) -> typing.List[str]:
sorting_option = SortingOption(index=self.get_sorting_index())
return [sorting_option.field]

# @todo #550:15m Move `TaggedCategory.get_tags` to property.
# As in `products` property case.
def get_tags(self) -> typing.Optional[TagQuerySet]:
Expand All @@ -272,7 +283,7 @@ def get_tags(self) -> typing.Optional[TagQuerySet]:
@property
def products(self):
products = self.super.products
sorting_option = SortingOption(index=self.get_sorting_index())

tags = self.get_tags()
if tags:
products = (
Expand All @@ -282,8 +293,8 @@ def products(self):
# that related with products by many-to-many relation.
# @todo #550:60m Try to rm sorting staff from context.TaggedCategory.
# Or explain again why it's impossible. Now it's not clear from comment.
.distinct(sorting_option.field)
.order_by(sorting_option.field)
.distinct(*self.get_undirected_sorting_options())
.order_by(*self.get_undirected_sorting_options())
)
return products

Expand Down Expand Up @@ -354,11 +365,14 @@ class SortingCategory(AbstractProductsListContext):
def get_sorting_index(self):
return int(self.url_kwargs.get('sorting', 0))

@property
def products(self) -> ProductQuerySet:
def get_sorting_options(self) -> typing.List[str]:
sorting_index = int(self.url_kwargs.get('sorting', 0))
sorting_option = SortingOption(index=sorting_index)
return self.super.products.order_by(sorting_option.directed_field)
return [sorting_option.directed_field]

@property
def products(self) -> ProductQuerySet:
return self.super.products.order_by(*self.get_sorting_options())

def get_context_data(self):
context = self.super.get_context_data()
Expand Down Expand Up @@ -423,8 +437,11 @@ def get_context_data(self):
context = self.super.get_context_data()
self.check_pagination_args()

if not self.products:
raise http.Http404('Page without products does not exist.')
# @todo #187:30m Uncomment the if_404 check for empty products list.
# To do it fix stb tests to cover this case.

# if not self.products:
# raise http.Http404('Page without products does not exist.')

paginated = PaginatorLinks(
self.page_number,
Expand Down

1 comment on commit 8d25b32

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 8d25b32 Sep 22, 2018

Choose a reason for hiding this comment

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

Puzzle 187-26ce6496 discovered in catalog/context.py and submitted as #189. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.