Skip to content

Commit

Permalink
#444  Cleanup arch for new code
Browse files Browse the repository at this point in the history
Signed-off-by: duker33 <duker33@gmail.com>
  • Loading branch information
duker33 committed Aug 1, 2018
1 parent 8bd4fba commit 5544410
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from functools import partial

from django import http
Expand Down Expand Up @@ -54,30 +55,22 @@ def get(self, request, *args, **kwargs):
try:
self.object = self.get_object()
except http.Http404 as error404:
# TODO - move it to method
not_active_product = models.Product.objects.filter(
**{self.slug_field: kwargs['product_vendor_code']},
category__isnull=False,
page__is_active=False
)
if not_active_product:
product = not_active_product.first()
related_products = models.Product.objects.filter(
category=product.category,
page__is_active=True
)[10:]
self.object = product
context = super(ProductPage, self).get_context_data(object=product, **kwargs)
# context = self.get_context_data(object=product)
context.update(related_products=related_products)
return render(request, 'catalog/product_404.html', context, status=404)
raise error404
response_404 = self.render_siblings_on_404(request, **kwargs)
if response_404:
return response_404
else:
raise error404

context = self.get_context_data(object=self.object)
return self.render_to_response(context)

def get_context_data(self, **kwargs):
context = super(ProductPage, self).get_context_data(**kwargs)
product = self.object
if not product.page.is_active:
# this context required to render 404 page
# with it's own logic
return context

group_tags_pairs = (
models.Tag.objects
Expand All @@ -91,6 +84,26 @@ def get_context_data(self, **kwargs):
'group_tags_pairs': group_tags_pairs
}

def render_siblings_on_404(
self, request, **url_kwargs
) -> typing.Union[http.Http404, None]:
"""Try to render removed product's siblings on it's 404 page."""
product_inactive_qs = models.Product.objects.filter(
**{self.slug_field: url_kwargs['product_vendor_code']},
category__isnull=False,
page__is_active=False
)
if product_inactive_qs:
product = product_inactive_qs.first()
related_products = models.Product.objects.filter(
category=product.category,
page__is_active=True
)[10:]
self.object = product
context = self.get_context_data(object=product, **url_kwargs)
context.update(related_products=related_products)
return render(request, 'catalog/product_404.html', context, status=404)


# SHOPELECTRO-SPECIFIC VIEWS
@set_csrf_cookie
Expand Down

0 comments on commit 5544410

Please sign in to comment.