Skip to content

Commit

Permalink
#444  Review#1 fixes. Collapse some code and fix products offsetting
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 5544410 commit a3127c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
4 changes: 3 additions & 1 deletion shopelectro/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,14 @@ def test_orphan_product(self):
self.assertEqual(response.status_code, 404)

def test_related_products_on_404(self):
"""404 page of sometimes removed product should contain product's siblings."""
product = Product.objects.first()
product.page.is_active = False
product.save() # saves product.page too

response = self.client.get(product.url)
sibling_product = product.category.products.last()
# 404 page should show 10 siblings. We'll check the last one
sibling_product = product.category.products.all()[9]
self.assertEqual(response.status_code, 404)
self.assertTrue(
sibling_product.name in str(response.content)
Expand Down
15 changes: 7 additions & 8 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,18 @@ 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(
inactive_product = 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()
).first()
if inactive_product:
related_products = models.Product.objects.filter(
category=product.category,
category=inactive_product.category,
page__is_active=True
)[10:]
self.object = product
context = self.get_context_data(object=product, **url_kwargs)
)[:10]
self.object = inactive_product
context = self.get_context_data(object=inactive_product, **url_kwargs)
context.update(related_products=related_products)
return render(request, 'catalog/product_404.html', context, status=404)

Expand Down

0 comments on commit a3127c7

Please sign in to comment.