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