ConceptPortal-public/rsconcept/backend/apps/oss/models/PropagationFacade.py

102 lines
4.4 KiB
Python
Raw Normal View History

2024-08-11 12:38:08 +03:00
''' Models: Change propagation facade - managing all changes in OSS. '''
2024-10-28 14:53:41 +03:00
from typing import Optional
from apps.library.models import LibraryItem, LibraryItemType
2025-08-12 20:31:55 +03:00
from apps.rsform.models import Association, Constituenta, CstType, RSFormCached
2024-08-11 12:38:08 +03:00
2025-08-03 11:40:22 +03:00
from .OperationSchemaCached import CstSubstitution, OperationSchemaCached
2024-08-11 12:38:08 +03:00
2025-08-03 11:40:22 +03:00
def _get_oss_hosts(schemaID: int) -> list[LibraryItem]:
2024-08-11 12:38:08 +03:00
''' Get all hosts for LibraryItem. '''
2025-08-03 11:40:22 +03:00
return list(LibraryItem.objects.filter(operations__result_id=schemaID).only('pk').distinct())
2024-08-11 12:38:08 +03:00
class PropagationFacade:
''' Change propagation API. '''
@staticmethod
2025-08-01 10:55:53 +03:00
def after_create_cst(source: RSFormCached, new_cst: list[Constituenta],
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions when new constituenta is created. '''
2025-08-03 11:40:22 +03:00
hosts = _get_oss_hosts(source.model.pk)
2024-08-11 12:38:08 +03:00
for host in hosts:
2024-10-28 14:53:41 +03:00
if exclude is None or host.pk not in exclude:
2025-08-03 11:40:22 +03:00
OperationSchemaCached(host).after_create_cst(source, new_cst)
2024-08-11 12:38:08 +03:00
@staticmethod
2025-08-03 15:47:00 +03:00
def after_change_cst_type(sourceID: int, target: int, new_type: CstType,
exclude: Optional[list[int]] = None) -> None:
2024-08-11 12:38:08 +03:00
''' Trigger cascade resolutions when constituenta type is changed. '''
2025-08-03 15:47:00 +03:00
hosts = _get_oss_hosts(sourceID)
2024-08-11 12:38:08 +03:00
for host in hosts:
2024-10-28 14:53:41 +03:00
if exclude is None or host.pk not in exclude:
2025-08-03 15:47:00 +03:00
OperationSchemaCached(host).after_change_cst_type(sourceID, target, new_type)
2024-08-11 12:38:08 +03:00
@staticmethod
2024-10-28 14:53:41 +03:00
def after_update_cst(
2025-08-01 10:55:53 +03:00
source: RSFormCached,
2025-08-03 15:47:00 +03:00
target: int,
2024-10-28 14:53:41 +03:00
data: dict,
old_data: dict,
exclude: Optional[list[int]] = None
) -> None:
2024-08-11 12:38:08 +03:00
''' Trigger cascade resolutions when constituenta data is changed. '''
2025-08-03 11:40:22 +03:00
hosts = _get_oss_hosts(source.model.pk)
2024-08-11 12:38:08 +03:00
for host in hosts:
2024-10-28 14:53:41 +03:00
if exclude is None or host.pk not in exclude:
2025-08-03 11:40:22 +03:00
OperationSchemaCached(host).after_update_cst(source, target, data, old_data)
2024-08-11 12:38:08 +03:00
@staticmethod
2025-08-03 15:47:00 +03:00
def before_delete_cst(sourceID: int, target: list[int],
2025-08-01 10:55:53 +03:00
exclude: Optional[list[int]] = None) -> None:
2024-08-11 12:38:08 +03:00
''' Trigger cascade resolutions before constituents are deleted. '''
2025-08-03 15:47:00 +03:00
hosts = _get_oss_hosts(sourceID)
2024-08-11 12:38:08 +03:00
for host in hosts:
2024-10-28 14:53:41 +03:00
if exclude is None or host.pk not in exclude:
2025-08-03 15:47:00 +03:00
OperationSchemaCached(host).before_delete_cst(sourceID, target)
2024-08-12 16:52:06 +03:00
@staticmethod
2025-08-03 11:40:22 +03:00
def before_substitute(sourceID: int, substitutions: CstSubstitution,
2025-08-01 10:55:53 +03:00
exclude: Optional[list[int]] = None) -> None:
2024-08-12 16:52:06 +03:00
''' Trigger cascade resolutions before constituents are substituted. '''
2025-08-10 12:40:36 +03:00
if not substitutions:
2025-08-03 11:40:22 +03:00
return
hosts = _get_oss_hosts(sourceID)
2024-08-12 16:52:06 +03:00
for host in hosts:
2024-10-28 14:53:41 +03:00
if exclude is None or host.pk not in exclude:
2025-08-03 11:40:22 +03:00
OperationSchemaCached(host).before_substitute(sourceID, substitutions)
@staticmethod
2024-10-28 14:53:41 +03:00
def before_delete_schema(item: LibraryItem, exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions before schema is deleted. '''
if item.item_type != LibraryItemType.RSFORM:
return
2025-08-03 11:40:22 +03:00
hosts = _get_oss_hosts(item.pk)
2025-08-10 12:40:36 +03:00
if not hosts:
return
2025-08-03 15:47:00 +03:00
ids = list(Constituenta.objects.filter(schema=item).order_by('order').values_list('pk', flat=True))
2025-08-10 12:40:36 +03:00
for host in hosts:
if exclude is None or host.pk not in exclude:
OperationSchemaCached(host).before_delete_cst(item.pk, ids)
2025-08-12 20:31:55 +03:00
@staticmethod
def after_create_association(sourceID: int, associations: list[Association],
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions when association is created. '''
hosts = _get_oss_hosts(sourceID)
for host in hosts:
if exclude is None or host.pk not in exclude:
OperationSchemaCached(host).after_create_association(sourceID, associations)
@staticmethod
def before_delete_association(sourceID: int,
associations: list[Association],
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions before association is deleted. '''
hosts = _get_oss_hosts(sourceID)
for host in hosts:
if exclude is None or host.pk not in exclude:
OperationSchemaCached(host).before_delete_association(sourceID, associations)