diff --git a/requirements.txt b/requirements.txt index 9f87c730..4d852129 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,4 @@ ua-parser==0.8.0 user-agents==1.1.0 sorl-thumbnail==12.4a1 https://github.com/selwin/django-user_agents/archive/master.zip -https://github.com/fidals/refarm-site/archive/0.4.11.zip +https://github.com/fidals/refarm-site/archive/0.4.12.zip diff --git a/shopelectro/context.py b/shopelectro/context.py new file mode 100644 index 00000000..1d3da4c1 --- /dev/null +++ b/shopelectro/context.py @@ -0,0 +1,32 @@ +import typing + +from catalog.context import AbstractProductsListContext +from catalog.models import ProductQuerySet +from images.models import Image + + +class ProductImages(AbstractProductsListContext): + + @property + def images(self) -> typing.Dict[int, Image]: + assert isinstance(self.products, ProductQuerySet) + + images = {} + if self.product_pages: + images = Image.objects.get_main_images_by_pages( + self.product_pages.filter(shopelectro_product__in=self.products) + ) + + return { + product.id: images.get(product.page) + for product in self.products + } + + def get_context_data(self): + return { + 'product_images': self.images, + **( + self.super.get_context_data() + if self.super else {} + ), + } diff --git a/shopelectro/tests/tests_views.py b/shopelectro/tests/tests_views.py index bd3dc101..636554d8 100644 --- a/shopelectro/tests/tests_views.py +++ b/shopelectro/tests/tests_views.py @@ -19,8 +19,6 @@ from django.urls import reverse from django.utils.translation import ugettext as _ -from catalog import context - from shopelectro import models from shopelectro.views.service import generate_md5_for_ya_kassa, YANDEX_REQUEST_PARAM from shopelectro.tests.helpers import create_doubled_tag @@ -77,20 +75,6 @@ def get_category_page( )) -class CatalogPage(BaseCatalogTestCase): - - def test_merge_product_cache(self): - """Context merging should cached.""" - products = models.Product.objects.all()[:2] - product_pages = models.ProductPage.objects.all() - with self.assertNumQueries(1): - # N db queries without before cached - context.prepare_tile_products(products, product_pages) - with self.assertNumQueries(0): - # no db queries after cached - context.prepare_tile_products(products, product_pages) - - class CatalogTags(BaseCatalogTestCase): def test_category_page_contains_all_tags(self): @@ -258,7 +242,7 @@ def test_pagination_step(self): """Category page contains `pagination_step` count of products in list.""" pagination_step = 25 response = self.get_category_page(query_string={'step': pagination_step}) - self.assertEqual(len(response.context['products_data']), pagination_step) + self.assertEqual(len(response.context['products']), pagination_step) def test_pagination_404(self): """Category page returns 404 for a nonexistent page number.""" diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py index 6b0b9d09..6ab2691b 100644 --- a/shopelectro/views/catalog.py +++ b/shopelectro/views/catalog.py @@ -11,6 +11,7 @@ from catalog.views import catalog from pages import views as pages_views +from shopelectro import context as se_context from shopelectro import models from shopelectro.views.helpers import set_csrf_cookie @@ -70,16 +71,13 @@ def get_context_data(self, **kwargs): **context_, 'price_bounds': settings.PRICE_BOUNDS, 'group_tags_pairs': self.product.get_params(), - 'product_images': self.get_images_context().get_context_data()['product_images'], - 'tile_products': context.prepare_tile_products( - self.product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT), - models.ProductPage.objects.all() - ), + 'product_images': self.get_images_context_data()['product_images'], + 'tile_products': self.product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT), } - def get_images_context(self): + def get_images_context_data(self) -> dict: return ( - context.ProductImages( + se_context.ProductImages( url_kwargs={}, request=self.request, page=self.product.page, @@ -87,7 +85,7 @@ def get_images_context(self): product_pages=models.ProductPage.objects.filter( shopelectro_product=self.product ), - ) + ).get_context_data() ) def render_siblings_on_404( @@ -103,18 +101,15 @@ def render_siblings_on_404( self.object = inactive_product context_ = self.get_context_data( object=inactive_product, - tile_products=context.prepare_tile_products( - inactive_product.get_siblings( - offset=settings.PRODUCT_SIBLINGS_COUNT - ), - models.ProductPage.objects.all() + tile_products=inactive_product.get_siblings( + offset=settings.PRODUCT_SIBLINGS_COUNT ), tile_title='Возможно вас заинтересуют похожие товары:', **url_kwargs, ) context_['product_images'] = ( - self.get_images_context().get_context_data()['product_images'] + self.get_images_context_data()['product_images'] ) return render(request, 'catalog/product_404.html', context_, status=404) @@ -136,10 +131,7 @@ def get_context_data(self, **kwargs): .select_related('page') ) if not mobile_view: - tile_products = context.prepare_tile_products( - top_products, - models.ProductPage.objects.all() - ) + tile_products = top_products return { **context_, @@ -147,21 +139,21 @@ def get_context_data(self, **kwargs): 'category_tile': settings.MAIN_PAGE_TILE, 'tile_products': tile_products, 'product_images': ( - self - .get_images_context(products=top_products) - .get_context_data()['product_images'] + self.get_products_context_data( + products=top_products + )['product_images'] ) } - def get_images_context(self, products=None, product_pages=None): + def get_products_context_data(self, products=None, product_pages=None) -> dict: return ( - context.ProductImages( + se_context.ProductImages( url_kwargs={}, # Ignore CPDBear request=self.request, page=self.object, products=products or models.Product.objects.all(), product_pages=product_pages or models.ProductPage.objects.all(), - ) + ).get_context_data() ) @@ -176,12 +168,13 @@ def get_context_data(self, **kwargs): request=self.request, page=self.object, products=models.Product.objects.all(), - product_pages=models.ProductPage.objects.all(), + product_pages=models.ProductPage.objects.all(), # Ignore CPDBear ) | context.TaggedCategory(tags=models.Tag.objects.all()) | context.SortingCategory() # requires TaggedCategory | context.PaginationCategory() # requires SortingCategory - | context.ProductImages() + | context.ProductBrands() # requires TaggedCategory + | se_context.ProductImages() | context.DBTemplate() # requires TaggedCategory ) return { @@ -244,7 +237,7 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): products = paginated_page.object_list view = request.session.get('view_type', 'tile') - context_ = ( + data_from_context = ( context.Category( url_kwargs={}, request=request, @@ -254,19 +247,18 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): shopelectro_product__in=products ), ) - | context.ProductImages( - products=products, - product_pages=models.ProductPage.objects.filter( - shopelectro_product__in=products - ) - ) - ) + | context.TaggedCategory(tags=models.Tag.objects.all()) + | context.SortingCategory() # requires TaggedCategory + | context.PaginationCategory() # requires SortingCategory + | context.ProductBrands() # requires TaggedCategory + | se_context.ProductImages() + | context.DBTemplate() # requires TaggedCategory + ).get_context_data() return render(request, 'catalog/category_products.html', { - 'products_data': context.prepare_tile_products( - products, models.ProductPage.objects.all() - ), - 'product_images': context_.get_context_data()['product_images'], + 'products': products, + 'product_images': data_from_context['product_images'], + 'product_brands': data_from_context['product_brands'], 'paginated': paginated, 'paginated_page': paginated_page, 'view_type': view, diff --git a/templates/catalog/category_products.html b/templates/catalog/category_products.html index fb40f8d7..402b6348 100644 --- a/templates/catalog/category_products.html +++ b/templates/catalog/category_products.html @@ -2,7 +2,7 @@ {% load se_extras %} {% load user_agents %} -{% for product, brand in products_data %} +{% for product in products %}
@@ -40,14 +40,16 @@
- + {% with brand=product_brands|get_item:product.id %} + + {% endwith %}
{% endfor %} - + diff --git a/templates/layout/tile_products.html b/templates/layout/tile_products.html index 18d51b30..d57baa79 100644 --- a/templates/layout/tile_products.html +++ b/templates/layout/tile_products.html @@ -6,7 +6,7 @@ {% if tile_products %}

{{ tile_title }}

- {% for product, root_category in tile_products %} + {% for product in tile_products %}
-
{{ root_category.name }}