feat(config_management): switch model ConfigGroups to inheirt from CenturionModel
ref: #789 #795
This commit is contained in:
@ -0,0 +1,122 @@
|
|||||||
|
# Generated by Django 5.1.9 on 2025-06-06 02:40
|
||||||
|
|
||||||
|
import access.models.tenancy_abstract
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("access", "0012_teamusers_model_notes_alter_teamusers_id_and_more"),
|
||||||
|
("config_management", "0008_alter_configgrouphosts_organization_and_more"),
|
||||||
|
("core", "0028_delete_history"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name="configgroups",
|
||||||
|
name="is_global",
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="configgroups",
|
||||||
|
name="id",
|
||||||
|
field=models.AutoField(
|
||||||
|
help_text="ID of the item",
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
unique=True,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="configgroups",
|
||||||
|
name="model_notes",
|
||||||
|
field=models.TextField(
|
||||||
|
blank=True,
|
||||||
|
help_text="Tid bits of information",
|
||||||
|
null=True,
|
||||||
|
verbose_name="Notes",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="configgroups",
|
||||||
|
name="organization",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
help_text="Tenant this belongs to",
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="+",
|
||||||
|
to="access.tenant",
|
||||||
|
validators=[
|
||||||
|
access.models.tenancy_abstract.TenancyAbstractModel.validatate_organization_exists
|
||||||
|
],
|
||||||
|
verbose_name="Tenant",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="ConfigGroupsAuditHistory",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"centurionaudit_ptr",
|
||||||
|
models.OneToOneField(
|
||||||
|
auto_created=True,
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
parent_link=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
to="core.centurionaudit",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"model",
|
||||||
|
models.ForeignKey(
|
||||||
|
help_text="Model this history belongs to",
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="audit_history",
|
||||||
|
to="config_management.configgroups",
|
||||||
|
verbose_name="Model",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "Config Group History",
|
||||||
|
"verbose_name_plural": "Config Group Histories",
|
||||||
|
"db_table": "config_management_configgroups_audithistory",
|
||||||
|
"managed": True,
|
||||||
|
},
|
||||||
|
bases=("core.centurionaudit", models.Model),
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="ConfigGroupsCenturionModelNote",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"centurionmodelnote_ptr",
|
||||||
|
models.OneToOneField(
|
||||||
|
auto_created=True,
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
parent_link=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
to="core.centurionmodelnote",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"model",
|
||||||
|
models.ForeignKey(
|
||||||
|
help_text="Model this note belongs to",
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="+",
|
||||||
|
to="config_management.configgroups",
|
||||||
|
verbose_name="Model",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
"verbose_name": "Config Group Note",
|
||||||
|
"verbose_name_plural": "Config Group Notes",
|
||||||
|
"db_table": "config_management_configgroups_centurionmodelnote",
|
||||||
|
"managed": True,
|
||||||
|
},
|
||||||
|
bases=("core.centurionmodelnote", models.Model),
|
||||||
|
),
|
||||||
|
]
|
@ -1,46 +1,24 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
from django.core.exceptions import (
|
||||||
|
ValidationError
|
||||||
|
)
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models.signals import post_delete
|
|
||||||
from django.dispatch import receiver
|
|
||||||
from django.forms import ValidationError
|
|
||||||
|
|
||||||
from rest_framework.reverse import reverse
|
from access.fields import AutoLastModifiedField
|
||||||
|
|
||||||
from access.fields import *
|
|
||||||
from access.models.tenancy import TenancyObject
|
|
||||||
|
|
||||||
from centurion.helpers.merge_software import merge_software
|
from centurion.helpers.merge_software import merge_software
|
||||||
|
|
||||||
from core.lib.feature_not_used import FeatureNotUsed
|
from core.models.centurion import CenturionModel
|
||||||
from core.mixin.history_save import SaveHistory
|
|
||||||
from core.signal.ticket_linked_item_delete import TicketLinkedItem, deleted_model
|
|
||||||
|
|
||||||
from itam.models.device import Device, DeviceSoftware
|
from itam.models.device import Device, DeviceSoftware
|
||||||
from itam.models.software import Software, SoftwareVersion
|
from itam.models.software import Software, SoftwareVersion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GroupsCommonFields(TenancyObject, models.Model):
|
class ConfigGroups(
|
||||||
|
CenturionModel,
|
||||||
class Meta:
|
):
|
||||||
abstract = True
|
|
||||||
|
|
||||||
id = models.AutoField(
|
|
||||||
blank=False,
|
|
||||||
help_text = 'ID of this Group',
|
|
||||||
primary_key=True,
|
|
||||||
unique=True,
|
|
||||||
verbose_name = 'ID'
|
|
||||||
)
|
|
||||||
|
|
||||||
created = AutoCreatedField()
|
|
||||||
|
|
||||||
modified = AutoLastModifiedField()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|
||||||
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -68,7 +46,10 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
for invalid_key in ConfigGroups.reserved_config_keys:
|
for invalid_key in ConfigGroups.reserved_config_keys:
|
||||||
|
|
||||||
if invalid_key in value.keys():
|
if invalid_key in value.keys():
|
||||||
raise ValidationError(f'json key "{invalid_key}" is a reserved configuration key')
|
|
||||||
|
raise ValidationError(
|
||||||
|
message = f'json key "{invalid_key}" is a reserved configuration key'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
parent = models.ForeignKey(
|
parent = models.ForeignKey(
|
||||||
@ -107,6 +88,8 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
verbose_name = 'Hosts'
|
verbose_name = 'Hosts'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
modified = AutoLastModifiedField()
|
||||||
|
|
||||||
|
|
||||||
page_layout: dict = [
|
page_layout: dict = [
|
||||||
{
|
{
|
||||||
@ -213,6 +196,36 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def clean_fields(self, exclude = None):
|
||||||
|
|
||||||
|
if self.config:
|
||||||
|
|
||||||
|
self.config = self.config_keys_ansible_variable(self.config)
|
||||||
|
|
||||||
|
if self.parent:
|
||||||
|
self.organization = ConfigGroups.objects.get(id=self.parent.id).organization
|
||||||
|
|
||||||
|
if self.pk:
|
||||||
|
|
||||||
|
obj = ConfigGroups.objects.get(
|
||||||
|
id = self.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Prevent organization change. ToDo: add feature so that config can change organizations
|
||||||
|
self.organization = obj.organization
|
||||||
|
|
||||||
|
if self.parent is not None:
|
||||||
|
|
||||||
|
if self.pk == self.parent.pk:
|
||||||
|
|
||||||
|
raise ValidationError('Can not set self as parent')
|
||||||
|
|
||||||
|
|
||||||
|
super().clean_fields(exclude = exclude)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def config_keys_ansible_variable(self, value: dict):
|
def config_keys_ansible_variable(self, value: dict):
|
||||||
|
|
||||||
clean_value = {}
|
clean_value = {}
|
||||||
@ -254,21 +267,6 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
def get_url( self, request = None ) -> str:
|
|
||||||
|
|
||||||
if request:
|
|
||||||
|
|
||||||
return reverse("v2:_api_v2_config_group-detail", request=request, kwargs={'pk': self.id})
|
|
||||||
|
|
||||||
return reverse("v2:_api_v2_config_group-detail", kwargs={'pk': self.id})
|
|
||||||
|
|
||||||
|
|
||||||
# @property
|
|
||||||
# def parent_object(self):
|
|
||||||
# """ Fetch the parent object """
|
|
||||||
|
|
||||||
# return self.parent
|
|
||||||
|
|
||||||
|
|
||||||
def render_config(self):
|
def render_config(self):
|
||||||
|
|
||||||
@ -323,32 +321,6 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
|
|
||||||
if self.config:
|
|
||||||
|
|
||||||
self.config = self.config_keys_ansible_variable(self.config)
|
|
||||||
|
|
||||||
if self.parent:
|
|
||||||
self.organization = ConfigGroups.objects.get(id=self.parent.id).organization
|
|
||||||
|
|
||||||
if self.pk:
|
|
||||||
|
|
||||||
obj = ConfigGroups.objects.get(
|
|
||||||
id = self.id,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Prevent organization change. ToDo: add feature so that config can change organizations
|
|
||||||
self.organization = obj.organization
|
|
||||||
|
|
||||||
if self.parent is not None:
|
|
||||||
|
|
||||||
if self.pk == self.parent.pk:
|
|
||||||
|
|
||||||
raise ValidationError('Can not set self as parent')
|
|
||||||
|
|
||||||
super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
||||||
@ -359,29 +331,7 @@ class ConfigGroups(GroupsCommonFields, SaveHistory):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
def save_history(self, before: dict, after: dict) -> bool:
|
|
||||||
|
|
||||||
from config_management.models.config_groups_history import ConfigGroupsHistory
|
|
||||||
|
|
||||||
history = super().save_history(
|
|
||||||
before = before,
|
|
||||||
after = after,
|
|
||||||
history_model = ConfigGroupsHistory,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
return history
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_delete, sender=ConfigGroups, dispatch_uid='config_group_delete_signal')
|
|
||||||
def signal_deleted_model(sender, instance, using, **kwargs):
|
|
||||||
|
|
||||||
deleted_model.send(sender='config_group_deleted', item_id=instance.id, item_type = TicketLinkedItem.Modules.CONFIG_GROUP)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigGroupHosts(GroupsCommonFields, SaveHistory):
|
|
||||||
|
|
||||||
|
|
||||||
def validate_host_no_parent_group(self):
|
def validate_host_no_parent_group(self):
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from drf_spectacular.utils import extend_schema_serializer
|
||||||
|
|
||||||
|
from api.serializers import common
|
||||||
|
|
||||||
|
from centurion.models.meta import ConfigGroupsAuditHistory # pylint: disable=E0401:import-error disable=E0611:no-name-in-module
|
||||||
|
|
||||||
|
from core.serializers.centurionaudit import (
|
||||||
|
BaseSerializer,
|
||||||
|
ViewSerializer as AuditHistoryViewSerializer
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@extend_schema_serializer(component_name = 'ConfigGroupsAuditHistoryModelSerializer')
|
||||||
|
class ModelSerializer(
|
||||||
|
common.CommonModelSerializer,
|
||||||
|
BaseSerializer
|
||||||
|
):
|
||||||
|
"""Git Group Audit History Base Model"""
|
||||||
|
|
||||||
|
|
||||||
|
_urls = serializers.SerializerMethodField('get_url')
|
||||||
|
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
model = ConfigGroupsAuditHistory
|
||||||
|
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'organization',
|
||||||
|
'display_name',
|
||||||
|
'content_type',
|
||||||
|
'model',
|
||||||
|
'before',
|
||||||
|
'after',
|
||||||
|
'action',
|
||||||
|
'user',
|
||||||
|
'created',
|
||||||
|
'_urls',
|
||||||
|
]
|
||||||
|
|
||||||
|
read_only_fields = fields
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@extend_schema_serializer(component_name = 'ConfigGroupsAuditHistoryViewSerializer')
|
||||||
|
class ViewSerializer(
|
||||||
|
ModelSerializer,
|
||||||
|
AuditHistoryViewSerializer,
|
||||||
|
):
|
||||||
|
"""Git Group Audit History Base View Model"""
|
||||||
|
pass
|
@ -0,0 +1,87 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from drf_spectacular.utils import extend_schema_serializer
|
||||||
|
|
||||||
|
from access.serializers.organization import (TenantBaseSerializer)
|
||||||
|
|
||||||
|
from centurion.models.meta import ConfigGroupsCenturionModelNote # pylint: disable=E0401:import-error disable=E0611:no-name-in-module
|
||||||
|
|
||||||
|
from core.serializers.centurionmodelnote import ( # pylint: disable=W0611:unused-import
|
||||||
|
BaseSerializer,
|
||||||
|
ModelSerializer as BaseModelModelSerializer,
|
||||||
|
ViewSerializer as BaseModelViewSerializer
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@extend_schema_serializer(component_name = 'ConfigGroupsModelNoteModelSerializer')
|
||||||
|
class ModelSerializer(
|
||||||
|
BaseModelModelSerializer,
|
||||||
|
):
|
||||||
|
|
||||||
|
|
||||||
|
_urls = serializers.SerializerMethodField('get_url')
|
||||||
|
|
||||||
|
def get_url(self, item) -> dict:
|
||||||
|
|
||||||
|
return {
|
||||||
|
'_self': item.get_url( request = self._context['view'].request ),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
model = ConfigGroupsCenturionModelNote
|
||||||
|
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'organization',
|
||||||
|
'display_name',
|
||||||
|
'body',
|
||||||
|
'created_by',
|
||||||
|
'modified_by',
|
||||||
|
'content_type',
|
||||||
|
'model',
|
||||||
|
'created',
|
||||||
|
'modified',
|
||||||
|
'_urls',
|
||||||
|
]
|
||||||
|
|
||||||
|
read_only_fields = [
|
||||||
|
'id',
|
||||||
|
'display_name',
|
||||||
|
'organization',
|
||||||
|
'created_by',
|
||||||
|
'modified_by',
|
||||||
|
'content_type',
|
||||||
|
'model',
|
||||||
|
'created',
|
||||||
|
'modified',
|
||||||
|
'_urls',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
|
||||||
|
is_valid = False
|
||||||
|
|
||||||
|
note_model = self.Meta.model.model.field.related_model
|
||||||
|
|
||||||
|
attrs['model'] = note_model.objects.get(
|
||||||
|
id = int( self.context['view'].kwargs['model_id'] )
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
is_valid = super().validate(attrs)
|
||||||
|
|
||||||
|
return is_valid
|
||||||
|
|
||||||
|
|
||||||
|
@extend_schema_serializer(component_name = 'ConfigGroupsModelNoteViewSerializer')
|
||||||
|
class ViewSerializer(
|
||||||
|
ModelSerializer,
|
||||||
|
BaseModelViewSerializer,
|
||||||
|
):
|
||||||
|
|
||||||
|
organization = TenantBaseSerializer( many = False, read_only = True )
|
@ -19,7 +19,7 @@ router.register(
|
|||||||
)
|
)
|
||||||
router.register(
|
router.register(
|
||||||
prefix = 'group', viewset = config_group_v2.ViewSet,
|
prefix = 'group', viewset = config_group_v2.ViewSet,
|
||||||
basename = '_api_v2_config_group'
|
basename = '_api_configgroups'
|
||||||
)
|
)
|
||||||
router.register(
|
router.register(
|
||||||
prefix = 'group/(?P<parent_group>[0-9]+)/child_group', viewset = config_group_v2.ViewSet,
|
prefix = 'group/(?P<parent_group>[0-9]+)/child_group', viewset = config_group_v2.ViewSet,
|
||||||
|
Reference in New Issue
Block a user