Portal/rsconcept/backend/apps/oss/models/PropagationFacade.py

112 lines
4.9 KiB
Python
Raw Normal View History

2024-08-11 00:10:27 +03:00
''' Models: Change propagation facade - managing all changes in OSS. '''
2024-10-28 14:52:30 +03:00
from typing import Optional
2025-11-08 19:32:42 +03:00
from apps.library.models import LibraryItem
from apps.rsform.models import Attribution, Constituenta, CstType, RSFormCached
2024-08-11 00:10:27 +03:00
2025-08-01 18:31:23 +03:00
from .OperationSchemaCached import CstSubstitution, OperationSchemaCached
2025-11-08 21:44:25 +03:00
from .PropagationContext import PropagationContext
2024-08-11 00:10:27 +03:00
2025-11-08 19:32:42 +03:00
def _get_oss_hosts(schemaID: int) -> list[int]:
''' Get all hosts for schema. '''
return list(LibraryItem.objects.filter(operations__result_id=schemaID).values_list('pk', flat=True))
2024-08-11 00:10:27 +03:00
class PropagationFacade:
''' Change propagation API. '''
2025-11-08 21:44:25 +03:00
def __init__(self) -> None:
self._context = PropagationContext()
self._oss: dict[int, OperationSchemaCached] = {}
2025-11-08 19:32:42 +03:00
2025-11-08 21:44:25 +03:00
def get_oss(self, schemaID: int) -> OperationSchemaCached:
2025-11-08 19:32:42 +03:00
''' Get OperationSchemaCached for schemaID. '''
2025-11-08 21:44:25 +03:00
if schemaID not in self._oss:
self._oss[schemaID] = OperationSchemaCached(schemaID, self._context)
return self._oss[schemaID]
2025-11-08 19:32:42 +03:00
2025-11-08 21:44:25 +03:00
def get_schema(self, schemaID: int) -> RSFormCached:
''' Get RSFormCached for schemaID. '''
return self._context.get_schema(schemaID)
def reset_cache(self) -> None:
2025-11-08 19:32:42 +03:00
''' Reset cache. '''
2025-11-08 21:44:25 +03:00
self._oss = {}
2025-11-08 19:32:42 +03:00
2025-11-08 21:44:25 +03:00
def after_create_cst(self, source: RSFormCached, new_cst: list[Constituenta],
2025-08-01 10:43:51 +03:00
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions when new constituenta is created. '''
2025-11-08 19:32:42 +03:00
hosts = _get_oss_hosts(source.pk)
2024-08-11 00:10:27 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).after_create_cst(source, new_cst)
2024-08-11 00:10:27 +03:00
2025-11-08 21:44:25 +03:00
def after_change_cst_type(self, sourceID: int, target: int, new_type: CstType,
2025-08-03 15:28:16 +03:00
exclude: Optional[list[int]] = None) -> None:
2024-08-11 00:10:27 +03:00
''' Trigger cascade resolutions when constituenta type is changed. '''
2025-08-03 15:28:16 +03:00
hosts = _get_oss_hosts(sourceID)
2024-08-11 00:10:27 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).after_change_cst_type(sourceID, target, new_type)
2024-08-11 00:10:27 +03:00
2025-11-08 21:44:25 +03:00
# pylint: disable=too-many-arguments, too-many-positional-arguments
2024-10-28 14:52:30 +03:00
def after_update_cst(
2025-11-08 21:44:25 +03:00
self, source: RSFormCached, target: int,
data: dict, old_data: dict,
2024-10-28 14:52:30 +03:00
exclude: Optional[list[int]] = None
) -> None:
2024-08-11 00:10:27 +03:00
''' Trigger cascade resolutions when constituenta data is changed. '''
2025-11-08 19:32:42 +03:00
hosts = _get_oss_hosts(source.pk)
2024-08-11 00:10:27 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).after_update_cst(source, target, data, old_data)
2024-08-11 00:10:27 +03:00
2025-11-08 21:44:25 +03:00
def before_delete_cst(self, sourceID: int, target: list[int],
2025-08-01 10:43:51 +03:00
exclude: Optional[list[int]] = None) -> None:
2024-08-11 00:10:27 +03:00
''' Trigger cascade resolutions before constituents are deleted. '''
2025-08-03 15:28:16 +03:00
hosts = _get_oss_hosts(sourceID)
2024-08-11 12:37:18 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).before_delete_cst(sourceID, target)
2024-08-12 16:51:24 +03:00
2025-11-08 21:44:25 +03:00
def before_substitute(self, sourceID: int, substitutions: CstSubstitution,
2025-08-01 10:43:51 +03:00
exclude: Optional[list[int]] = None) -> None:
2024-08-12 16:51:24 +03:00
''' Trigger cascade resolutions before constituents are substituted. '''
2025-08-10 12:40:23 +03:00
if not substitutions:
2025-08-03 11:37:27 +03:00
return
hosts = _get_oss_hosts(sourceID)
2024-08-12 16:51:24 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).before_substitute(sourceID, substitutions)
2025-11-08 21:44:25 +03:00
def before_delete_schema(self, target: int, exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions before schema is deleted. '''
2025-11-08 19:32:42 +03:00
hosts = _get_oss_hosts(target)
2025-08-10 12:40:23 +03:00
if not hosts:
return
2025-11-08 19:32:42 +03:00
ids = list(Constituenta.objects.filter(schema_id=target).order_by('order').values_list('pk', flat=True))
2025-08-10 12:40:23 +03:00
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).before_delete_cst(target, ids)
del self._oss[host]
2025-08-12 20:31:35 +03:00
2025-11-08 21:44:25 +03:00
def after_create_attribution(self, sourceID: int,
2025-11-08 19:32:42 +03:00
attributions: list[Attribution],
2025-08-12 20:31:35 +03:00
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions when Attribution is created. '''
2025-08-12 20:31:35 +03:00
hosts = _get_oss_hosts(sourceID)
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).after_create_attribution(sourceID, attributions)
2025-08-12 20:31:35 +03:00
2025-11-08 21:44:25 +03:00
def before_delete_attribution(self, sourceID: int,
2025-11-07 00:06:06 +03:00
attributions: list[Attribution],
2025-08-12 20:31:35 +03:00
exclude: Optional[list[int]] = None) -> None:
''' Trigger cascade resolutions before Attribution is deleted. '''
2025-08-12 20:31:35 +03:00
hosts = _get_oss_hosts(sourceID)
for host in hosts:
2025-11-08 19:32:42 +03:00
if exclude is None or host not in exclude:
2025-11-08 21:44:25 +03:00
self.get_oss(host).before_delete_attribution(sourceID, attributions)