Skip to content

Commit

Permalink
rf#168 (#178)
Browse files Browse the repository at this point in the history
Move TagQS.as_string to refarm
Add ProductQS.active() method
  • Loading branch information
duker33 authored Sep 12, 2018
1 parent ef0210e commit 0b37cbe
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def filter_by_categories(self, categories: Iterable[AbstractCategory]):
.filter(category__in=categories, price__gt=0)
)

def active(self):
return self.filter(page__is_active=True)


class ProductManager(models.Manager.from_queryset(ProductQuerySet)):
"""Get all products of given category by Category's id or instance."""
Expand All @@ -126,15 +129,15 @@ def get_category_descendants(self, category: models.Model, ordering: [str]=None)
# @todo #164:15m Rm ProductManager.get_active() method
# Use ProductActiveManager instead
def get_active(self):
return self.get_queryset().filter(page__is_active=True)
return self.get_queryset().active()


class ProductActiveManager(ProductManager):
def get_queryset(self):
return (
super(ProductActiveManager, self)
.get_queryset()
.filter(page__is_active=True)
.active()
)


Expand Down Expand Up @@ -217,7 +220,7 @@ def __str__(self):

class TagQuerySet(models.QuerySet):

def filter_by_products(self, products: List[AbstractProduct]):
def filter_by_products(self, products: Iterable[AbstractProduct]):
ordering = settings.TAGS_ORDER
distinct = [order.lstrip('-') for order in ordering]

Expand Down Expand Up @@ -248,6 +251,45 @@ def get_brands(self, products: List[AbstractProduct]) -> Dict[AbstractProduct, '
if product in brand.products.all()
}

def as_string( # Ignore PyDocStyleBear
self,
field_name: str,
type_delimiter: str,
group_delimiter: str,
) -> str:
"""
:param field_name: Only field's value is used to represent tag as string.
:param type_delimiter:
:param group_delimiter:
:return:
"""
if not self:
return ''

group_tags_map = self.get_group_tags_pairs()

_, tags_by_group = zip(*group_tags_map)

return group_delimiter.join(
type_delimiter.join(getattr(tag, field_name) for tag in tags_list)
for tags_list in tags_by_group
)

def as_title(self) -> str:
return self.as_string(
field_name='name',
type_delimiter=settings.TAGS_TITLE_DELIMITER,
group_delimiter=settings.TAG_GROUPS_TITLE_DELIMITER
)

def as_url(self) -> str:
return self.as_string(
field_name='slug',
type_delimiter=settings.TAGS_URL_DELIMITER,
group_delimiter=settings.TAG_GROUPS_URL_DELIMITER
)



class TagManager(models.Manager.from_queryset(TagQuerySet)):

Expand Down Expand Up @@ -304,6 +346,8 @@ def save(self, *args, **kwargs):
self.slug = randomize_slug(self.slug)
super(Tag, self).save(*args, **kwargs)

# @todo #168:15m Move `Tags.parse_url_tags` Tags context.
# Depends on se#567
@staticmethod
def parse_url_tags(tags: str) -> list:
groups = tags.split(settings.TAGS_URL_DELIMITER)
Expand Down

1 comment on commit 0b37cbe

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 0b37cbe Sep 12, 2018

Choose a reason for hiding this comment

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

Puzzle 168-f08f9547 discovered in catalog/models.py and submitted as #179. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.