Skip to content

Commit

Permalink
Set of fast images fixes from PO (#644)
Browse files Browse the repository at this point in the history
* #642  Fix links on products with images

* #641  Repair stale previews

* #643  Add featured new year goods on main page

* #641  Minor fix for images test

* #641  Fix 404 siblings functions
  • Loading branch information
duker33 committed Nov 27, 2018
1 parent ebfdca3 commit 7c785f5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,10 @@ for info about both of them.
## Новый год
- Как добавляли - https://github.com/fidals/shopelectro/pull/224
- Как удаляли - https://github.com/fidals/shopelectro/pull/278

## Товары без картинок
Есть отдельная ссылка на товары без картинок для удобства
- [Separated no-images link](https://www.shopelectro.ru/catalog/no-images/).
It's good to print list.
- [Link to no-images prods in admin panel](https://www.shopelectro.ru/admin/shopelectro/productpage/?has_images=no&is_active__exact=1).
It has filters.
Binary file added front/images/main/featured-new-year-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/main/featured-new-year-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added front/images/main/featured-new-year-3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions shopelectro/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ class ProductPage(TestCase):
def setUp(self):
self.product = models.Product.objects.first()

def get_product_page(self, product: models.Product=None):
product = product or self.product
return self.client.get(product.url)

def get_product_soup(self, product: models.Product=None) -> BeautifulSoup:
product_page = self.get_product_page(product)
return BeautifulSoup(
product_page.content.decode('utf-8'),
'html.parser'
)

def test_orphan_product(self):
self.product.category = None
self.product.save()
Expand All @@ -483,7 +494,7 @@ def test_orphan_product(self):

def test_related_products(self):
"""404 page of sometimes removed product should contain product's siblings."""
response = self.client.get(self.product.url)
response = self.get_product_page()
# 404 page should show 10 siblings. We'll check the last one
sibling_product = self.product.category.products.all()[9]
self.assertTrue(
Expand All @@ -495,14 +506,25 @@ def test_related_products_on_404(self):
self.product.page.is_active = False
self.product.save() # saves product.page too

response = self.client.get(self.product.url)
response = self.get_product_page()
# 404 page should show 10 siblings. We'll check the last one
sibling_product = self.product.category.products.all()[9]
self.assertEqual(response.status_code, 404)
self.assertTrue(
sibling_product.name in str(response.content)
)

def test_related_product_images(self):
"""Check previews for product's siblings on product page."""
product_soup = self.get_product_soup(product=models.Product.objects.get(id=19))
img_path = (
product_soup
.find_all(class_='stuff-top-item')[-1]
.find('img')['src']
)
# app shows logo image if preview can't generated
self.assertNotIn('logo', img_path)


class ProductPageSchema(TestCase):

Expand Down
27 changes: 18 additions & 9 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,31 @@ def get_context_data(self, **kwargs):
# with it's own logic
return context_

tile_products = self.product.get_siblings(
offset=settings.PRODUCT_SIBLINGS_COUNT
)
product_images = self.get_images_context_data(tile_products)['product_images']

return {
**context_,
'price_bounds': settings.PRICE_BOUNDS,
'group_tags_pairs': self.product.get_params(),
'product_images': self.get_images_context_data()['product_images'],
'tile_products': self.product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT),
'product_images': product_images,
'tile_products': tile_products,
}

def get_images_context_data(self) -> dict:
def get_images_context_data(self, products) -> dict:
"""Return images for given products."""
products_to_filter = [self.product, *products]
product_ids_to_filter = [p.id for p in products_to_filter]
return (
se_context.ProductImages(
url_kwargs={},
request=self.request,
page=self.product.page,
products=models.Product.objects.filter(id=self.product.id),
products=models.Product.objects.filter(id__in=product_ids_to_filter),
product_pages=models.ProductPage.objects.filter(
shopelectro_product=self.product
shopelectro_product__in=products_to_filter
),
).get_context_data()
)
Expand All @@ -98,18 +106,19 @@ def render_siblings_on_404(
page__is_active=False
).first()
if inactive_product:
siblings = inactive_product.get_siblings(
offset=settings.PRODUCT_SIBLINGS_COUNT
)
self.object = inactive_product
context_ = self.get_context_data(
object=inactive_product,
tile_products=inactive_product.get_siblings(
offset=settings.PRODUCT_SIBLINGS_COUNT
),
tile_products=siblings,
tile_title='Возможно вас заинтересуют похожие товары:',
**url_kwargs,
)

context_['product_images'] = (
self.get_images_context_data()['product_images']
self.get_images_context_data(siblings)['product_images']
)
return render(request, 'catalog/product_404.html', context_, status=404)

Expand Down
54 changes: 54 additions & 0 deletions templates/index/featured_new_year.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% load static %}

<div class="featured-product-wrapper clearfix">
<div class="featured-product-item">
<div class="stuff-wrapper">
<div class="featured-product-item-title">
<div>
<a class="featured-product-item-link" href="{% url 'category' 'novogodnie-girliandy-207' %}">
Новогодние гирлянды <b>от 119 руб.</b>
</a>
</div>
</div>
<a class="featured-product-img-wrapper" href="{% url 'category' 'novogodnie-girliandy-207' %}">
<img src="{% static 'images/main/featured-new-year-1.jpg' %}"
class="img-responsive img-centered" alt="Новогодние гирлянды">
</a>
</div>
</div>

<div class="featured-product-item">
<div class="stuff-wrapper">
<div class="featured-product-item-title">
<div>
<a class="featured-product-item-link" href="{% url 'category' slug='novogodnie-lazernye-proektory-209' %}">
Лазерные проекторы <b>от 941 руб.</b>
</a>
</div>
</div>
<a class="featured-product-img-wrapper" href="{% url 'category' slug='novogodnie-lazernye-proektory-209' %}">
<img src="{% static 'images/main/featured-new-year-2.jpg' %}"
class="img-responsive img-centered" alt="Лазерные проекторы">
</a>
</div>
</div>

<div class="featured-product-item">
<div class="stuff-wrapper">
<div class="featured-product-item-title">
<div>
<a class="featured-product-item-link" href="{% url 'category' 'pirotekhnika' %}">
Новогодние фейерверки и салюты <b>от 25 руб.</b>
</a>
</div>
</div>
<a class="featured-product-img-wrapper" href="{% url 'category' 'pirotekhnika' %}">
<img src="{% static 'images/main/featured-new-year-3.jpg' %}"
class="img-responsive img-centered"
alt="Новогодние фейерверки и салюты"
style="padding-top: 50px;"
>
</a>
</div>
</div>
</div>
2 changes: 2 additions & 0 deletions templates/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{% block content %}
<div class="page-front">
<p class="h1">Большой выбор аккумуляторов, батареек и блоков питания:</p>

{% include 'index/featured_new_year.html' %}
{% include 'index/featured.html' %}

{# Tile on main page #}
Expand Down

0 comments on commit 7c785f5

Please sign in to comment.