Skip to content

Commit

Permalink
#550 Apply linter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 committed Sep 7, 2018
1 parent fa79b5b commit a41d3f6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 66 deletions.
19 changes: 12 additions & 7 deletions shopelectro/context.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -194,15 +198,16 @@ 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)
.get_group_tags_pairs()
)

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,
Expand Down Expand Up @@ -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(),
}

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions shopelectro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ class Meta(ModelPage.Meta): # Ignore PycodestyleBear (E303)


class TagGroup(caTagGroup):
pass


def __str__(self):
return self.name
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
63 changes: 8 additions & 55 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
),
}
Expand All @@ -111,17 +82,17 @@ 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
)
),
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
Expand All @@ -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,
Expand All @@ -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):

Expand Down Expand Up @@ -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,)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a41d3f6

Please sign in to comment.