Source code for ferrosoft.apps.emiflow.services.update
# Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
"""Emiflow hooks into the ferrobase update pipeline.
The ferrobase ``ferrobase_update`` management command iterates over
registered global and per-tenant updaters at deploy time. This module
registers an updater that ensures the four predefined
:class:`LifecycleCategory` rows exist in every database — both the
shared catalog database (global pass) and each tenant database (tenant
pass).
"""
from django.utils.translation import gettext as _
from ferrosoft.apps.emiflow.models import LifecycleCategory
from ferrosoft.apps.ferrobase.models import Tenant
from ferrosoft.apps.ferrobase.services.update import StatusWriter, Updater
[docs]
def create_lifecycle_categories(writer: StatusWriter, tenant: Tenant | None = None):
"""Upsert the predefined lifecycle categories in the appropriate database.
Args:
writer: Status writer used to report progress back to the
update pipeline.
tenant: When provided, the upsert is routed to that tenant's
database; when ``None``, the default (catalog) database is
used so the same function serves both the global and the
per-tenant pass.
"""
manager = (
LifecycleCategory.objects
if tenant is None
else LifecycleCategory.objects.db_manager(tenant.database_name)
)
manager.upsert_predefined()
writer.success(_("Upserted lifecycle categories"))
[docs]
def init():
"""Register :func:`create_lifecycle_categories` with the global and tenant updater registries.
Called from :meth:`EmiflowConfig.ready` so the updaters are wired
up at Django app initialisation.
"""
Updater.add_global_updater(create_lifecycle_categories)
Updater.add_tenant_updater(create_lifecycle_categories)