-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create catalog.context package * Create context package * Specify context ABS classes * Implement base product contexts classes * Implement base tag contexts classes * Resolve todo and create another one for ordering arg * Self-review fixes * Review fixes * Self-review fixes
- Loading branch information
1 parent
a799c38
commit fb2fa2a
Showing
6 changed files
with
176 additions
and
5 deletions.
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 @@ | ||
# @todo #183:120m Implement Page, PaginatedProducts, ProductBrands, ProductsImages context classes. |
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,55 @@ | ||
import abc | ||
import collections | ||
import typing | ||
|
||
from django.db.models import QuerySet | ||
|
||
|
||
class Context(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def context(self) -> typing.Dict[str, typing.Any]: | ||
... | ||
|
||
|
||
class ModelContext(abc.ABC): | ||
|
||
def __init__(self, qs: QuerySet): | ||
self._qs = qs | ||
|
||
def qs(self): | ||
return self._qs | ||
|
||
@abc.abstractmethod | ||
def context(self) -> typing.Dict[str, typing.Any]: | ||
... | ||
|
||
|
||
Context.register(ModelContext) | ||
|
||
|
||
class Contexts(Context): | ||
|
||
def __init__(self, *contexts: typing.List[Context]): | ||
self.contexts = contexts | ||
|
||
def context(self): | ||
return collections.ChainMap( | ||
*[ctx.context() for ctx in self.contexts] | ||
) | ||
|
||
|
||
class Tags(ModelContext): | ||
|
||
def context(self): | ||
return { | ||
'tags': self.qs(), | ||
} | ||
|
||
|
||
class Products(ModelContext): | ||
|
||
def context(self): | ||
return { | ||
'products': self.qs(), | ||
} |
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,57 @@ | ||
import typing | ||
|
||
from django.db.models import QuerySet | ||
|
||
from catalog.context.context import Products, Tags | ||
from catalog.models import AbstractCategory | ||
|
||
|
||
class ActiveProducts(Products): | ||
|
||
def __init__(self, products: Products): | ||
self._products = products | ||
|
||
def qs(self): | ||
return self._products.qs().active() | ||
|
||
|
||
class OrderedProducts(Products): | ||
|
||
def __init__(self, products: Products, req_kwargs): | ||
self._products = products | ||
self._sorting_index = self._req_kwargs.get('sorting', 0) | ||
|
||
def qs(self): | ||
return self._products.qs().order_by( | ||
SortingOption(index=self._sorting_index).directed_field, | ||
) | ||
|
||
def context(self): | ||
return { | ||
**super().context(), | ||
'sorting_index': self._sorting_index, | ||
} | ||
|
||
|
||
class ProductsByCategory(Products): | ||
|
||
def __init__(self, products: Products, category: AbstractCategory): | ||
self._products = products | ||
self._category = category | ||
|
||
def qs(self): | ||
return self._products.qs().get_category_descendants(self._category) | ||
|
||
|
||
class ProductsByTags(Products): | ||
|
||
def __init__(self, products: Products, tags_context: Tags): | ||
self._products = products | ||
self._tags = tags | ||
|
||
def qs(self): | ||
tags = self._tags.qs() | ||
if tags.exists(): | ||
return self._products.qs().tagged(tags) | ||
else: | ||
return self._products.qs() |
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,39 @@ | ||
from catalog.context.context import Context, Tags, Products | ||
|
||
from django.db.models import QuerySet | ||
|
||
|
||
class GroupedTags(Context): | ||
|
||
def __init__(self, tags: Tags): | ||
self._tags = tags | ||
|
||
def context(self): | ||
return { | ||
'group_tags_pairs': self._tags.qs().get_group_tags_pairs(), | ||
} | ||
|
||
|
||
class ParsedTags(Tags): | ||
|
||
def __init__(self, tags: Tags, req_kwargs): | ||
self._tags = tags | ||
self._raw_tags = req_kwargs.get('tags') | ||
|
||
def qs(self): | ||
tags = self._tags.qs() | ||
if not self._raw_tags: | ||
tags.none() | ||
return tags.parsed(self._raw_tags) | ||
|
||
|
||
class Checked404Tags(Tags): | ||
|
||
def __init__(self, tags: Tags): | ||
self._tags = tags | ||
|
||
def qs(self): | ||
tags = self._tags.qs() | ||
if not tags.exists(): | ||
raise http.Http404('No such tag.') | ||
return tags |
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
fb2fa2a
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.
Puzzle
550-035b850e
disappeared fromcatalog/context.py
, that's why I closed #186. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.fb2fa2a
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.
Puzzle
183-d6341b3e
discovered incatalog/context/__init__.py
and submitted as #207. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.fb2fa2a
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.
Puzzle
183-a9ba47b7
discovered incatalog/models.py
and submitted as #208. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.