Refactor RSForm backend and tests
This commit is contained in:
parent
98ad3e2534
commit
e9f77057d9
|
@ -1,6 +1,6 @@
|
||||||
''' Models: RSForm API. '''
|
''' Models: RSForm API. '''
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from typing import Optional, Union, cast
|
from typing import Optional, cast
|
||||||
|
|
||||||
from cctext import Entity, Resolver, TermForm, extract_entities, split_grams
|
from cctext import Entity, Resolver, TermForm, extract_entities, split_grams
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
@ -39,7 +39,7 @@ class RSForm:
|
||||||
def create(**kwargs) -> 'RSForm':
|
def create(**kwargs) -> 'RSForm':
|
||||||
return RSForm(LibraryItem.objects.create(item_type=LibraryItemType.RSFORM, **kwargs))
|
return RSForm(LibraryItem.objects.create(item_type=LibraryItemType.RSFORM, **kwargs))
|
||||||
|
|
||||||
def constituents(self) -> QuerySet['Constituenta']:
|
def constituents(self) -> QuerySet[Constituenta]:
|
||||||
''' Get QuerySet containing all constituents of current RSForm. '''
|
''' Get QuerySet containing all constituents of current RSForm. '''
|
||||||
return Constituenta.objects.filter(schema=self.item)
|
return Constituenta.objects.filter(schema=self.item)
|
||||||
|
|
||||||
|
@ -104,11 +104,40 @@ class RSForm:
|
||||||
result = max(result, int(alias[1:]))
|
result = max(result, int(alias[1:]))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def create_cst(self, data: dict, insert_after: Optional[str] = None) -> Constituenta:
|
||||||
|
''' Create new cst from data. '''
|
||||||
|
if insert_after is None:
|
||||||
|
position = _INSERT_LAST
|
||||||
|
else:
|
||||||
|
cst_after = Constituenta.objects.get(pk=insert_after)
|
||||||
|
position = cst_after.order + 1
|
||||||
|
result = self.insert_new(data['alias'], data['cst_type'], position)
|
||||||
|
result.convention = data.get('convention', '')
|
||||||
|
result.definition_formal = data.get('definition_formal', '')
|
||||||
|
result.term_forms = data.get('term_forms', [])
|
||||||
|
result.term_raw = data.get('term_raw', '')
|
||||||
|
result.definition_raw = data.get('definition_raw', '')
|
||||||
|
|
||||||
|
if result.term_raw != '' or result.definition_raw != '':
|
||||||
|
resolver = self.resolver()
|
||||||
|
if result.term_raw != '':
|
||||||
|
resolved = resolver.resolve(result.term_raw)
|
||||||
|
result.term_resolved = resolved
|
||||||
|
resolver.context[result.alias] = Entity(result.alias, resolved)
|
||||||
|
if result.definition_raw != '':
|
||||||
|
result.definition_resolved = resolver.resolve(result.definition_raw)
|
||||||
|
|
||||||
|
result.save()
|
||||||
|
self.on_term_change([result.id])
|
||||||
|
result.refresh_from_db()
|
||||||
|
return result
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def insert_new(
|
def insert_new(
|
||||||
self,
|
self,
|
||||||
alias: str,
|
alias: str,
|
||||||
cst_type: Union[CstType, None] = None,
|
cst_type: Optional[CstType] = None,
|
||||||
position: int = _INSERT_LAST,
|
position: int = _INSERT_LAST,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> Constituenta:
|
) -> Constituenta:
|
||||||
|
@ -195,27 +224,6 @@ class RSForm:
|
||||||
self.resolve_all_text()
|
self.resolve_all_text()
|
||||||
self.item.save()
|
self.item.save()
|
||||||
|
|
||||||
@transaction.atomic
|
|
||||||
def create_cst(self, data: dict, insert_after: Optional[str] = None) -> Constituenta:
|
|
||||||
''' Create new cst from data. '''
|
|
||||||
resolver = self.resolver()
|
|
||||||
cst = self._insert_new(data, insert_after)
|
|
||||||
cst.convention = data.get('convention', '')
|
|
||||||
cst.definition_formal = data.get('definition_formal', '')
|
|
||||||
cst.term_forms = data.get('term_forms', [])
|
|
||||||
cst.term_raw = data.get('term_raw', '')
|
|
||||||
if cst.term_raw != '':
|
|
||||||
resolved = resolver.resolve(cst.term_raw)
|
|
||||||
cst.term_resolved = resolved
|
|
||||||
resolver.context[cst.alias] = Entity(cst.alias, resolved)
|
|
||||||
cst.definition_raw = data.get('definition_raw', '')
|
|
||||||
if cst.definition_raw != '':
|
|
||||||
cst.definition_resolved = resolver.resolve(cst.definition_raw)
|
|
||||||
cst.save()
|
|
||||||
self.on_term_change([cst.id])
|
|
||||||
cst.refresh_from_db()
|
|
||||||
return cst
|
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def substitute(
|
def substitute(
|
||||||
self,
|
self,
|
||||||
|
@ -363,13 +371,6 @@ class RSForm:
|
||||||
cst.save()
|
cst.save()
|
||||||
order += 1
|
order += 1
|
||||||
|
|
||||||
def _insert_new(self, data: dict, insert_after: Optional[str] = None) -> Constituenta:
|
|
||||||
if insert_after is not None:
|
|
||||||
cst_after = Constituenta.objects.get(pk=insert_after)
|
|
||||||
return self.insert_new(data['alias'], data['cst_type'], cst_after.order + 1)
|
|
||||||
else:
|
|
||||||
return self.insert_new(data['alias'], data['cst_type'])
|
|
||||||
|
|
||||||
def _graph_formal(self) -> Graph[int]:
|
def _graph_formal(self) -> Graph[int]:
|
||||||
''' Graph based on formal definitions. '''
|
''' Graph based on formal definitions. '''
|
||||||
result: Graph[int] = Graph()
|
result: Graph[int] = Graph()
|
||||||
|
|
|
@ -26,6 +26,21 @@ class EndpointTester(APITestCase):
|
||||||
''' Abstract base class for Testing endpoints. '''
|
''' Abstract base class for Testing endpoints. '''
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
self.factory = APIRequestFactory()
|
||||||
|
self.user = User.objects.create(
|
||||||
|
username='UserTest',
|
||||||
|
email='blank@test.com',
|
||||||
|
password='password'
|
||||||
|
)
|
||||||
|
self.user2 = User.objects.create(
|
||||||
|
username='UserTest2',
|
||||||
|
email='another@test.com',
|
||||||
|
password='password'
|
||||||
|
)
|
||||||
|
self.client = APIClient()
|
||||||
|
self.client.force_authenticate(user=self.user)
|
||||||
|
|
||||||
|
def setUpFullUsers(self):
|
||||||
self.factory = APIRequestFactory()
|
self.factory = APIRequestFactory()
|
||||||
self.user = User.objects.create_user(
|
self.user = User.objects.create_user(
|
||||||
username='UserTest',
|
username='UserTest',
|
||||||
|
|
|
@ -9,7 +9,7 @@ class TestUserAPIViews(EndpointTester):
|
||||||
''' Testing Authentication views. '''
|
''' Testing Authentication views. '''
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUpFullUsers()
|
||||||
|
|
||||||
|
|
||||||
@decl_endpoint('/users/api/login', method='post')
|
@decl_endpoint('/users/api/login', method='post')
|
||||||
|
@ -59,7 +59,7 @@ class TestUserUserProfileAPIView(EndpointTester):
|
||||||
''' Testing User profile views. '''
|
''' Testing User profile views. '''
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUpFullUsers()
|
||||||
self.user.first_name = 'John'
|
self.user.first_name = 'John'
|
||||||
self.user.second_name = 'Smith'
|
self.user.second_name = 'Smith'
|
||||||
self.user.save()
|
self.user.save()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user