From a41d3f667b4446102fb551d63f78e85bab938525 Mon Sep 17 00:00:00 2001 From: duker Date: Fri, 7 Sep 2018 04:18:39 +0300 Subject: [PATCH] #550 Apply linter rules --- shopelectro/context.py | 19 +++++++---- shopelectro/models.py | 5 +-- shopelectro/views/catalog.py | 63 +++++------------------------------- 3 files changed, 21 insertions(+), 66 deletions(-) diff --git a/shopelectro/context.py b/shopelectro/context.py index ae9722e8..bf41cffa 100644 --- a/shopelectro/context.py +++ b/shopelectro/context.py @@ -1,5 +1,6 @@ """ Contains view context classes. + Context is our own concept. It's part of the View abstract layer in MTV paradigm. Data flow looks like this: @@ -81,7 +82,7 @@ def number_url_map(self): # @todo #550:30m Split to ProductImagesContext and ProductBrandContext @lru_cache(maxsize=64) -def merge_products_context(products: QuerySet): +def prepare_tile_products(products: QuerySet): assert isinstance(products, QuerySet) images = Image.objects.get_main_images_by_pages( @@ -108,6 +109,7 @@ def __or__(self, other: 'ObjectsComposition'): other.super = self return other + # @todo #550:120m Move context realization to pure to objects composition. # Discussed some thoughts with Artemiy via call. # Artemiy will do it. @@ -117,7 +119,7 @@ class AbstractContext(ObjectsComposition, ABC): super: 'AbstractContext' = None - def __init__( + def __init__( # Ignore PyDocStyleBear self, url_kwargs: typing.Dict[str, str]=None, request: http.HttpRequest=None @@ -126,6 +128,7 @@ def __init__( :param url_kwargs: Came from `urls` module. :param request: Came from `urls` module """ + self.url_kwargs_ = url_kwargs or {} self.request_ = request @@ -146,7 +149,7 @@ class AbstractPageContext(AbstractContext, ABC): super: 'AbstractPageContext' = None - def __init__( + def __init__( # Ignore PyDocStyleBear self, url_kwargs: typing.Dict[str, str]=None, request: http.HttpRequest=None @@ -155,6 +158,7 @@ def __init__( :param url_kwargs: Came from `urls` module. :param request: Came from `urls` module """ + if url_kwargs: assert 'slug' in url_kwargs super().__init__(url_kwargs, request) @@ -194,7 +198,8 @@ def get_context_data(self): # Depends on updating to python3.7 view_type = self.request.session.get('view_type', 'tile') - # @todo - create product.get_brand instead + # @todo #550:60m Create `Product.get_brand` + # Or `ProductQuerySet.get_brand`. group_tags_pairs = ( models.Tag.objects .filter_by_products(self.products) @@ -202,7 +207,7 @@ def get_context_data(self): ) return { - 'products_data': merge_products_context(self.products), + 'products_data': prepare_tile_products(self.products), 'group_tags_pairs': group_tags_pairs, # can be `tile` or `list`. Defines products list layout. 'view_type': view_type, @@ -312,7 +317,7 @@ def get_context_data(self): context = self.super.get_context_data() return { **context, - 'products_data': merge_products_context(self.products), + 'products_data': prepare_tile_products(self.products), 'sort': self.get_sorting_index(), } @@ -360,7 +365,7 @@ def get_context_data(self): return { **context, - 'products_data': merge_products_context(products), + 'products_data': prepare_tile_products(products), 'total_products': total_products, 'products_count': (page_number - 1) * products_on_page + products.count(), 'paginated': paginated, diff --git a/shopelectro/models.py b/shopelectro/models.py index 71663af4..4c43ee8c 100644 --- a/shopelectro/models.py +++ b/shopelectro/models.py @@ -202,8 +202,6 @@ class Meta(ModelPage.Meta): # Ignore PycodestyleBear (E303) class TagGroup(caTagGroup): - pass - def __str__(self): return self.name @@ -242,7 +240,7 @@ def get_brands(self, products: typing.List[Product]) -> typing.Dict[Product, 'Ta if product in brand.products.all() } - def as_string( + def as_string( # Ignore PyDocStyleBear self, field_name: str, type_delimiter: str, @@ -322,7 +320,6 @@ class Tag(models.Model): TagGroup, on_delete=models.CASCADE, null=True, related_name='tags', ) - def save(self, *args, **kwargs): if not self.slug: # same slugify code used in PageMixin object diff --git a/shopelectro/views/catalog.py b/shopelectro/views/catalog.py index a40842f3..9e3d1fd4 100644 --- a/shopelectro/views/catalog.py +++ b/shopelectro/views/catalog.py @@ -1,15 +1,13 @@ import typing -from functools import lru_cache from django import http from django.conf import settings -from django.core.paginator import Paginator, InvalidPage +from django.core.paginator import Paginator from django.shortcuts import render, get_object_or_404 from django.views.decorators.http import require_POST from django_user_agents.utils import get_user_agent from catalog.views import catalog -from images.models import Image from pages import views as pages_views from shopelectro import context, models @@ -25,38 +23,11 @@ def get_products_count(request): return PRODUCTS_ON_PAGE_MOB if mobile_view else PRODUCTS_ON_PAGE_PC -class SortingOption: - def __init__(self, index=0): - options = settings.CATEGORY_SORTING_OPTIONS[index] - self.label = options['label'] - self.field = options['field'] - self.direction = options['direction'] - - @property - def directed_field(self): - return self.direction + self.field - - # CATALOG VIEWS class CategoryTree(catalog.CategoryTree): category_model = models.Category -def prepare_tile_products(products): - images = Image.objects.get_main_images_by_pages( - models.ProductPage.objects.filter( - shopelectro_product__in=products - ) - ) - categories = models.Category.objects.get_root_categories_by_products( - products - ) - return [ - (product, images.get(product.page), categories.get(product)) - for product in products - ] - - @set_csrf_cookie class ProductPage(catalog.ProductPage): pk_url_kwarg = None @@ -95,7 +66,7 @@ def get_context_data(self, **kwargs): **context, 'price_bounds': settings.PRICE_BOUNDS, 'group_tags_pairs': product.get_params(), - 'tile_products': prepare_tile_products( + 'tile_products': context.prepare_tile_products( product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT) ), } @@ -111,9 +82,9 @@ def render_siblings_on_404( ).first() if inactive_product: self.object = inactive_product - context = self.get_context_data( + context_ = self.get_context_data( object=inactive_product, - tile_products=prepare_tile_products( + tile_products=context.prepare_tile_products( inactive_product.get_siblings( offset=settings.PRODUCT_SIBLINGS_COUNT ) @@ -121,7 +92,7 @@ def render_siblings_on_404( tile_title='Возможно вас заинтересуют похожие товары:', **url_kwargs, ) - return render(request, 'catalog/product_404.html', context, status=404) + return render(request, 'catalog/product_404.html', context_, status=404) # SHOPELECTRO-SPECIFIC VIEWS @@ -141,7 +112,7 @@ def get_context_data(self, **kwargs): .prefetch_related('category') .select_related('page') ) - tile_products = prepare_tile_products(top_products) + tile_products = context.prepare_tile_products(top_products) return { **context, @@ -151,24 +122,6 @@ def get_context_data(self, **kwargs): } -@lru_cache(maxsize=64) -def merge_products_context(products): - images = Image.objects.get_main_images_by_pages( - models.ProductPage.objects.filter(shopelectro_product__in=products) - ) - - brands = ( - models.Tag.objects - .filter_by_products(products) - .get_brands(products) - ) - - return [ - (product, images.get(product.page), brands.get(product)) - for product in products - ] - - @set_csrf_cookie class CategoryPage(catalog.CategoryPage): @@ -213,7 +166,7 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): # 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=category_slug).model - sorting_option = SortingOption(index=int(sorting)) + sorting_option = context.SortingOption(index=int(sorting)) all_products = models.Product.actives.get_category_descendants( category, ordering=(sorting_option.directed_field,) @@ -242,7 +195,7 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): view = request.session.get('view_type', 'tile') return render(request, 'catalog/category_products.html', { - 'products_data': merge_products_context(products), + 'products_data': context.prepare_tile_products(products), 'paginated': paginated, 'paginated_page': paginated_page, 'view_type': view,