refactor(core): adjust CenturionSubModel to not be it's own inheritable class
it's not required as the inheritance does not match the actual class'. ref: #807 #767
This commit is contained in:
@ -262,10 +262,28 @@ class Centurion(
|
||||
dict: Kwargs required for reverse function to build a models URL.
|
||||
"""
|
||||
|
||||
kwargs = {}
|
||||
|
||||
if self._is_submodel:
|
||||
|
||||
kwargs.update({
|
||||
# **super().get_url_kwargs( many = many ),
|
||||
# 'app_label': self._meta.app_label, # this has been removed as the app_namespace can cover
|
||||
'model_name': str(self._meta.model_name),
|
||||
# 'model_id': self.model.id, # Unknown why this was added as sub-model id's match the model
|
||||
})
|
||||
|
||||
if many:
|
||||
return {}
|
||||
|
||||
return kwargs
|
||||
|
||||
else:
|
||||
return { 'pk': self.id }
|
||||
|
||||
kwargs.update({
|
||||
'pk': self.id
|
||||
})
|
||||
|
||||
return kwargs
|
||||
|
||||
|
||||
|
||||
|
@ -9,7 +9,6 @@ from django.core.exceptions import ValidationError
|
||||
|
||||
from core.models.centurion import (
|
||||
CenturionModel,
|
||||
CenturionSubModel,
|
||||
)
|
||||
|
||||
|
||||
@ -240,9 +239,10 @@ class CenturionAudit(
|
||||
|
||||
class AuditMetaModel(
|
||||
CenturionAudit,
|
||||
CenturionSubModel,
|
||||
):
|
||||
|
||||
_is_submodel = True
|
||||
|
||||
model_notes = None
|
||||
|
||||
class Meta:
|
||||
@ -280,7 +280,9 @@ class AuditMetaModel(
|
||||
|
||||
kwargs.update({
|
||||
**super().get_url_kwargs( many = many ),
|
||||
'app_label': self._meta.app_label,
|
||||
'model_name': str(self._meta.model_name).replace('audithistory', ''),
|
||||
'model_id': self.model.id,
|
||||
})
|
||||
|
||||
return kwargs
|
||||
|
@ -49,31 +49,3 @@ class CenturionModel(
|
||||
if value is None:
|
||||
|
||||
raise ValidationError(code = 'field_value_not_none', message = 'Value can not be none.')
|
||||
|
||||
|
||||
|
||||
class CenturionSubModel(
|
||||
CenturionModel
|
||||
):
|
||||
|
||||
_is_submodel: bool = True
|
||||
"""This model a sub-model"""
|
||||
|
||||
|
||||
class Meta:
|
||||
|
||||
abstract = True
|
||||
|
||||
|
||||
def get_url_kwargs(self, many = False):
|
||||
|
||||
kwargs = {}
|
||||
|
||||
kwargs.update({
|
||||
**super().get_url_kwargs( many = many ),
|
||||
'app_label': self._meta.app_label,
|
||||
'model_name': str(self._meta.model_name),
|
||||
'model_id': self.model.id,
|
||||
})
|
||||
|
||||
return kwargs
|
||||
|
@ -8,7 +8,6 @@ from django.db import models
|
||||
from access.fields import AutoLastModifiedField
|
||||
from core.models.centurion import (
|
||||
CenturionModel,
|
||||
CenturionSubModel,
|
||||
)
|
||||
|
||||
|
||||
@ -109,9 +108,10 @@ class CenturionModelNote(
|
||||
|
||||
class NoteMetaModel(
|
||||
CenturionModelNote,
|
||||
CenturionSubModel,
|
||||
):
|
||||
|
||||
_is_submodel = True
|
||||
|
||||
model_notes = None
|
||||
|
||||
class Meta:
|
||||
@ -154,7 +154,9 @@ class NoteMetaModel(
|
||||
|
||||
kwargs.update({
|
||||
**super().get_url_kwargs( many = many ),
|
||||
'app_label': self._meta.app_label,
|
||||
'model_name': str(self._meta.model_name).replace('centurionmodelnote', ''),
|
||||
'model_id': self.model.id,
|
||||
})
|
||||
|
||||
return kwargs
|
||||
|
@ -30,6 +30,12 @@ if apps.models_ready:
|
||||
continue
|
||||
|
||||
|
||||
related_name = 'audit_history'
|
||||
if model._is_submodel:
|
||||
|
||||
related_name = '+'
|
||||
|
||||
|
||||
AuditMetaModel = type(
|
||||
audit_meta_name,
|
||||
( import_string("core.models.audit.AuditMetaModel"), ),
|
||||
@ -50,7 +56,7 @@ if apps.models_ready:
|
||||
help_text = 'Model this history belongs to',
|
||||
null = False,
|
||||
on_delete = models.CASCADE,
|
||||
related_name = 'audit_history',
|
||||
related_name = related_name,
|
||||
verbose_name = 'Model',
|
||||
)
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from core.models.centurion import CenturionSubModel
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model(model_centurionsubmodel):
|
||||
|
||||
yield model_centurionsubmodel
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class', autouse = True)
|
||||
def model_kwargs(request, kwargs_centurionsubmodel):
|
||||
|
||||
request.cls.kwargs_create_item = kwargs_centurionsubmodel.copy()
|
||||
|
||||
yield kwargs_centurionsubmodel.copy()
|
||||
|
||||
if hasattr(request.cls, 'kwargs_create_item'):
|
||||
del request.cls.kwargs_create_item
|
@ -2,25 +2,24 @@ import pytest
|
||||
|
||||
from django.db import models
|
||||
|
||||
from core.tests.unit.centurion_abstract.test_unit_centurion_abstract_model import (
|
||||
CenturionAbstractModelTestCases,
|
||||
CenturionAbstractModelInheritedCases,
|
||||
)
|
||||
|
||||
from centurion.tests.unit_models import ModelTestCases
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
@pytest.mark.unit
|
||||
class CenturionSubAbstractModelTestCases(
|
||||
CenturionAbstractModelTestCases
|
||||
ModelTestCases
|
||||
):
|
||||
|
||||
|
||||
parameterized_class_attributes = {
|
||||
'_is_submodel': {
|
||||
'value': True,
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
|
||||
return {
|
||||
'_is_submodel': {
|
||||
'value': True,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_method_get_url_attribute__is_submodel_set(self, mocker, model_instance, settings):
|
||||
@ -37,7 +36,15 @@ class CenturionSubAbstractModelTestCases(
|
||||
|
||||
model_instance.model = model_instance
|
||||
|
||||
url_basename = f'v2:_api_{model_instance._meta.model_name}_sub-detail'
|
||||
app_namespace = ''
|
||||
if model_instance.app_namespace:
|
||||
app_namespace = model_instance.app_namespace + ':'
|
||||
|
||||
url_model_name = model_instance._meta.model_name
|
||||
if model_instance.url_model_name:
|
||||
url_model_name = model_instance.url_model_name
|
||||
|
||||
url_basename = f'v2:{app_namespace}_api_{url_model_name}_sub-detail'
|
||||
|
||||
url = model_instance.get_url( relative = True)
|
||||
|
||||
@ -45,9 +52,9 @@ class CenturionSubAbstractModelTestCases(
|
||||
url_basename,
|
||||
None,
|
||||
{
|
||||
'app_label': model_instance._meta.app_label,
|
||||
# 'app_label': model_instance._meta.app_label,
|
||||
'model_name': model_instance._meta.model_name,
|
||||
'model_id': model_instance.model.id,
|
||||
# 'model_id': model_instance.model.id,
|
||||
'pk': model_instance.id,
|
||||
},
|
||||
None,
|
||||
@ -68,9 +75,9 @@ class CenturionSubAbstractModelTestCases(
|
||||
url = model_instance.get_url_kwargs()
|
||||
|
||||
assert model_instance.get_url_kwargs() == {
|
||||
'app_label': model_instance._meta.app_label,
|
||||
# 'app_label': model_instance._meta.app_label,
|
||||
'model_name': model_instance._meta.model_name,
|
||||
'model_id': model_instance.model.id,
|
||||
# 'model_id': model_instance.model.id,
|
||||
'pk': model_instance.id,
|
||||
}
|
||||
|
||||
@ -81,50 +88,6 @@ class CenturionSubAbstractModelTestCases(
|
||||
|
||||
class CenturionSubAbstractModelInheritedCases(
|
||||
CenturionSubAbstractModelTestCases,
|
||||
CenturionAbstractModelInheritedCases,
|
||||
):
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
class CenturionSubAbstractModelPyTest(
|
||||
CenturionSubAbstractModelTestCases,
|
||||
):
|
||||
|
||||
@property
|
||||
def parameterized_class_attributes(self):
|
||||
|
||||
return {
|
||||
'page_layout': {
|
||||
'type': models.NOT_PROVIDED,
|
||||
'value': models.NOT_PROVIDED,
|
||||
},
|
||||
'table_fields': {
|
||||
'type': models.NOT_PROVIDED,
|
||||
'value': models.NOT_PROVIDED,
|
||||
},
|
||||
'model_tag': {
|
||||
'type': type(None),
|
||||
'value': None,
|
||||
},
|
||||
'url_model_name': {
|
||||
'type': type(None),
|
||||
'value': None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.xfail( reason = 'This model is an abstract model')
|
||||
def test_model_tag_defined(self, model):
|
||||
""" Model Tag
|
||||
|
||||
Ensure that the model has a tag defined.
|
||||
"""
|
||||
|
||||
assert model.model_tag is not None
|
||||
|
||||
|
||||
def test_model_is_abstract(self, model):
|
||||
|
||||
assert model._meta.abstract
|
||||
|
@ -242,6 +242,16 @@ class CenturionMixnInheritedCases(
|
||||
|
||||
|
||||
|
||||
def test_method_get_url_returns_str(self, mocker, model_instance):
|
||||
"""Test Class Method
|
||||
|
||||
Ensure method `get_url` returns the url as str
|
||||
"""
|
||||
|
||||
assert type(model_instance.get_url()) is str, model_instance.get_url()
|
||||
|
||||
|
||||
|
||||
class CenturionMixnPyTest(
|
||||
CenturionMixnTestCases,
|
||||
):
|
||||
|
7
app/tests/fixtures/__init__.py
vendored
7
app/tests/fixtures/__init__.py
vendored
@ -1,6 +1,6 @@
|
||||
# pylint: disable=W0611:unused-import
|
||||
|
||||
from app.tests.fixtures.model_centurionsubmodel import model_centurionsubmodel
|
||||
|
||||
from .api_request_permissions import (
|
||||
api_request_permissions,
|
||||
)
|
||||
@ -38,11 +38,6 @@ from .model_centurionmodelnotemeta import (
|
||||
model_centurionmodelnotemeta,
|
||||
)
|
||||
|
||||
from .model_centurionsubmodel import (
|
||||
kwargs_centurionsubmodel,
|
||||
model_centurionsubmodel,
|
||||
)
|
||||
|
||||
from .model_checkin import (
|
||||
kwargs_checkin,
|
||||
model_checkin,
|
||||
|
20
app/tests/fixtures/model_centurionsubmodel.py
vendored
20
app/tests/fixtures/model_centurionsubmodel.py
vendored
@ -1,20 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from core.models.centurion import CenturionSubModel
|
||||
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def model_centurionsubmodel(request):
|
||||
|
||||
yield CenturionSubModel
|
||||
|
||||
|
||||
@pytest.fixture( scope = 'class')
|
||||
def kwargs_centurionsubmodel(request, kwargs_centurionmodel):
|
||||
|
||||
kwargs = {
|
||||
**kwargs_centurionmodel.copy(),
|
||||
}
|
||||
|
||||
yield kwargs.copy()
|
Reference in New Issue
Block a user