feat: add order_by to vgroups()

This commit is contained in:
relikd
2022-07-23 20:34:04 +02:00
parent f13bd3dfc6
commit a0b53c7566
2 changed files with 22 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
from lektor.context import get_ctx from lektor.context import get_ctx
from typing import TYPE_CHECKING, Union, Iterable, Iterator from typing import TYPE_CHECKING, Union, Iterable, Iterator, Optional
import weakref import weakref
if TYPE_CHECKING: if TYPE_CHECKING:
from lektor.builder import Builder from lektor.builder import Builder
@@ -47,7 +47,8 @@ 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 = False recursive: bool = False,
order_by: Optional[str] = None,
) -> Iterator['GroupBySource']: ) -> Iterator['GroupBySource']:
''' Extract all referencing groupby virtual objects from a page. ''' ''' Extract all referencing groupby virtual objects from a page. '''
ctx = get_ctx() ctx = get_ctx()
@@ -69,6 +70,7 @@ class VGroups:
flows = [flows] flows = [flows]
# find groups # find groups
proc_list = [record] proc_list = [record]
done_list = set()
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'):
@@ -80,4 +82,10 @@ class VGroups:
continue continue
if keys and vobj().config.key not in keys: if keys and vobj().config.key not in keys:
continue continue
yield vobj() done_list.add(vobj())
if order_by:
order = order_by.split(',')
yield from sorted(done_list, key=lambda x: x.get_sort_key(order))
else:
yield from done_list

View File

@@ -1,9 +1,10 @@
from lektor.build_programs import BuildProgram # subclass from lektor.build_programs import BuildProgram # subclass
from lektor.context import get_ctx from lektor.context import get_ctx
from lektor.db import _CmpHelper
from lektor.environment import Expression from lektor.environment import Expression
from lektor.sourceobj import VirtualSourceObject # subclass from lektor.sourceobj import VirtualSourceObject # subclass
from lektor.utils import build_url from lektor.utils import build_url
from typing import TYPE_CHECKING, Dict, List, Any, Optional, Iterator from typing import TYPE_CHECKING, Dict, List, Any, Optional, Iterator, Iterable
from .util import report_config_error, most_used_key from .util import report_config_error, most_used_key
if TYPE_CHECKING: if TYPE_CHECKING:
from lektor.builder import Artifact from lektor.builder import Artifact
@@ -93,6 +94,15 @@ class GroupBySource(VirtualSourceObject):
for record in self._children: for record in self._children:
yield from record.iter_source_filenames() yield from record.iter_source_filenames()
def get_sort_key(self, fields: Iterable[str]) -> List:
def cmp_val(field: str) -> Any:
reverse = field.startswith('-')
if reverse or field.startswith('+'):
field = field[1:]
return _CmpHelper(getattr(self, field, None), reverse)
return [cmp_val(field) for field in fields]
# ----------------------- # -----------------------
# Properties & Helper # Properties & Helper
# ----------------------- # -----------------------