-
Notifications
You must be signed in to change notification settings - Fork 5
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
rf#197 Rm prepare_data from context #627
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
546df04
rf#197 Move prepare_data to context.ProductBrand
duker33 8d9c2af
rf#198 Customize product images context
duker33 e787029
rf#197 Grade refarm dep to 0.4.12
duker33 cd31e99
rf#197 Review and tests fixes
duker33 bff0caf
rf#197 Fix from tests
duker33 6e933c7
rf#197 Fix code style according to linter
duker33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import typing | ||
|
||
from catalog.context import AbstractProductsListContext | ||
from catalog.models import ProductQuerySet | ||
from images.models import Image | ||
|
||
|
||
class ProductImages(AbstractProductsListContext): | ||
|
||
@property | ||
def images(self) -> typing.Dict[int, Image]: | ||
assert isinstance(self.products, ProductQuerySet) | ||
|
||
images = {} | ||
if self.product_pages: | ||
images = Image.objects.get_main_images_by_pages( | ||
self.product_pages.filter(shopelectro_product__in=self.products) | ||
) | ||
|
||
return { | ||
product.id: images.get(product.page) | ||
for product in self.products | ||
} | ||
|
||
def get_context_data(self): | ||
return { | ||
'product_images': self.images, | ||
**( | ||
self.super.get_context_data() | ||
if self.super else {} | ||
), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||||||||||
from catalog.views import catalog | ||||||||||||
from pages import views as pages_views | ||||||||||||
|
||||||||||||
from shopelectro import context as se_context | ||||||||||||
from shopelectro import models | ||||||||||||
from shopelectro.views.helpers import set_csrf_cookie | ||||||||||||
|
||||||||||||
|
@@ -71,15 +72,12 @@ def get_context_data(self, **kwargs): | |||||||||||
'price_bounds': settings.PRICE_BOUNDS, | ||||||||||||
'group_tags_pairs': self.product.get_params(), | ||||||||||||
'product_images': self.get_images_context().get_context_data()['product_images'], | ||||||||||||
'tile_products': context.prepare_tile_products( | ||||||||||||
self.product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT), | ||||||||||||
models.ProductPage.objects.all() | ||||||||||||
), | ||||||||||||
'tile_products': self.product.get_siblings(offset=settings.PRODUCT_SIBLINGS_COUNT), | ||||||||||||
} | ||||||||||||
|
||||||||||||
def get_images_context(self): | ||||||||||||
return ( | ||||||||||||
context.ProductImages( | ||||||||||||
se_context.ProductImages( | ||||||||||||
url_kwargs={}, | ||||||||||||
request=self.request, | ||||||||||||
page=self.product.page, | ||||||||||||
|
@@ -103,11 +101,8 @@ def render_siblings_on_404( | |||||||||||
self.object = inactive_product | ||||||||||||
context_ = self.get_context_data( | ||||||||||||
object=inactive_product, | ||||||||||||
tile_products=context.prepare_tile_products( | ||||||||||||
inactive_product.get_siblings( | ||||||||||||
offset=settings.PRODUCT_SIBLINGS_COUNT | ||||||||||||
), | ||||||||||||
models.ProductPage.objects.all() | ||||||||||||
tile_products=inactive_product.get_siblings( | ||||||||||||
offset=settings.PRODUCT_SIBLINGS_COUNT | ||||||||||||
), | ||||||||||||
tile_title='Возможно вас заинтересуют похожие товары:', | ||||||||||||
**url_kwargs, | ||||||||||||
|
@@ -136,10 +131,7 @@ def get_context_data(self, **kwargs): | |||||||||||
.select_related('page') | ||||||||||||
) | ||||||||||||
if not mobile_view: | ||||||||||||
tile_products = context.prepare_tile_products( | ||||||||||||
top_products, | ||||||||||||
models.ProductPage.objects.all() | ||||||||||||
) | ||||||||||||
tile_products = top_products | ||||||||||||
|
||||||||||||
return { | ||||||||||||
**context_, | ||||||||||||
|
@@ -148,12 +140,12 @@ def get_context_data(self, **kwargs): | |||||||||||
'tile_products': tile_products, | ||||||||||||
'product_images': ( | ||||||||||||
self | ||||||||||||
.get_images_context(products=top_products) | ||||||||||||
.get_products_context(products=top_products) | ||||||||||||
.get_context_data()['product_images'] | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
def get_images_context(self, products=None, product_pages=None): | ||||||||||||
def get_products_context(self, products=None, product_pages=None): | ||||||||||||
return ( | ||||||||||||
context.ProductImages( | ||||||||||||
url_kwargs={}, # Ignore CPDBear | ||||||||||||
|
@@ -181,7 +173,8 @@ def get_context_data(self, **kwargs): | |||||||||||
| context.TaggedCategory(tags=models.Tag.objects.all()) | ||||||||||||
| context.SortingCategory() # requires TaggedCategory | ||||||||||||
| context.PaginationCategory() # requires SortingCategory | ||||||||||||
| context.ProductImages() | ||||||||||||
| context.ProductBrands() # requires TaggedCategory | ||||||||||||
| se_context.ProductImages() | ||||||||||||
| context.DBTemplate() # requires TaggedCategory | ||||||||||||
) | ||||||||||||
return { | ||||||||||||
|
@@ -254,19 +247,18 @@ def load_more(request, category_slug, offset=0, limit=0, sorting=0, tags=None): | |||||||||||
shopelectro_product__in=products | ||||||||||||
), | ||||||||||||
) | ||||||||||||
| context.ProductImages( | ||||||||||||
products=products, | ||||||||||||
product_pages=models.ProductPage.objects.filter( | ||||||||||||
shopelectro_product__in=products | ||||||||||||
) | ||||||||||||
) | ||||||||||||
| context.TaggedCategory(tags=models.Tag.objects.all()) | ||||||||||||
| context.SortingCategory() # requires TaggedCategory | ||||||||||||
| context.PaginationCategory() # requires SortingCategory | ||||||||||||
| context.ProductBrands() # requires TaggedCategory | ||||||||||||
| se_context.ProductImages() | ||||||||||||
| context.DBTemplate() # requires TaggedCategory | ||||||||||||
) | ||||||||||||
|
||||||||||||
return render(request, 'catalog/category_products.html', { | ||||||||||||
'products_data': context.prepare_tile_products( | ||||||||||||
products, models.ProductPage.objects.all() | ||||||||||||
), | ||||||||||||
'products': products, | ||||||||||||
'product_images': context_.get_context_data()['product_images'], | ||||||||||||
'product_brands': context_.get_context_data()['product_brands'], | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
'paginated': paginated, | ||||||||||||
'paginated_page': paginated_page, | ||||||||||||
'view_type': view, | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is better to be a method, because it perform heavily computations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artemiy312 , we already have
tags
,products
,product_pages
in the same property-style.Let's leave as is too keep it's style. And fidals/refarm-site#194 with it's subtasks will solve this problem too