From d129256c4efe13531374caa2caeabccae6f6d5b5 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 7 Jun 2025 19:56:28 +0930 Subject: [PATCH] revert(access): revert model inheriteance work ref: #804 #791 --- app/access/models/team.py | 102 +++++++++++++++--- app/access/serializers/organization.py | 2 +- ..._serializer.py => test_team_serializer.py} | 0 .../{team_api_v2.py => test_team_api_v2.py} | 2 +- ..._team_model.py => test_unit_team_model.py} | 0 ...m_viewset.py => test_unit_team_viewset.py} | 2 +- app/access/urls_api.py | 6 +- app/assistance/serializers/knowledge_base.py | 2 +- .../serializers/knowledge_base_category.py | 2 +- 9 files changed, 93 insertions(+), 25 deletions(-) rename app/access/tests/functional/team/{team_serializer.py => test_team_serializer.py} (100%) rename app/access/tests/unit/team/{team_api_v2.py => test_team_api_v2.py} (99%) rename app/access/tests/unit/team/{unit_team_model.py => test_unit_team_model.py} (100%) rename app/access/tests/unit/team/{unit_team_viewset.py => test_unit_team_viewset.py} (94%) diff --git a/app/access/models/team.py b/app/access/models/team.py index 8776f919..ddd936aa 100644 --- a/app/access/models/team.py +++ b/app/access/models/team.py @@ -1,19 +1,20 @@ from django.db import models from django.contrib.auth.models import Group +from rest_framework.reverse import reverse + from access.fields import ( + AutoCreatedField, AutoLastModifiedField ) -from core.models.centurion import CenturionModel +from access.models.tenant import Tenant +from access.models.tenancy import TenancyObject + +from core import exceptions as centurion_exceptions - -class Team( - Group, - CenturionModel, -): - +class Team(Group, TenancyObject): class Meta: @@ -24,6 +25,27 @@ class Team( verbose_name_plural = "Teams" + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): + + if self.organization_id: + + self.name = self.organization.name.lower().replace(' ', '_') + '_' + self.team_name.lower().replace(' ', '_') + + super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields) + + + def validatate_organization_exists(self): + """Ensure that the user did provide an organization + + Raises: + ValidationError: User failed to supply organization. + """ + + if not self: + raise centurion_exceptions.ValidationError('You must provide an organization') + + + team_name = models.CharField( blank = False, help_text = 'Name to give this team', @@ -32,6 +54,18 @@ class Team( verbose_name = 'Name', ) + organization = models.ForeignKey( + Tenant, + blank = False, + help_text = 'Tenant this belongs to', + null = False, + on_delete = models.CASCADE, + validators = [validatate_organization_exists], + verbose_name = 'Tenant' + ) + + created = AutoCreatedField() + modified = AutoLastModifiedField() page_layout: dict = [ @@ -82,27 +116,47 @@ class Team( ] + def get_url( self, request = None ) -> str: - def clean_fields(self, exclude = None): + if request: - if self.organization_id: + return reverse(f"v2:_api_v2_organization_team-detail", request=request, kwargs = self.get_url_kwargs() ) - self.name = self.organization.name.lower().replace(' ', '_') + '_' + self.team_name.lower().replace(' ', '_') + return reverse(f"v2:_api_v2_organization_team-detail", kwargs = self.get_url_kwargs() ) - super().clean_fields(exclude = exclude) + def get_url_kwargs(self) -> dict: + """Fetch the URL kwargs + + Returns: + dict: kwargs required for generating the URL with `reverse` + """ + + return { + 'organization_id': self.organization.id, + 'pk': self.id + } + def get_url_kwargs_notes(self) -> dict: + """Fetch the URL kwargs for model notes - def get_url_kwargs(self, many = False) -> dict: + Returns: + dict: notes kwargs required for generating the URL with `reverse` + """ - kwargs = super().get_url_kwargs() + return { + 'organization_id': self.organization.id, + 'model_id': self.id + } + - kwargs.update({ - 'organization_id': self.organization.id - }) - return kwargs + # @property + # def parent_object(self): + # """ Fetch the parent object """ + + # return self.organization def permission_list(self) -> list: @@ -121,3 +175,17 @@ class Team( def __str__(self): return self.organization.name + ', ' + self.team_name + + + def save_history(self, before: dict, after: dict) -> bool: + + from access.models.team_history import TeamAuditHistory + + history = super().save_history( + before = before, + after = after, + history_model = TeamAuditHistory + ) + + + return history diff --git a/app/access/serializers/organization.py b/app/access/serializers/organization.py index 05d9380d..efd19f3e 100644 --- a/app/access/serializers/organization.py +++ b/app/access/serializers/organization.py @@ -68,7 +68,7 @@ class TenantModelSerializer( # 'model_id': item.pk # } # ), - 'teams': reverse("v2:_api_team-list", request=self._context['view'].request, kwargs={'organization_id': item.pk}), + 'teams': reverse("v2:_api_v2_organization_team-list", request=self._context['view'].request, kwargs={'organization_id': item.pk}), } model_notes = centurion_field.MarkdownField( required = False ) diff --git a/app/access/tests/functional/team/team_serializer.py b/app/access/tests/functional/team/test_team_serializer.py similarity index 100% rename from app/access/tests/functional/team/team_serializer.py rename to app/access/tests/functional/team/test_team_serializer.py diff --git a/app/access/tests/unit/team/team_api_v2.py b/app/access/tests/unit/team/test_team_api_v2.py similarity index 99% rename from app/access/tests/unit/team/team_api_v2.py rename to app/access/tests/unit/team/test_team_api_v2.py index 1c66e57b..0ac6e9d8 100644 --- a/app/access/tests/unit/team/team_api_v2.py +++ b/app/access/tests/unit/team/test_team_api_v2.py @@ -27,7 +27,7 @@ class TeamAPI( app_namespace = 'v2' - url_name = '_api_team' + url_name = '_api_v2_organization_team' @classmethod def setUpTestData(self): diff --git a/app/access/tests/unit/team/unit_team_model.py b/app/access/tests/unit/team/test_unit_team_model.py similarity index 100% rename from app/access/tests/unit/team/unit_team_model.py rename to app/access/tests/unit/team/test_unit_team_model.py diff --git a/app/access/tests/unit/team/unit_team_viewset.py b/app/access/tests/unit/team/test_unit_team_viewset.py similarity index 94% rename from app/access/tests/unit/team/unit_team_viewset.py rename to app/access/tests/unit/team/test_unit_team_viewset.py index d56df676..52140475 100644 --- a/app/access/tests/unit/team/unit_team_viewset.py +++ b/app/access/tests/unit/team/test_unit_team_viewset.py @@ -16,7 +16,7 @@ class TeamViewsetList( viewset = ViewSet - route_name = 'API:_api_team' + route_name = 'API:_api_v2_organization_team' @classmethod diff --git a/app/access/urls_api.py b/app/access/urls_api.py index 62601744..07bfc699 100644 --- a/app/access/urls_api.py +++ b/app/access/urls_api.py @@ -7,7 +7,7 @@ from access.viewsets import ( index as access_v2, organization as organization_v2, role, - team, + team as team_v2, team_user as team_user_v2 ) @@ -64,8 +64,8 @@ router.register( # ) router.register( - prefix = 'tenant/(?P[0-9]+)/team', viewset = team.ViewSet, - basename = '_api_team' + prefix = 'tenant/(?P[0-9]+)/team', viewset = team_v2.ViewSet, + basename = '_api_v2_organization_team' ) # router.register( diff --git a/app/assistance/serializers/knowledge_base.py b/app/assistance/serializers/knowledge_base.py index 47aa830b..bd9a4fb0 100644 --- a/app/assistance/serializers/knowledge_base.py +++ b/app/assistance/serializers/knowledge_base.py @@ -77,7 +77,7 @@ class KnowledgeBaseModelSerializer( request=self.context['view'].request, ), 'team': reverse( - 'v2:_api_team-list', + 'v2:_api_v2_organization_team-list', request=self.context['view'].request, kwargs={ 'organization_id': item.organization.id, diff --git a/app/assistance/serializers/knowledge_base_category.py b/app/assistance/serializers/knowledge_base_category.py index 00589095..b20c21b1 100644 --- a/app/assistance/serializers/knowledge_base_category.py +++ b/app/assistance/serializers/knowledge_base_category.py @@ -69,7 +69,7 @@ class KnowledgeBaseCategoryModelSerializer( request=self.context['view'].request, ), 'team': reverse( - 'v2:_api_team-list', + 'v2:_api_v2_organization_team-list', request=self.context['view'].request, kwargs={ 'organization_id': item.organization.id,