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
|
|
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
from apps.library.models import LibraryItem
|
2025-08-21 21:28:14 +03:00
|
|
|
from apps.rsform.models import Attribution, Constituenta, CstType, RSFormCached
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2025-08-03 11:40:22 +03:00
|
|
|
from .OperationSchemaCached import CstSubstitution, OperationSchemaCached
|
2025-11-09 13:48:43 +03:00
|
|
|
from .PropagationContext import PropagationContext
|
2024-08-11 12:38:08 +03:00
|
|
|
|
|
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def _get_oss_hosts(schemaID: int) -> list[int]:
|
|
|
|
|
''' Get all hosts for schema. '''
|
|
|
|
|
return list(LibraryItem.objects.filter(operations__result_id=schemaID).distinct().values_list('pk', flat=True))
|
2024-08-11 12:38:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class PropagationFacade:
|
|
|
|
|
''' Change propagation API. '''
|
|
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self._context = PropagationContext()
|
|
|
|
|
self._oss: dict[int, OperationSchemaCached] = {}
|
|
|
|
|
|
|
|
|
|
def get_oss(self, schemaID: int) -> OperationSchemaCached:
|
|
|
|
|
''' Get OperationSchemaCached for schemaID. '''
|
|
|
|
|
if schemaID not in self._oss:
|
|
|
|
|
self._oss[schemaID] = OperationSchemaCached(schemaID, self._context)
|
|
|
|
|
return self._oss[schemaID]
|
|
|
|
|
|
|
|
|
|
def get_schema(self, schemaID: int) -> RSFormCached:
|
|
|
|
|
''' Get RSFormCached for schemaID. '''
|
|
|
|
|
return self._context.get_schema(schemaID)
|
|
|
|
|
|
|
|
|
|
def after_create_cst(self, new_cst: list[Constituenta],
|
2025-08-01 10:55:53 +03:00
|
|
|
exclude: Optional[list[int]] = None) -> None:
|
2025-04-16 11:19:29 +03:00
|
|
|
''' Trigger cascade resolutions when new constituenta is created. '''
|
2025-11-09 13:48:43 +03:00
|
|
|
if not new_cst:
|
|
|
|
|
return
|
|
|
|
|
source = new_cst[0].schema_id
|
|
|
|
|
hosts = _get_oss_hosts(source)
|
2024-08-11 12:38:08 +03:00
|
|
|
for host in hosts:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).after_create_cst(source, new_cst)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def after_change_cst_type(self, sourceID: int, target: int, new_type: CstType,
|
2025-08-03 15:47:00 +03:00
|
|
|
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:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).after_change_cst_type(sourceID, target, new_type)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
# pylint: disable=too-many-arguments, too-many-positional-arguments
|
2024-10-28 14:53:41 +03:00
|
|
|
def after_update_cst(
|
2025-11-09 13:48:43 +03:00
|
|
|
self, sourceID: int, target: int,
|
|
|
|
|
data: dict, old_data: dict,
|
2024-10-28 14:53:41 +03:00
|
|
|
exclude: Optional[list[int]] = None
|
|
|
|
|
) -> None:
|
2024-08-11 12:38:08 +03:00
|
|
|
''' Trigger cascade resolutions when constituenta data is changed. '''
|
2025-11-09 13:48:43 +03:00
|
|
|
hosts = _get_oss_hosts(sourceID)
|
2024-08-11 12:38:08 +03:00
|
|
|
for host in hosts:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).after_update_cst(sourceID, target, data, old_data)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def before_delete_cst(self, 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:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).before_delete_cst(sourceID, target)
|
2024-08-12 16:52:06 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def before_substitute(self, 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:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).before_substitute(sourceID, substitutions)
|
2024-08-15 23:23:45 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def before_delete_schema(self, target: int, exclude: Optional[list[int]] = None) -> None:
|
2024-08-15 23:23:45 +03:00
|
|
|
''' Trigger cascade resolutions before schema is deleted. '''
|
2025-11-09 13:48:43 +03:00
|
|
|
hosts = _get_oss_hosts(target)
|
2025-08-10 12:40:36 +03:00
|
|
|
if not hosts:
|
2024-08-15 23:23:45 +03:00
|
|
|
return
|
|
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
ids = list(Constituenta.objects.filter(schema_id=target).order_by('order').values_list('pk', flat=True))
|
2025-08-10 12:40:36 +03:00
|
|
|
for host in hosts:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).before_delete_cst(target, ids)
|
|
|
|
|
del self._oss[host]
|
2025-08-12 20:31:55 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def after_create_attribution(self, sourceID: int,
|
|
|
|
|
attributions: list[Attribution],
|
2025-08-12 20:31:55 +03:00
|
|
|
exclude: Optional[list[int]] = None) -> None:
|
2025-08-21 21:28:14 +03:00
|
|
|
''' Trigger cascade resolutions when Attribution is created. '''
|
2025-08-12 20:31:55 +03:00
|
|
|
hosts = _get_oss_hosts(sourceID)
|
|
|
|
|
for host in hosts:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).after_create_attribution(sourceID, attributions)
|
2025-08-12 20:31:55 +03:00
|
|
|
|
2025-11-09 13:48:43 +03:00
|
|
|
def before_delete_attribution(self, sourceID: int,
|
2025-11-07 00:09:42 +03:00
|
|
|
attributions: list[Attribution],
|
2025-08-12 20:31:55 +03:00
|
|
|
exclude: Optional[list[int]] = None) -> None:
|
2025-08-21 21:28:14 +03:00
|
|
|
''' Trigger cascade resolutions before Attribution is deleted. '''
|
2025-08-12 20:31:55 +03:00
|
|
|
hosts = _get_oss_hosts(sourceID)
|
|
|
|
|
for host in hosts:
|
2025-11-09 13:48:43 +03:00
|
|
|
if exclude is None or host not in exclude:
|
|
|
|
|
self.get_oss(host).before_delete_attribution(sourceID, attributions)
|