Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#645 Fix previews in load_more #650

Merged
merged 6 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion shopelectro/fixtures/dump.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions shopelectro/management/commands/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Command(BaseCommand):
'assets/gold_deer.jpg'
)

PRODUCT_WITH_IMAGE = 1
PRODUCTS_WITH_IMAGE = [1, 113]

def __init__(self):
super(BaseCommand, self).__init__()
Expand Down Expand Up @@ -94,6 +94,8 @@ def save_dump():
output='shopelectro/fixtures/dump.json'
)

# @todo #645:30m Move test_db's product_id to generator. stb2
# Now it uses dirty autoincrement.
@property
def product_id(self):
self._product_id += 1
Expand Down Expand Up @@ -161,7 +163,8 @@ def create_product(parent: se_models.Category, tags_, price_factor):
for tag in tags_:
product.tags.add(tag)

if product.id == self.PRODUCT_WITH_IMAGE:
if product.id in self.PRODUCTS_WITH_IMAGE:
create_images(product.page)
create_images(product.page)

def fill_with_products(to_fill, tags_, count):
Expand Down
28 changes: 25 additions & 3 deletions shopelectro/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def reverse_catalog_url(
url: str,
route: str,
route_kwargs: dict,
tags: models.TagQuerySet=None,
sorting: int=None,
Expand All @@ -43,7 +43,7 @@ def reverse_catalog_url(
if sorting is not None:
route_kwargs['sorting'] = sorting

return f'{reverse(url, kwargs=route_kwargs)}{query_string}'
return f'{reverse(route, kwargs=route_kwargs)}{query_string}'


def get_page_number(response):
Expand All @@ -68,10 +68,11 @@ def get_category_page(
tags: models.TagQuerySet=None,
sorting: int=None,
query_string: dict=None,
route='category',
):
category = category or self.category
return self.client.get(reverse_catalog_url(
'category', {'slug': category.page.slug}, tags, sorting, query_string,
route, {'slug': category.page.slug}, tags, sorting, query_string,
))


Expand Down Expand Up @@ -309,10 +310,13 @@ class LoadMore(TestCase):

fixtures = ['dump.json']
DEFAULT_LIMIT = 48
PRODUCT_ID_WITH_IMAGE = 114

def setUp(self):
self.category = models.Category.objects_.root_nodes().select_related('page').first()

# @todo #645:15m Reuse get_category_page method in LoadMore.load_more
# BaseCatalogTestCase.get_category_page.
def load_more(
self,
category: models.Category=None,
Expand All @@ -334,6 +338,14 @@ def load_more(
'load_more', route_kwargs, tags, sorting, query_string,
))

def get_load_more_soup(self, *args, **kwargs) -> BeautifulSoup:
"""Uses interface of `self.load_more` method."""
load_more_response = self.load_more(*args, **kwargs)
return BeautifulSoup(
load_more_response.content.decode('utf-8'),
'html.parser'
)

def test_pagination_numbering_first_page(self):
self.assertEqual(get_page_number(self.load_more()), 1)

Expand All @@ -351,6 +363,16 @@ def test_pagination_numbering_rest_page(self):
2,
)

def test_image_previews(self):
"""Load_more button should load product with image previews."""
load_more_soup = self.get_load_more_soup(offset=self.DEFAULT_LIMIT)
img_path = (
load_more_soup
.find('a', href=f'/catalog/products/{self.PRODUCT_ID_WITH_IMAGE}/')
.find('img')['src']
)
self.assertNotIn('logo', img_path)


@tag('fast')
class SitemapXML(TestCase):
Expand Down
9 changes: 7 additions & 2 deletions shopelectro/views/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,19 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None):
products = paginated_page.object_list
view = request.session.get('view_type', 'tile')

products_to_filter = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems products and products_to_filter are the same. How will exactly it help and what is the difference between them?

Copy link
Contributor Author

@duker33 duker33 Dec 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artemiy312 , products list has been sliced. That's why it can't be arg to context.
I'll append comment

models.Product.objects
.filter(id__in=[p.id for p in products])
.order_by(sorting_option.directed_field)
)
data_from_context = (
context.Category(
url_kwargs={},
request=request,
page=category.page,
products=models.Product.objects.all(),
products=products_to_filter,
product_pages=models.ProductPage.objects.filter(
shopelectro_product__in=products
shopelectro_product__in=products_to_filter
),
)
| context.TaggedCategory(tags=models.Tag.objects.all())
Expand Down