core.collection
Attributes
Classes
Abstract base class for generic types. |
|
Requires a self.locale and self.term |
|
Provides collections with pagination, if they implement a few |
|
Provides a pagination that supports loading multiple pages at once. |
Module Contents
- class core.collection.GenericCollection(session: sqlalchemy.orm.Session, **kwargs: Any)[source]
Bases:
Generic
[_M
]Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as:
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows:
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default
- class core.collection.SearcheableCollection(session: sqlalchemy.orm.Session, **kwargs: Any)[source]
Bases:
GenericCollection
[_M
]Requires a self.locale and self.term
- static match_term(column: Column[str] | Column[str | None], language: str, term: str) sqlalchemy.sql.elements.ClauseElement [source]
Usage: model.filter(match_term(model.col, ‘german’, ‘my search term’))
- static term_to_tsquery_string(term: str) str [source]
Returns the current search term transformed to use within Postgres
to_tsquery
function. Removes all unwanted characters, replaces prefix matching, joins word together using FOLLOWED BY.
- filter_text_by_locale(column: Column[str] | Column[str | None], term: str, locale: str | None = None) sqlalchemy.sql.elements.ClauseElement [source]
Returns an SqlAlchemy filter statement based on the search term. If no locale is provided, it will use english as language.
to_tsquery
creates a tsquery value from term, which must consist of single tokens separated by the Boolean operators & (AND), | (OR) and ! (NOT).to_tsvector
parses a textual document into tokens, reduces the tokens to lexemes, and returns a tsvector which lists the lexemes together with their positions in the document. The document is processed according to the specified or default text search configuration.
- class core.collection.Pagination(page: int = 0)[source]
Bases:
Generic
[_M
]Provides collections with pagination, if they implement a few documented properties and methods.
See
onegov.ticket.TicketCollection
for an example.- abstract __eq__(other: object) bool [source]
Returns True if the current and the other Pagination instance are equal. Used to find the current page in a list of pages.
- abstract subset() Query[_M] [source]
Returns an SQLAlchemy query containing all records that should be considered for pagination.
- abstract page_by_index(index: int) Self [source]
Returns the page at the given index. A page here means an instance of the class inheriting from the
Pagination
base class.
- transform_batch_query(query: Query[_M]) Query[_M] [source]
Allows subclasses to transform the given query before it is used to retrieve the batch. This is a good place to add additional loading that should only apply to the batch (say joining other values to the batch which are then not loaded by the whole query).
- property subset_count: int[source]
Returns the total number of elements this pagination represents.
- class core.collection.RangedPagination[source]
Bases:
Generic
[_M
]Provides a pagination that supports loading multiple pages at once.
This is useful in a context where a single button is used to ‘load more’ results one by one. In this case we need an URL that represents what’s happening on the screen (multiple pages are shown at the same time).
- abstract subset() Query[_M] [source]
Returns an SQLAlchemy query containing all records that should be considered for pagination.
- property page_range: tuple[int, int][source]
- Abstractmethod:
Returns the current page range (starting at (0, 0)).
- abstract by_page_range(page_range: tuple[int, int]) Self [source]
Returns an instance of the collection limited to the given page range.
- limit_range(page_range: Sequence[int] | None, direction: Literal['up', 'down']) tuple[int, int] [source]
Limits the range to the range limit in the given direction.
For example, 0-99 will be limited to 89-99 with a limit of 10 and ‘up’. With ‘down’ it will be limited to 0-9.
- transform_batch_query(query: Query[_M]) Query[_M] [source]
Allows subclasses to transform the given query before it is used to retrieve the batch. This is a good place to add additional loading that should only apply to the batch (say joining other values to the batch which are then not loaded by the whole query).