@ -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
|
||||
|
@ -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 )
|
||||
|
@ -27,7 +27,7 @@ class TeamAPI(
|
||||
|
||||
app_namespace = 'v2'
|
||||
|
||||
url_name = '_api_team'
|
||||
url_name = '_api_v2_organization_team'
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(self):
|
@ -16,7 +16,7 @@ class TeamViewsetList(
|
||||
|
||||
viewset = ViewSet
|
||||
|
||||
route_name = 'API:_api_team'
|
||||
route_name = 'API:_api_v2_organization_team'
|
||||
|
||||
|
||||
@classmethod
|
@ -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<organization_id>[0-9]+)/team', viewset = team.ViewSet,
|
||||
basename = '_api_team'
|
||||
prefix = 'tenant/(?P<organization_id>[0-9]+)/team', viewset = team_v2.ViewSet,
|
||||
basename = '_api_v2_organization_team'
|
||||
)
|
||||
|
||||
# router.register(
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user