refactor: init GroupBySource with Config
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
from lektor.db import Page # isinstance
|
||||
from typing import (
|
||||
TYPE_CHECKING, NamedTuple, Dict, List, Set, Any, Optional, Iterable
|
||||
)
|
||||
from typing import TYPE_CHECKING, NamedTuple, Dict, List, Set, Any, Optional
|
||||
from .util import build_url
|
||||
from .vobj import VPATH, GroupBySource
|
||||
if TYPE_CHECKING:
|
||||
@@ -74,7 +72,7 @@ class Resolver:
|
||||
rv = subset.get(url)
|
||||
if rv:
|
||||
return GroupBySource(
|
||||
node, rv.key, rv.page).finalize(rv.config, rv.key_obj)
|
||||
node, rv.key, rv.config, rv.page).finalize(rv.key_obj)
|
||||
return None
|
||||
|
||||
def resolve_virtual_path(self, node: 'SourceObject', pieces: List[str]) \
|
||||
@@ -93,5 +91,5 @@ class Resolver:
|
||||
for rv in self._data.get(conf_key, {}).values():
|
||||
if rv.equals(path, conf_key, vobj_key, page):
|
||||
return GroupBySource(
|
||||
node, rv.key, rv.page).finalize(rv.config, rv.key_obj)
|
||||
node, rv.key, rv.config, rv.page).finalize(rv.key_obj)
|
||||
return None
|
||||
|
||||
@@ -34,6 +34,7 @@ class GroupBySource(VirtualSourceObject):
|
||||
self,
|
||||
record: 'Record',
|
||||
key: str,
|
||||
config: 'Config',
|
||||
page_num: Optional[int] = None
|
||||
) -> None:
|
||||
super().__init__(record)
|
||||
@@ -41,6 +42,7 @@ class GroupBySource(VirtualSourceObject):
|
||||
self.__key_obj_map = [] # type: List[Any]
|
||||
self._expr_fields = {} # type: Dict[str, Expression]
|
||||
self.key = key
|
||||
self.config = config
|
||||
self.page_num = page_num
|
||||
|
||||
def append_child(self, child: 'Record', key_obj: Any) -> None:
|
||||
@@ -68,28 +70,31 @@ class GroupBySource(VirtualSourceObject):
|
||||
# Evaluate Extra Fields
|
||||
# -------------------------
|
||||
|
||||
def finalize(self, config: 'Config', key_obj: Optional[Any] = None) \
|
||||
def finalize(self, key_obj: Optional[Any] = None) \
|
||||
-> 'GroupBySource':
|
||||
self.config = config
|
||||
# make a sorted children query
|
||||
self._query = FixedRecordsQuery(self.pad, self.__children, self.alt)
|
||||
self._query._order_by = config.order_by
|
||||
self._query._order_by = self.config.order_by
|
||||
del self.__children
|
||||
# set indexed original value (can be: str, int, float, bool, obj)
|
||||
self.key_obj = key_obj or most_used_key(self.__key_obj_map)
|
||||
del self.__key_obj_map
|
||||
# evaluate slug Expression
|
||||
self.slug = config.eval_slug(self.key, on=self)
|
||||
if self.slug and self.slug.endswith('/index.html'):
|
||||
self.slug = self.slug[:-10]
|
||||
|
||||
if key_obj: # exit early if initialized through resolver
|
||||
return self
|
||||
# extra fields
|
||||
for attr in config.fields:
|
||||
self._update_attr(attr, config.eval_field(attr, on=self))
|
||||
for attr in self.config.fields:
|
||||
self._update_attr(attr, self.config.eval_field(attr, on=self))
|
||||
return self
|
||||
|
||||
@cached_property
|
||||
def slug(self) -> Optional[str]:
|
||||
# evaluate slug Expression once we need it
|
||||
slug = self.config.eval_slug(self.key, on=self)
|
||||
if slug and slug.endswith('/index.html'):
|
||||
slug = slug[:-10]
|
||||
return slug
|
||||
|
||||
# -----------------------
|
||||
# Pagination handling
|
||||
# -----------------------
|
||||
|
||||
@@ -117,12 +117,12 @@ class Watcher:
|
||||
# update internal object storage
|
||||
alt = args.record.alt
|
||||
if slug not in self._state[alt]:
|
||||
src = GroupBySource(self._root_record[alt], slug)
|
||||
src = GroupBySource(self._root_record[alt], slug, self.config)
|
||||
self._state[alt][slug] = src
|
||||
else:
|
||||
src = self._state[alt][slug]
|
||||
|
||||
src.append_child(args.record, obj) # obj is used as "group" string
|
||||
src.append_child(args.record, obj)
|
||||
# reverse reference
|
||||
VGroups.of(args.record).add(args.key, src)
|
||||
return slug
|
||||
@@ -137,7 +137,7 @@ class Watcher:
|
||||
del self._rmmbr
|
||||
for vobj_list in self._state.values():
|
||||
for vobj in vobj_list.values():
|
||||
yield vobj.finalize(self.config)
|
||||
yield vobj.finalize()
|
||||
# cleanup. remove this code if you'd like to iter twice
|
||||
del self._model_reader
|
||||
del self._root_record
|
||||
|
||||
Reference in New Issue
Block a user