2024-08-11 12:38:08 +03:00
|
|
|
''' Models: Change propagation facade - managing all changes in OSS. '''
|
2024-08-15 23:23:45 +03:00
|
|
|
from apps.library.models import LibraryItem, LibraryItemType
|
2024-08-11 12:38:08 +03:00
|
|
|
from apps.rsform.models import Constituenta, RSForm
|
|
|
|
|
2024-08-14 13:13:41 +03:00
|
|
|
from .OperationSchema import CstSubstitution, OperationSchema
|
2024-08-11 12:38:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2024-08-16 20:57:37 +03:00
|
|
|
def after_create_cst(source: RSForm, new_cst: list[Constituenta]) -> 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-16 20:57:37 +03:00
|
|
|
OperationSchema(host).after_create_cst(source, new_cst)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
2024-08-16 20:57:37 +03:00
|
|
|
def after_change_cst_type(source: RSForm, target: Constituenta) -> 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-16 20:57:37 +03:00
|
|
|
OperationSchema(host).after_change_cst_type(source, target)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
2024-08-16 20:57:37 +03:00
|
|
|
def after_update_cst(source: RSForm, target: Constituenta, data: dict, old_data: dict) -> 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-16 20:57:37 +03:00
|
|
|
OperationSchema(host).after_update_cst(source, target, data, old_data)
|
2024-08-11 12:38:08 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
2024-08-16 20:57:37 +03:00
|
|
|
def before_delete_cst(source: RSForm, target: list[Constituenta]) -> 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:
|
2024-08-16 20:57:37 +03:00
|
|
|
OperationSchema(host).before_delete_cst(source, target)
|
2024-08-12 16:52:06 +03:00
|
|
|
|
2024-08-13 23:38:19 +03:00
|
|
|
@staticmethod
|
2024-08-16 20:57:37 +03:00
|
|
|
def before_substitute(source: RSForm, substitutions: CstSubstitution) -> 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:
|
2024-08-16 20:57:37 +03:00
|
|
|
OperationSchema(host).before_substitute(source, substitutions)
|
2024-08-15 23:23:45 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def before_delete_schema(item: LibraryItem) -> None:
|
|
|
|
''' Trigger cascade resolutions before schema is deleted. '''
|
|
|
|
if item.item_type != LibraryItemType.RSFORM:
|
|
|
|
return
|
|
|
|
hosts = _get_oss_hosts(item)
|
|
|
|
if len(hosts) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
schema = RSForm(item)
|
2024-09-01 15:14:24 +03:00
|
|
|
PropagationFacade.before_delete_cst(schema, list(schema.constituents().order_by('order')))
|