Skip to content

Commit

Permalink
stb#374 Let group_tag_pairs return sorted tags list (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
duker33 authored Dec 17, 2018
1 parent fd4a246 commit 5f8deb7
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,27 @@ def filter_by_products(self, products: Iterable[AbstractProduct]):
)

def get_group_tags_pairs(self) -> List[Tuple[TagGroup, List['Tag']]]:
"""
Return set of group_tag pairs with specific properties.
Every pair contains tag group and sorted tag list.
It's sorted alphabetically or numerically.
Sort method depends of tag value type.
"""
# @todo #STB374:120m Move tag's value to separated field.
# Now we have fields like `tag.name == '10 м'`.
# But should have smth like this:
# `tag.value, tag.group.measure, tag.label == 10, 'м', '10 м'`.
# Right now we should do dirty hacks for tags comparing mech.
def int_or_str(value: str):
try:
return int(value.split(' ')[0])
except ValueError:
return value

grouped_tags = groupby(self.prefetch_related('group'), key=attrgetter('group'))
return [
(group, list(tags_))
(group, sorted(list(tags_), key=lambda t: int_or_str(t.name)))
for group, tags_ in grouped_tags
]

Expand Down

1 comment on commit 5f8deb7

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 5f8deb7 Dec 17, 2018

Choose a reason for hiding this comment

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

Puzzle STB374-01960267 discovered in catalog/models.py and submitted as #224. 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.