-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(access): Move models to their own file
ref: #
- Loading branch information
Showing
8 changed files
with
754 additions
and
706 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
from rest_framework.reverse import reverse | ||
|
||
from access.fields import ( | ||
AutoCreatedField, | ||
AutoLastModifiedField, | ||
AutoSlugField | ||
) | ||
|
||
from core.mixin.history_save import SaveHistory | ||
|
||
|
||
|
||
class Organization(SaveHistory): | ||
|
||
class Meta: | ||
verbose_name = "Organization" | ||
verbose_name_plural = "Organizations" | ||
ordering = ['name'] | ||
|
||
def save(self, *args, **kwargs): | ||
|
||
if self.slug == '_': | ||
self.slug = self.name.lower().replace(' ', '_') | ||
|
||
super().save(*args, **kwargs) | ||
|
||
id = models.AutoField( | ||
blank=False, | ||
help_text = 'ID of this item', | ||
primary_key=True, | ||
unique=True, | ||
verbose_name = 'ID' | ||
) | ||
|
||
name = models.CharField( | ||
blank = False, | ||
help_text = 'Name of this Organization', | ||
max_length = 50, | ||
unique = True, | ||
verbose_name = 'Name' | ||
) | ||
|
||
manager = models.ForeignKey( | ||
User, | ||
blank = False, | ||
help_text = 'Manager for this organization', | ||
null = True, | ||
on_delete=models.SET_NULL, | ||
verbose_name = 'Manager' | ||
) | ||
|
||
model_notes = models.TextField( | ||
blank = True, | ||
default = None, | ||
help_text = 'Tid bits of information', | ||
null= True, | ||
verbose_name = 'Notes', | ||
) | ||
|
||
slug = AutoSlugField() | ||
|
||
created = AutoCreatedField() | ||
|
||
modified = AutoLastModifiedField() | ||
|
||
|
||
def get_organization(self): | ||
return self | ||
|
||
def __int__(self): | ||
|
||
return self.id | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
table_fields: list = [ | ||
'nbsp', | ||
'name', | ||
'created', | ||
'modified', | ||
'nbsp' | ||
] | ||
|
||
page_layout: list = [ | ||
{ | ||
"name": "Details", | ||
"slug": "details", | ||
"sections": [ | ||
{ | ||
"layout": "double", | ||
"left": [ | ||
'name', | ||
'manager', | ||
'created', | ||
'modified', | ||
], | ||
"right": [ | ||
'model_notes', | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Teams", | ||
"slug": "teams", | ||
"sections": [ | ||
{ | ||
"layout": "table", | ||
"field": "teams" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Knowledge Base", | ||
"slug": "kb_articles", | ||
"sections": [ | ||
{ | ||
"layout": "table", | ||
"field": "knowledge_base", | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Notes", | ||
"slug": "notes", | ||
"sections": [] | ||
} | ||
] | ||
|
||
|
||
def get_url( self, request = None ) -> str: | ||
|
||
if request: | ||
|
||
return reverse("v2:_api_v2_organization-detail", request=request, kwargs={'pk': self.id}) | ||
|
||
return reverse("v2:_api_v2_organization-detail", kwargs={'pk': self.id}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.db import models | ||
|
||
from access.models.organization import Organization | ||
|
||
from core.models.model_notes import ModelNotes | ||
|
||
|
||
|
||
class OrganizationNotes( | ||
ModelNotes | ||
): | ||
|
||
|
||
class Meta: | ||
|
||
db_table = 'access_organization_notes' | ||
|
||
ordering = ModelNotes._meta.ordering | ||
|
||
verbose_name = 'Organization Note' | ||
|
||
verbose_name_plural = 'Organization Notes' | ||
|
||
|
||
model = models.ForeignKey( | ||
Organization, | ||
blank = False, | ||
help_text = 'Model this note belongs to', | ||
null = False, | ||
on_delete = models.CASCADE, | ||
related_name = 'notes', | ||
verbose_name = 'Model', | ||
) | ||
|
||
table_fields: list = [] | ||
|
||
page_layout: dict = [] | ||
|
||
|
||
def get_url_kwargs(self) -> dict: | ||
|
||
return { | ||
'model_id': self.model.pk, | ||
'pk': self.pk | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
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 access.models.organization import Organization | ||
from access.models.tenancy import TenancyObject | ||
|
||
from core import exceptions as centurion_exceptions | ||
|
||
|
||
class Team(Group, TenancyObject): | ||
|
||
class Meta: | ||
|
||
ordering = [ 'team_name' ] | ||
|
||
verbose_name = 'Team' | ||
|
||
verbose_name_plural = "Teams" | ||
|
||
|
||
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): | ||
|
||
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', | ||
max_length = 50, | ||
unique = False, | ||
verbose_name = 'Name', | ||
) | ||
|
||
organization = models.ForeignKey( | ||
Organization, | ||
blank = False, | ||
help_text = 'Organization this belongs to', | ||
null = False, | ||
on_delete = models.CASCADE, | ||
validators = [validatate_organization_exists], | ||
verbose_name = 'Organization' | ||
) | ||
|
||
created = AutoCreatedField() | ||
|
||
modified = AutoLastModifiedField() | ||
|
||
page_layout: dict = [ | ||
{ | ||
"name": "Details", | ||
"slug": "details", | ||
"sections": [ | ||
{ | ||
"layout": "double", | ||
"left": [ | ||
'organization', | ||
'team_name', | ||
'created', | ||
'modified', | ||
], | ||
"right": [ | ||
'model_notes', | ||
] | ||
}, | ||
{ | ||
"layout": "table", | ||
"name": "Users", | ||
"field": "users", | ||
}, | ||
] | ||
}, | ||
{ | ||
"name": "Knowledge Base", | ||
"slug": "kb_articles", | ||
"sections": [ | ||
{ | ||
"layout": "table", | ||
"field": "knowledge_base", | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "Notes", | ||
"slug": "notes", | ||
"sections": [] | ||
}, | ||
] | ||
|
||
table_fields: list = [ | ||
'team_name', | ||
'modified', | ||
'created', | ||
] | ||
|
||
|
||
def get_url( self, request = None ) -> str: | ||
|
||
if request: | ||
|
||
return reverse(f"v2:_api_v2_organization_team-detail", request=request, kwargs = self.get_url_kwargs() ) | ||
|
||
return reverse(f"v2:_api_v2_organization_team-detail", kwargs = self.get_url_kwargs() ) | ||
|
||
|
||
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 | ||
} | ||
|
||
|
||
@property | ||
def parent_object(self): | ||
""" Fetch the parent object """ | ||
|
||
return self.organization | ||
|
||
|
||
def permission_list(self) -> list: | ||
|
||
permission_list = [] | ||
|
||
for permission in self.permissions.all(): | ||
|
||
if str(permission.content_type.app_label + '.' + permission.codename) in permission_list: | ||
continue | ||
|
||
permission_list += [ str(permission.content_type.app_label + '.' + permission.codename) ] | ||
|
||
return [permission_list, self.permissions.all()] | ||
|
||
|
||
def __str__(self): | ||
return self.organization.name + ', ' + self.team_name |
Oops, something went wrong.