from libres.context.registry import create_default_registry
from libres.db.models import ORMBase
from onegov.reservation.collection import ResourceCollection
from uuid import UUID
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from libres.context.core import Context
from libres.context.registry import Registry
from onegov.core.orm.session_manager import SessionManager
[docs]
class LibresIntegration:
""" Provides libres integration for
:class:`onegov.core.framework.Framework` based applications.
The application must be connected to a database
Usage::
from onegov.core import Framework
class MyApp(Framework, LibresIntegration):
pass
"""
if TYPE_CHECKING:
# necessary forward declaration
# provided by onegov.core.framework.Framework
[docs]
session_manager: SessionManager
@staticmethod
[docs]
def libres_context_from_session_manager(
registry: 'Registry',
session_manager: 'SessionManager'
) -> 'Context':
if registry.is_existing_context('onegov.reservation'):
return registry.get_context('onegov.reservation')
context = registry.register_context('onegov.reservation')
context.set_service('session_provider', lambda ctx: session_manager)
# onegov.reservation uses uuids for the resources, so we don't need to
# generate anything, we can just reuse the id (which is passed as the
# name)
def uuid_generator(name: UUID) -> UUID:
assert isinstance(name, UUID)
return name
context.set_service('uuid_generator', lambda ctx: uuid_generator)
return context
@property
[docs]
def libres_resources(self) -> ResourceCollection:
return ResourceCollection(self.libres_context)