From 73fdaee33fd32a4d08de0256913ca6e7e3b55f02 Mon Sep 17 00:00:00 2001 From: Jon Date: Wed, 21 May 2025 00:50:58 +0930 Subject: [PATCH] refactor: rejig whats in each inherited centurion model ref: #770 --- app/access/models/tenancy_abstract.py | 101 ++++++++------------------ app/core/models/audit.py | 90 +++++++++-------------- app/core/models/centurion.py | 31 ++++++-- 3 files changed, 89 insertions(+), 133 deletions(-) diff --git a/app/access/models/tenancy_abstract.py b/app/access/models/tenancy_abstract.py index d4c9bb7d..40e845a6 100644 --- a/app/access/models/tenancy_abstract.py +++ b/app/access/models/tenancy_abstract.py @@ -28,65 +28,40 @@ class TenancyManager( (queryset): **not super user**: return data from the stored unique organizations. """ - # user = None # When CenturionUser in use - - # if hasattr(self.model, 'context'): - - # user = self.model.context['user'] - - - # if user: - - # tencies = user.get_tenancies(int_list = True) - - # if len(tenancies) > 0 and not request.user.is_superuser: - - # if hasattr(self.model, 'organization'): - # return super().get_queryset().select_related('organization').filter( - # models.Q(organization__in = tenancies) - # ) - - # return super().get_queryset().select_related('organization').filter( - # models.Q(organization__in = tenancies) - # ) - - request = None + user = None # When CenturionUser in use if hasattr(self.model, 'context'): - request = self.model.context['request'] - - if request is not None: - - tenancies: list(str()) = [] - - if request.app_settings.global_organization: - - tenancies += [ request.app_settings.global_organization.id ] + user = self.model.context['user'] - if request.user.is_authenticated: - - for team in request.tenancy._user_teams: - - if team.organization.id in tenancies: - continue - - tenancies += [ team.organization.id ] + has_tenant_field = False + if hasattr(self.model, 'organization'): + has_tenant_field = True - if len(tenancies) > 0 and not request.user.is_superuser: + if user: - if hasattr(self.model, 'organization'): - return super().get_queryset().select_related('organization').filter( - models.Q(organization__in = tenancies) - ) + tencies = user.get_tenancies(int_list = True) + if len(tenancies) > 0 and not request.user.is_superuser: + + if has_tenant_field: return super().get_queryset().select_related('organization').filter( models.Q(organization__in = tenancies) ) - return super().get_queryset().select_related('organization') + + return super().get_queryset().select_related('organization').filter( + models.Q(organization__in = tenancies) + ) + + + if has_tenant_field: + return super().get_queryset().select_related('organization') + + + return super().get_queryset() @@ -103,6 +78,15 @@ class TenancyAbstractModel( ValidationError: User failed to supply organization """ + context: dict = { + 'logger': None, + 'request': None, + } + """ Model Context + + Context for actions within the model. + """ + objects = TenancyManager() """ Multi-Tenanant Objects """ @@ -123,15 +107,6 @@ class TenancyAbstractModel( message = 'You must provide an organization' ) - - id = models.AutoField( - blank=False, - help_text = 'ID of the item', - primary_key=True, - unique=True, - verbose_name = 'ID' - ) - organization = models.ForeignKey( Tenant, blank = False, @@ -145,22 +120,6 @@ class TenancyAbstractModel( verbose_name = 'Tenant' ) - is_global = models.BooleanField( - blank = False, - default = False, - help_text = 'Is this a global object?', - null = False, - verbose_name = 'Global Object' - ) - - model_notes = models.TextField( - blank = True, - default = None, # ToDo: Remove this field - help_text = 'Tid bits of information', - null = True, - verbose_name = 'Notes', - ) - def get_tenant(self) -> Tenant: diff --git a/app/core/models/audit.py b/app/core/models/audit.py index a345f087..53f2ef05 100644 --- a/app/core/models/audit.py +++ b/app/core/models/audit.py @@ -4,10 +4,7 @@ from django.db import models from django.core.exceptions import ValidationError from rest_framework.reverse import reverse - -from access.fields import AutoCreatedField from access.models.tenant import Tenant -from access.models.tenancy import TenancyObject from core.models.centurion import ( CenturionModel, @@ -20,7 +17,6 @@ from core.lib.feature_not_used import FeatureNotUsed class CenturionAudit( CenturionModel, - TenancyObject, ): """Centurion Audit History @@ -28,7 +24,6 @@ class CenturionAudit( model history is via the `delete` and `save` signals Args: - ModelHistoryOld (_type_): Old Model attributes and functions due for removal. CenturionModel (_type_): Centurion Model attributes, functions and method TenancyObject (_type_): Centurion Tenancy Abstract model. """ @@ -43,7 +38,7 @@ class CenturionAudit( class Meta: # db_table = 'centurion_audit' - db_table = 'core_model_history' + db_table = 'core_audithistory' ordering = [ '-created' @@ -54,36 +49,15 @@ class CenturionAudit( verbose_name_plural = 'Model Histories' - id = models.AutoField( - blank=False, - help_text = 'ID of the item', - primary_key=True, - unique=True, - verbose_name = 'ID' - ) - - organization = models.ForeignKey( - Tenant, - blank = False, - help_text = 'Tenancy this belongs to', - null = True, - on_delete = models.CASCADE, - related_name = '+', - # validators = [ - # CenturionModel.validate_field_not_none, - # ], - verbose_name = 'Tenant' - ) - content_type = models.ForeignKey( ContentType, blank= True, help_text = 'Model this history is for', null = False, on_delete = models.CASCADE, - # validators = [ - # CenturionModel.validate_field_not_none, - # ], + validators = [ + CenturionModel.validate_field_not_none, + ], verbose_name = 'Content Model' ) @@ -92,9 +66,9 @@ class CenturionAudit( default = None, help_text = 'Value before Change', null = True, - # validators = [ - # CenturionModel.validate_field_not_none, - # ], + validators = [ + CenturionModel.validate_field_not_none, + ], verbose_name = 'Before' ) @@ -104,9 +78,9 @@ class CenturionAudit( default = None, help_text = 'Value Change to', null = True, - # validators = [ - # CenturionModel.validate_field_not_none, - # ], + validators = [ + CenturionModel.validate_field_not_none, + ], verbose_name = 'After' ) @@ -122,9 +96,9 @@ class CenturionAudit( default = None, help_text = 'History action performed', null = True, - # validators = [ - # CenturionModel.validate_field_not_none, - # ], + validators = [ + CenturionModel.validate_field_not_none, + ], verbose_name = 'Action' ) @@ -134,16 +108,12 @@ class CenturionAudit( help_text = 'User whom performed the action', null = True, on_delete = models.DO_NOTHING, - # validators = [ - # CenturionModel.validate_field_not_none, - # ], + validators = [ + CenturionModel.validate_field_not_none, + ], verbose_name = 'User' ) - created = AutoCreatedField( - editable = True - ) - page_layout: list = [] @@ -164,13 +134,6 @@ class CenturionAudit( def clean_fields(self, exclude = None): - if not self.get_model_history(): - - raise ValidationError( - code = 'did_not_process_history', - message = 'Unable to process the history.' - ) - super().clean_fields(exclude = exclude) @@ -230,6 +193,21 @@ class AuditMetaModel( ): - class Meta: - abstract = True - proxy = False + class Meta: + abstract = True + proxy = False + + + + def clean_fields(self, exclude = None): + + if hasattr(self, 'model'): + + if not self.get_model_history(self.model): + + raise ValidationError( + code = 'did_not_process_history', + message = 'Unable to process the history.' + ) + + super().clean_fields(exclude = exclude) diff --git a/app/core/models/centurion.py b/app/core/models/centurion.py index 17c57455..71166458 100644 --- a/app/core/models/centurion.py +++ b/app/core/models/centurion.py @@ -2,6 +2,9 @@ from django.conf import settings from django.core.exceptions import ( ValidationError ) +from django.db import models + +from access.fields import AutoCreatedField from rest_framework.reverse import reverse @@ -11,7 +14,6 @@ from access.models.tenancy_abstract import TenancyAbstractModel class CenturionModel( TenancyAbstractModel, - # models.Model ): @@ -29,12 +31,7 @@ class CenturionModel( 'logger': None, 'request': None, } - """ Model Context - **ToDo:** Drop request and add user when user model refactored with tenancy. - - Context for actions within the model. - """ class Meta: @@ -42,6 +39,28 @@ class CenturionModel( abstract = True + id = models.AutoField( + blank=False, + help_text = 'ID of the item', + primary_key=True, + unique=True, + verbose_name = 'ID' + ) + + + model_notes = models.TextField( + blank = True, + help_text = 'Tid bits of information', + null = True, + verbose_name = 'Notes', + ) + + + created = AutoCreatedField( + editable = True + ) + + @staticmethod def validate_field_not_none(value):