mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 13:00:39 +03:00
Refactor apply_mapping
This commit is contained in:
parent
e4a0b2aedc
commit
cf47d90822
|
@ -1,4 +1,6 @@
|
||||||
''' Models: Constituenta. '''
|
''' Models: Constituenta. '''
|
||||||
|
import re
|
||||||
|
|
||||||
from django.db.models import (
|
from django.db.models import (
|
||||||
CASCADE, ForeignKey, Model, PositiveIntegerField,
|
CASCADE, ForeignKey, Model, PositiveIntegerField,
|
||||||
TextChoices, TextField, CharField, JSONField
|
TextChoices, TextField, CharField, JSONField
|
||||||
|
@ -6,6 +8,12 @@ from django.db.models import (
|
||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from ..utils import apply_pattern
|
||||||
|
|
||||||
|
|
||||||
|
_REF_ENTITY_PATTERN = re.compile(r'@{([^0-9\-].*?)\|.*?}')
|
||||||
|
_GLOBAL_ID_PATTERN = re.compile(r'([XCSADFPT][0-9]+)') # cspell:disable-line
|
||||||
|
|
||||||
|
|
||||||
class CstType(TextChoices):
|
class CstType(TextChoices):
|
||||||
''' Type of constituenta '''
|
''' Type of constituenta '''
|
||||||
|
@ -99,3 +107,26 @@ class Constituenta(Model):
|
||||||
return
|
return
|
||||||
self.term_resolved = new_term
|
self.term_resolved = new_term
|
||||||
self.term_forms = []
|
self.term_forms = []
|
||||||
|
|
||||||
|
def apply_mapping(self, mapping: dict[str, str], change_aliases: bool = False):
|
||||||
|
modified = False
|
||||||
|
if change_aliases and self.alias in mapping:
|
||||||
|
modified = True
|
||||||
|
self.alias = mapping[self.alias]
|
||||||
|
expression = apply_pattern(self.definition_formal, mapping, _GLOBAL_ID_PATTERN)
|
||||||
|
if expression != self.definition_formal:
|
||||||
|
modified = True
|
||||||
|
self.definition_formal = expression
|
||||||
|
convention = apply_pattern(self.convention, mapping, _GLOBAL_ID_PATTERN)
|
||||||
|
if convention != self.convention:
|
||||||
|
modified = True
|
||||||
|
self.convention = convention
|
||||||
|
term = apply_pattern(self.term_raw, mapping, _REF_ENTITY_PATTERN)
|
||||||
|
if term != self.term_raw:
|
||||||
|
modified = True
|
||||||
|
self.term_raw = term
|
||||||
|
definition = apply_pattern(self.definition_raw, mapping, _REF_ENTITY_PATTERN)
|
||||||
|
if definition != self.definition_raw:
|
||||||
|
modified = True
|
||||||
|
self.definition_raw = definition
|
||||||
|
return modified
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
''' Models: RSForm API. '''
|
''' Models: RSForm API. '''
|
||||||
import re
|
|
||||||
|
|
||||||
from typing import Iterable, Optional, cast
|
from typing import Iterable, Optional, cast
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
@ -13,14 +11,9 @@ from .Constituenta import CstType, Constituenta
|
||||||
from .Version import Version
|
from .Version import Version
|
||||||
|
|
||||||
from ..graph import Graph
|
from ..graph import Graph
|
||||||
from ..utils import apply_pattern
|
|
||||||
from .. import messages as msg
|
from .. import messages as msg
|
||||||
|
|
||||||
|
|
||||||
_REF_ENTITY_PATTERN = re.compile(r'@{([^0-9\-].*?)\|.*?}')
|
|
||||||
_GLOBAL_ID_PATTERN = re.compile(r'([XCSADFPT][0-9]+)') # cspell:disable-line
|
|
||||||
|
|
||||||
|
|
||||||
def _get_type_prefix(cst_type: CstType) -> str:
|
def _get_type_prefix(cst_type: CstType) -> str:
|
||||||
''' Get alias prefix. '''
|
''' Get alias prefix. '''
|
||||||
if cst_type == CstType.BASE:
|
if cst_type == CstType.BASE:
|
||||||
|
@ -243,27 +236,7 @@ class RSForm:
|
||||||
''' Apply rename mapping. '''
|
''' Apply rename mapping. '''
|
||||||
cst_list = self.constituents().order_by('order')
|
cst_list = self.constituents().order_by('order')
|
||||||
for cst in cst_list:
|
for cst in cst_list:
|
||||||
modified = False
|
if cst.apply_mapping(mapping, change_aliases):
|
||||||
if change_aliases and cst.alias in mapping:
|
|
||||||
modified = True
|
|
||||||
cst.alias = mapping[cst.alias]
|
|
||||||
expression = apply_pattern(cst.definition_formal, mapping, _GLOBAL_ID_PATTERN)
|
|
||||||
if expression != cst.definition_formal:
|
|
||||||
modified = True
|
|
||||||
cst.definition_formal = expression
|
|
||||||
convention = apply_pattern(cst.convention, mapping, _GLOBAL_ID_PATTERN)
|
|
||||||
if convention != cst.convention:
|
|
||||||
modified = True
|
|
||||||
cst.convention = convention
|
|
||||||
term = apply_pattern(cst.term_raw, mapping, _REF_ENTITY_PATTERN)
|
|
||||||
if term != cst.term_raw:
|
|
||||||
modified = True
|
|
||||||
cst.term_raw = term
|
|
||||||
definition = apply_pattern(cst.definition_raw, mapping, _REF_ENTITY_PATTERN)
|
|
||||||
if definition != cst.definition_raw:
|
|
||||||
modified = True
|
|
||||||
cst.definition_raw = definition
|
|
||||||
if modified:
|
|
||||||
cst.save()
|
cst.save()
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
|
|
Loading…
Reference in New Issue
Block a user