feat: add unique=False to vgroups filter

This commit is contained in:
relikd
2022-12-21 20:12:13 +01:00
parent 05b9fbf20a
commit b603fb9dd2
2 changed files with 15 additions and 3 deletions

View File

@@ -7,6 +7,9 @@ and this project does adhere to [Semantic Versioning](https://semver.org/spec/v2
## [Unreleased] ## [Unreleased]
### Added
- Support `unique=False` in `vgroups` filter to return a list of all entries (including duplicates)
### Fixed ### Fixed
- No duplicate `GroupBySource` entries in `vgroups` filter (while keeping sort order) - No duplicate `GroupBySource` entries in `vgroups` filter (while keeping sort order)

View File

@@ -1,5 +1,5 @@
from lektor.context import get_ctx from lektor.context import get_ctx
from typing import TYPE_CHECKING, Set, Dict, Union, Iterable, Iterator from typing import TYPE_CHECKING, Set, List, Dict, Union, Iterable, Iterator
import weakref import weakref
from .util import split_strip from .util import split_strip
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -50,6 +50,7 @@ class VGroups:
fields: Union[str, Iterable[str], None] = None, fields: Union[str, Iterable[str], None] = None,
flows: Union[str, Iterable[str], None] = None, flows: Union[str, Iterable[str], None] = None,
recursive: bool = True, recursive: bool = True,
unique: bool = True,
order_by: Union[str, Iterable[str], None] = None, order_by: Union[str, Iterable[str], None] = None,
) -> Iterator['GroupBySource']: ) -> Iterator['GroupBySource']:
''' Extract all referencing groupby virtual objects from a page. ''' ''' Extract all referencing groupby virtual objects from a page. '''
@@ -68,9 +69,12 @@ class VGroups:
GroupByRef.of(builder).make_once(keys) # ensure did cluster before use GroupByRef.of(builder).make_once(keys) # ensure did cluster before use
# find groups # find groups
proc_list = [record] proc_list = [record]
# Note: An ordered Set would be more approptiate but there is none. # Note: An ordered Set would be more approptiate but there is none.
# So lets use the insert order of dict (guaranteed since Python 3.7) # So lets use the insert order of dict (guaranteed since Python 3.7)
done_list = {} # type: Dict[GroupBySource, None] _only_uniques = {} # type: Dict[GroupBySource, None]
_w_duplicates = [] # type: List[GroupBySource]
while proc_list: while proc_list:
page = proc_list.pop(0) page = proc_list.pop(0)
if recursive and hasattr(page, 'children'): if recursive and hasattr(page, 'children'):
@@ -82,7 +86,12 @@ class VGroups:
continue continue
if keys and vobj().config.key not in keys: if keys and vobj().config.key not in keys:
continue continue
done_list[vobj()] = None # we only need the keys() if unique:
_only_uniques[vobj()] = None # we only need the keys()
else:
_w_duplicates.append(vobj())
done_list = _only_uniques if unique else _w_duplicates
# manage config dependencies # manage config dependencies
deps = set() # type: Set[str] deps = set() # type: Set[str]