flask_more_smorest.protocols
Protocol definitions for Flask-More-Smorest.
These protocols define the interfaces that components expect, allowing for loose coupling between modules and better type safety.
Protocols provide structural typing (duck typing with type checking), enabling components to work with any object that has the required attributes and methods, without requiring inheritance.
Example
>>> from flask_more_smorest.protocols import Identifiable, PermissionAware
>>>
>>> def process_item(item: Identifiable & PermissionAware) -> None:
... if item.can_read():
... print(f"Processing item {item.id}")
Classes
|
Full CRUD model interface. |
|
Object with an ID attribute. |
|
Object with permission checking methods. |
|
Object that can be persisted to a database. |
|
Object with automatic timestamp tracking. |
|
Object that can be updated with new field values. |
|
User-like object for permission checking. |
- class flask_more_smorest.protocols.Identifiable(*args, **kwargs)[source]
Object with an ID attribute.
Any object that has an id attribute can satisfy this protocol, making it useful for generic functions that work with identified resources.
Example
>>> def get_resource_id(resource: Identifiable) -> UUID | Any: ... return resource.id
- __init__(*args, **kwargs)
- class flask_more_smorest.protocols.PermissionAware(*args, **kwargs)[source]
Object with permission checking methods.
Defines the interface for objects that support permission-based access control. Any object implementing these methods can be used in permission-aware contexts.
Example
>>> def safe_update(obj: PermissionAware, **kwargs: Any) -> None: ... if obj.can_write(): ... obj.update(**kwargs)
- __init__(*args, **kwargs)
- class flask_more_smorest.protocols.Saveable(*args, **kwargs)[source]
Object that can be persisted to a database.
Defines the interface for objects with CRUD operations.
Example
>>> def persist(obj: Saveable) -> None: ... obj.save(commit=True)
- save(commit=True)[source]
Save the object to the database.
- Parameters:
commit (
bool) – Whether to commit the transaction immediately- Return type:
Self- Returns:
The saved object (for method chaining)
- __init__(*args, **kwargs)
- class flask_more_smorest.protocols.Updatable(*args, **kwargs)[source]
Object that can be updated with new field values.
Example
>>> def apply_changes(obj: Updatable, changes: dict[str, Any]) -> None: ... obj.update(**changes)
- __init__(*args, **kwargs)
- class flask_more_smorest.protocols.CRUDModel(*args, **kwargs)[source]
Full CRUD model interface.
Combines all the basic protocols to define a complete model interface with identification, permissions, and persistence operations.
This is the most complete protocol and represents what most model classes in Flask-More-Smorest should implement.
Example
>>> def process_model(model: CRUDModel) -> None: ... if model.can_write(): ... model.update(status='processed') ... model.save()
- classmethod get_by_or_404(**kwargs)[source]
Get instance by field values or raise 404.
- Parameters:
**kwargs (
Any) – Field name/value pairs to filter by- Return type:
Self- Returns:
Instance if found
- Raises:
NotFoundError – If no instance is found
- class flask_more_smorest.protocols.UserLike(*args, **kwargs)[source]
User-like object for permission checking.
This protocol defines the minimal interface that permission checking code expects from user objects. Any user class (built-in or custom) that implements these methods can be used with the permissions system.
Example
>>> def check_access(user: UserLike, resource: str) -> bool: ... return user.has_role('admin') or user.has_role('superadmin')
- __init__(*args, **kwargs)