fix: dont use query for children total count
This commit is contained in:
@@ -65,7 +65,6 @@ class VGroups:
|
|||||||
if not ctx:
|
if not ctx:
|
||||||
raise NotImplementedError("Shouldn't happen, where is my context?")
|
raise NotImplementedError("Shouldn't happen, where is my context?")
|
||||||
builder = ctx.build_state.builder
|
builder = ctx.build_state.builder
|
||||||
# TODO: fix record_dependency -> process in non-capturing context
|
|
||||||
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]
|
||||||
|
|||||||
@@ -1,20 +1,29 @@
|
|||||||
from lektor import datamodel
|
from lektor import datamodel
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, Dict
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from lektor.environment import Environment
|
||||||
from lektor.pagination import Pagination
|
from lektor.pagination import Pagination
|
||||||
from lektor.sourceobj import SourceObject
|
from lektor.sourceobj import SourceObject
|
||||||
|
|
||||||
|
|
||||||
class PaginationConfig(datamodel.PaginationConfig):
|
class PaginationConfig(datamodel.PaginationConfig):
|
||||||
# because original method does not work for virtual sources.
|
# because original method does not work for virtual sources.
|
||||||
|
def __init__(self, env: 'Environment', config: Dict[str, Any], total: int):
|
||||||
|
super().__init__(env, **config)
|
||||||
|
self._total_items_count = total
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_record_for_page(source: 'SourceObject', page_num: int) -> Any:
|
def get_record_for_page(record: 'SourceObject', page_num: int) -> Any:
|
||||||
for_page = getattr(source, '__for_page__', None)
|
for_page = getattr(record, '__for_page__', None)
|
||||||
if callable(for_page):
|
if callable(for_page):
|
||||||
return for_page(page_num)
|
return for_page(page_num)
|
||||||
return datamodel.PaginationConfig.get_record_for_page(source, page_num)
|
return datamodel.PaginationConfig.get_record_for_page(record, page_num)
|
||||||
|
|
||||||
|
def count_total_items(self, record: 'SourceObject') -> int:
|
||||||
|
''' Override super() to prevent a record.children query. '''
|
||||||
|
return self._total_items_count
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
def get_pagination_controller(self, source: 'SourceObject') \
|
def get_pagination_controller(self, record: 'SourceObject') \
|
||||||
-> 'Pagination':
|
-> 'Pagination':
|
||||||
...
|
...
|
||||||
|
|||||||
@@ -58,6 +58,11 @@ class FixedRecordsQuery(Query):
|
|||||||
return len(self.__child_paths)
|
return len(self.__child_paths)
|
||||||
return super().count() # type: ignore[no-any-return]
|
return super().count() # type: ignore[no-any-return]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def total(self) -> int:
|
||||||
|
''' Return total entries count (without any filter). '''
|
||||||
|
return len(self.__child_paths)
|
||||||
|
|
||||||
def get(self, path: str, page_num: Optional[int] = None) \
|
def get(self, path: str, page_num: Optional[int] = None) \
|
||||||
-> Optional['Record']:
|
-> Optional['Record']:
|
||||||
''' Return Record with given path '''
|
''' Return Record with given path '''
|
||||||
|
|||||||
@@ -100,7 +100,8 @@ class GroupBySource(VirtualSourceObject):
|
|||||||
@cached_property
|
@cached_property
|
||||||
def _pagination_config(self) -> 'PaginationConfig':
|
def _pagination_config(self) -> 'PaginationConfig':
|
||||||
# Generate `PaginationConfig` once we need it
|
# Generate `PaginationConfig` once we need it
|
||||||
return PaginationConfig(self.record.pad.env, **self.config.pagination)
|
return PaginationConfig(self.record.pad.env, self.config.pagination,
|
||||||
|
self._query.total)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def pagination(self) -> 'Pagination':
|
def pagination(self) -> 'Pagination':
|
||||||
@@ -185,10 +186,10 @@ class GroupBySource(VirtualSourceObject):
|
|||||||
# Properties & Helper
|
# Properties & Helper
|
||||||
# -----------------------
|
# -----------------------
|
||||||
|
|
||||||
@cached_property
|
@property
|
||||||
def children(self) -> FixedRecordsQuery:
|
def children(self) -> FixedRecordsQuery:
|
||||||
''' Return query of children of type Record. '''
|
''' Return query of children of type Record. '''
|
||||||
return self._query.request_page(self.page_num)
|
return self._query
|
||||||
|
|
||||||
def __getitem__(self, key: str) -> Any:
|
def __getitem__(self, key: str) -> Any:
|
||||||
# Used for virtual path resolver
|
# Used for virtual path resolver
|
||||||
|
|||||||
Reference in New Issue
Block a user