chore(core): Add temp objects to CenturionAbstractModel to support depreciated features
these will be removed when #767 is complete ref: #772 #767
This commit is contained in:
@ -255,7 +255,7 @@ class TenancyObject(SaveHistory):
|
||||
|
||||
if self.app_namespace:
|
||||
|
||||
app_namespace = self.app_namespace + ':'
|
||||
app_namespace = self.app_namespace
|
||||
|
||||
return str(app_namespace)
|
||||
|
||||
@ -274,12 +274,17 @@ class TenancyObject(SaveHistory):
|
||||
|
||||
model_name = str(self._meta.verbose_name.lower()).replace(' ', '_')
|
||||
|
||||
namespace = f'v2'
|
||||
|
||||
if self.get_app_namespace():
|
||||
namespace = namespace + ':' + self.get_app_namespace()
|
||||
|
||||
|
||||
if request:
|
||||
|
||||
return reverse(f"v2:" + self.get_app_namespace() + f"_api_v2_{model_name}-detail", request=request, kwargs = self.get_url_kwargs() )
|
||||
return reverse(f"{namespace}:_api_v2_{model_name}-detail", request=request, kwargs = self.get_url_kwargs() )
|
||||
|
||||
return reverse(f"v2:" + self.get_app_namespace() + f"_api_v2_{model_name}-detail", kwargs = self.get_url_kwargs() )
|
||||
return reverse(f"{namespace}:_api_v2_{model_name}-detail", kwargs = self.get_url_kwargs() )
|
||||
|
||||
|
||||
def get_url_kwargs(self) -> dict:
|
||||
|
@ -82,11 +82,29 @@ class TenancyAbstractModel(
|
||||
}
|
||||
""" Model Context
|
||||
|
||||
Generally model usage will be from an API serializer, Admin Site or
|
||||
a management command. These sources are to pass through and set this
|
||||
context. The keys are:
|
||||
|
||||
!!! warning
|
||||
Failing to specify the user will prevent the tenancy manager from
|
||||
being multi-tenant. As such, the results retured will not be
|
||||
restricted to the users tenancy
|
||||
|
||||
returns:
|
||||
logger (logging.Logger): Instance of a logger for logging.
|
||||
user (User): The user that is logged into the system
|
||||
|
||||
Context for actions within the model.
|
||||
"""
|
||||
|
||||
objects = TenancyManager()
|
||||
""" Multi-Tenanant Objects """
|
||||
""" Multi-Tenant Manager
|
||||
|
||||
**Note:** This manager relies upon the model class having `context['user']`
|
||||
set. without a user the manager can not perform multi-tenant queries.
|
||||
"""
|
||||
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
@ -96,9 +96,9 @@ class ReactUIMetadata(OverRideJSONAPIMetadata):
|
||||
|
||||
if getattr(view, 'model', None):
|
||||
|
||||
if getattr(view.model, 'get_app_namespace', None):
|
||||
if getattr(view.model, 'app_namespace', None) not in [None, '']:
|
||||
|
||||
app_namespace = view.model().get_app_namespace()
|
||||
app_namespace = view.model().get_app_namespace() + ':'
|
||||
|
||||
|
||||
if view.kwargs.get('pk', None) is not None:
|
||||
|
@ -27,6 +27,12 @@ class CenturionModel(
|
||||
_notes_enabled: bool = True
|
||||
"""Should a table for notes be created for this model"""
|
||||
|
||||
app_namespace: str = None
|
||||
"""URL Application namespace.
|
||||
|
||||
**Note:** This attribute is a temp attribute until all models urls return
|
||||
to their own `urls.py` file from `api/urls_v2.py`.
|
||||
"""
|
||||
|
||||
|
||||
class Meta:
|
||||
@ -100,6 +106,25 @@ class CenturionModel(
|
||||
)
|
||||
|
||||
|
||||
def get_app_namespace(self) -> str:
|
||||
"""Fetch the Application namespace if specified.
|
||||
|
||||
**Note:** This attribute is a temp attribute until all models urls return
|
||||
to their own `urls.py` file from `api/urls_v2.py`.
|
||||
|
||||
Returns:
|
||||
str: Application namespace suffixed with colin `:`
|
||||
None: No application namespace found.
|
||||
"""
|
||||
|
||||
if not self.app_namespace:
|
||||
return None
|
||||
|
||||
app_namespace = self.app_namespace
|
||||
|
||||
return str(app_namespace)
|
||||
|
||||
|
||||
def get_audit_values(self) -> dict:
|
||||
"""Retrieve the field Values
|
||||
|
||||
@ -163,12 +188,14 @@ class CenturionModel(
|
||||
|
||||
|
||||
|
||||
def get_url( self, relative: bool = False, api_version: int = 2 ) -> str:
|
||||
def get_url( self, relative: bool = False, api_version: int = 2, request: any = None ) -> str:
|
||||
"""Return the models API URL
|
||||
|
||||
Args:
|
||||
relative (bool, optional): Return the relative URL for the model. Defaults to False.
|
||||
api_version (int, optional): API Version to use. Defaults to `2``.
|
||||
request (any, optional): Temp and unused attribute until rest of
|
||||
codebase re-written not to pass through.
|
||||
|
||||
Returns:
|
||||
str: API URL for the model
|
||||
@ -176,6 +203,9 @@ class CenturionModel(
|
||||
|
||||
namespace = f'v{api_version}'
|
||||
|
||||
if self.get_app_namespace():
|
||||
namespace = namespace + ':' + self.get_app_namespace()
|
||||
|
||||
url_basename = f'{namespace}:_api_{self._meta.model_name}-detail'
|
||||
|
||||
url = reverse( viewname = url_basename, kwargs = { 'pk': self.id } )
|
||||
|
@ -131,7 +131,7 @@ Within All of our models including when they are created via an API serializer,
|
||||
You have defined a model that has a user field that must always have a value. This model can be access via the API, in which the user field is auto-populated by object `request.user`. In the same token you have admin commands that uses this model.
|
||||
Now every time you use the admin command to create this model, it will fail validation due to there being no value for the user field. This is where the models validator methods come into play. defining method `clean()` within the model with the logic required to ensure that the user field has value for the user field ensures that the model now when used by the admin command is consistant and meets the intent of the models purpose.
|
||||
|
||||
Whilst most data that will use a model will be via an API Serializer, which in turn has its own validation. The models Validation is only to serve the purpose of ensuring data consistancy.
|
||||
Whilst most data that will use a model will be via an API Serializer, which in turn has its own validation. The models Validation is only to serve the purpose of ensuring data consistancy.
|
||||
|
||||
|
||||
## page_layout Attribute
|
||||
@ -223,7 +223,7 @@ table_fields: list = [
|
||||
|
||||
- adding `model.save()` method
|
||||
|
||||
- Do
|
||||
- Do
|
||||
|
||||
- Add `model.clean()` To set/modify any field values, _if required_
|
||||
|
||||
|
Reference in New Issue
Block a user