Skip to content

Commit

Permalink
Merge pull request #7 from earthpyy/eliminate-dup
Browse files Browse the repository at this point in the history
Eliminate duplication on dropdown items
  • Loading branch information
earthpyy authored Nov 9, 2021
2 parents 464081f + a43f88c commit 4f057ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions dropdown/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def from_model(
count = len(result_list)

# results
return [
return utils.remove_duplication(
types.DropdownItem(
label=operator.attrgetter(label_field)(x) if label_field is not None else str(x),
value=operator.attrgetter(value_field)(x),
context={y: operator.attrgetter(y)(x) for y in (context_fields)},
) for x in result_list
], count
), count


def from_choices(choices: models.Choices) -> typing.Tuple[typing.List[types.DropdownItem], int]:
Expand All @@ -90,4 +90,4 @@ def from_choices(choices: models.Choices) -> typing.Tuple[typing.List[types.Drop
@param choices: choices to get dropdown
"""

return [types.DropdownItem(label=x.label, value=x.value) for x in sorted(choices, key=lambda x: x.label)]
return utils.remove_duplication(types.DropdownItem(label=x.label, value=x.value) for x in sorted(choices, key=lambda x: x.label))
6 changes: 6 additions & 0 deletions dropdown/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ def __init__(self, value: typing.Optional[typing.Any], label: str = None, contex
self.value = value
self.label = label or str(value or '')
self.context = context or {}

def __eq__(self, o: object) -> bool:
return self.value == o.value and self.label == o.label and self.context == self.context

def __hash__(self) -> int:
return hash((self.value, self.label, self.context))
8 changes: 8 additions & 0 deletions dropdown/utils.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import collections
import typing


def dot_to_relation(value: str) -> str:
return value.replace('.', '__')


def remove_duplication(value: typing.Iterable) -> list:
return list(collections.OrderedDict.fromkeys(value))

0 comments on commit 4f057ae

Please sign in to comment.