2024-08-11 12:38:08 +03:00
|
|
|
''' Models: Change propagation facade - managing all changes in OSS. '''
|
|
|
|
from apps.library.models import LibraryItem
|
|
|
|
from apps.rsform.models import Constituenta, RSForm
|
|
|
|
|
|
|
|
from .ChangeManager import ChangeManager
|
|
|
|
|
|
|
|
|
|
|
|
def _get_oss_hosts(item: LibraryItem) -> list[LibraryItem]:
|
|
|
|
''' Get all hosts for LibraryItem. '''
|
|
|
|
return list(LibraryItem.objects.filter(operations__result=item).only('pk'))
|
|
|
|
|
|
|
|
|
|
|
|
class PropagationFacade:
|
|
|
|
''' Change propagation API. '''
|
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def after_create_cst(new_cst: list[Constituenta], source: RSForm) -> None:
|
2024-08-11 12:38:08 +03:00
|
|
|
''' Trigger cascade resolutions when new constituent is created. '''
|
|
|
|
hosts = _get_oss_hosts(source.model)
|
|
|
|
for host in hosts:
|
2024-08-11 21:23:45 +03:00
|
|
|
ChangeManager(host).after_create_cst(new_cst, source)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def after_change_cst_type(target: Constituenta, source: RSForm) -> None:
|
2024-08-11 12:38:08 +03:00
|
|
|
''' Trigger cascade resolutions when constituenta type is changed. '''
|
|
|
|
hosts = _get_oss_hosts(source.model)
|
|
|
|
for host in hosts:
|
2024-08-11 21:23:45 +03:00
|
|
|
ChangeManager(host).after_change_cst_type(target, source)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def after_update_cst(target: Constituenta, data: dict, old_data: dict, source: RSForm) -> None:
|
2024-08-11 12:38:08 +03:00
|
|
|
''' Trigger cascade resolutions when constituenta data is changed. '''
|
|
|
|
hosts = _get_oss_hosts(source.model)
|
|
|
|
for host in hosts:
|
2024-08-11 21:23:45 +03:00
|
|
|
ChangeManager(host).after_update_cst(target, data, old_data, source)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def before_delete(target: list[Constituenta], source: RSForm) -> None:
|
2024-08-11 12:38:08 +03:00
|
|
|
''' Trigger cascade resolutions before constituents are deleted. '''
|
|
|
|
hosts = _get_oss_hosts(source.model)
|
|
|
|
for host in hosts:
|
|
|
|
ChangeManager(host).before_delete(target, source)
|
2024-08-12 16:52:06 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
|
|
|
def before_substitute(substitutions: list[tuple[Constituenta, Constituenta]], source: RSForm) -> None:
|
2024-08-12 16:52:06 +03:00
|
|
|
''' Trigger cascade resolutions before constituents are substituted. '''
|
|
|
|
hosts = _get_oss_hosts(source.model)
|
|
|
|
for host in hosts:
|
|
|
|
ChangeManager(host).before_substitute(substitutions, source)
|