qunicorn_core.db.models.db_model module

class qunicorn_core.db.models.db_model.DbModel

Bases: object

Dataclass for database model to create a table name and the id column

classmethod apply_authentication_filter(query: Select, user_id: str | None) Select
classmethod apply_ordering(query: Select) Select
delete(commit: bool = False)

Delete the current object and optionally commit all pending changes.

classmethod delete_by_id(id_: int, commit: bool = False)

Delete a single database object by its id attribute and optionally commit all pending changes.

classmethod get_all(where: Sequence[ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement] | None = None, limit: int | None = None, offset: int | None = None)

Get all database objects of this class.

Objects can be filtered by providing a sequence of sql expressions as where clause. All expressions must evaluate to True for an item to be included (i.e., clauses are joined by an AND).

Limit and offset can be used for pagination.

classmethod get_all_authenticated(user_id: str | None, where: Sequence[ColumnElement[_T] | _HasClauseElement[bool] | SQLCoreOperations[_T] | ExpressionElementRole[bool] | TypedColumnsClauseRole[bool] | Callable[[], ColumnElement[_T]] | LambdaElement] | None = None, limit: int | None = None, offset: int | None = None)

Get all database objects of this class that the given user is allowed to access.

Uses the apply_authentication_filter method to filter out objects.

Additionally, objects can be filtered by providing a sequence of sql expressions as where clause. All expressions must evaluate to True for an item to be included (i.e., clauses are joined by an AND).

Limit and offset can be used for pagination.

classmethod get_by_id(id_: int)

Get a single database object by its id attribute.

classmethod get_by_id_authenticated(id_: int, user_id: str | None)

Get a single database object by its id attribute and return None if not found or the user has insufficient rights.

classmethod get_by_id_authenticated_or_404(id_: int, user_id: str | None)

Get a single database object by its id attribute and raise 404 error if not found or the user has insufficient rights.

classmethod get_by_id_or_404(id_: int)

Get a single database object by its id attribute and raise 404 error if not found.

classmethod get_by_id_query(id_: int)

Get a select query for a single database object by its id attribute.

classmethod not_found_message(id_: int) str
save(commit: bool = False)

Add the current object to the database and optionally commit all pending changes.

This does not automatically create the database ids (except when commit is True).