feat(config_management): switch model ConfigGroupSoftware to inheirt from CenturionModel

ref: #789 #795
This commit is contained in:
2025-06-06 12:19:44 +09:30
parent cff3bc5b2c
commit fc60d7f3be
5 changed files with 273 additions and 31 deletions

View File

@ -0,0 +1,122 @@
# Generated by Django 5.1.9 on 2025-06-06 02:42
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", "0009_remove_configgroups_is_global_and_more"),
("core", "0028_delete_history"),
]
operations = [
migrations.RemoveField(
model_name="configgrouphosts",
name="is_global",
),
migrations.AlterField(
model_name="configgrouphosts",
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="configgrouphosts",
name="model_notes",
field=models.TextField(
blank=True,
help_text="Tid bits of information",
null=True,
verbose_name="Notes",
),
),
migrations.AlterField(
model_name="configgrouphosts",
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="ConfigGroupHostsAuditHistory",
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.configgrouphosts",
verbose_name="Model",
),
),
],
options={
"verbose_name": "config group hosts History",
"verbose_name_plural": "config group hosts Histories",
"db_table": "config_management_configgrouphosts_audithistory",
"managed": True,
},
bases=("core.centurionaudit", models.Model),
),
migrations.CreateModel(
name="ConfigGroupHostsCenturionModelNote",
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.configgrouphosts",
verbose_name="Model",
),
),
],
options={
"verbose_name": "config group hosts Note",
"verbose_name_plural": "config group hosts Notes",
"db_table": "config_management_configgrouphosts_centurionmodelnote",
"managed": True,
},
bases=("core.centurionmodelnote", models.Model),
)
]

View File

@ -391,7 +391,9 @@ class ConfigGroups(
class ConfigGroupSoftware(GroupsCommonFields, SaveHistory):
class ConfigGroupSoftware(
CenturionModel,
):
""" A way to configure software to install/remove per config group """
class Meta:
@ -447,6 +449,8 @@ class ConfigGroupSoftware(GroupsCommonFields, SaveHistory):
verbose_name = 'Verrsion',
)
modified = AutoLastModifiedField()
# This model is not intended to be viewable on it's own page
# as it's a sub model for config groups
page_layout: dict = []
@ -460,35 +464,8 @@ class ConfigGroupSoftware(GroupsCommonFields, SaveHistory):
]
def get_url_kwargs(self) -> dict:
return {
'config_group_id': self.config_group.id,
'pk': self.id
}
def get_url_kwargs_notes(self):
return FeatureNotUsed
@property
def parent_object(self):
""" Fetch the parent object """
return self.config_group
def save_history(self, before: dict, after: dict) -> bool:
from config_management.models.config_groups_software_history import ConfigGroupSoftwareHistory
history = super().save_history(
before = before,
after = after,
history_model = ConfigGroupSoftwareHistory,
)
return history

View File

@ -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 ConfigGroupSoftwareAuditHistory # 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 = 'ConfigGroupSoftwareAuditHistoryModelSerializer')
class ModelSerializer(
common.CommonModelSerializer,
BaseSerializer
):
"""Git Group Audit History Base Model"""
_urls = serializers.SerializerMethodField('get_url')
class Meta:
model = ConfigGroupSoftwareAuditHistory
fields = [
'id',
'organization',
'display_name',
'content_type',
'model',
'before',
'after',
'action',
'user',
'created',
'_urls',
]
read_only_fields = fields
@extend_schema_serializer(component_name = 'ConfigGroupSoftwareAuditHistoryViewSerializer')
class ViewSerializer(
ModelSerializer,
AuditHistoryViewSerializer,
):
"""Git Group Audit History Base View Model"""
pass

View File

@ -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 ConfigGroupSoftwareCenturionModelNote # 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 = 'ConfigGroupSoftwareModelNoteModelSerializer')
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 = ConfigGroupSoftwareCenturionModelNote
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 = 'ConfigGroupSoftwareModelNoteViewSerializer')
class ViewSerializer(
ModelSerializer,
BaseModelViewSerializer,
):
organization = TenantBaseSerializer( many = False, read_only = True )

View File

@ -23,12 +23,12 @@ router.register(
)
router.register(
prefix = 'group/(?P<parent_group>[0-9]+)/child_group', viewset = config_group_v2.ViewSet,
basename = '_api_v2_config_group_child'
basename = '_api_configgroups_child'
)
router.register(
prefix = 'group/(?P<config_group_id>[0-9]+)/software',
viewset = config_group_software_v2.ViewSet,
basename = '_api_v2_config_group_software'
basename = '_api_configgroupsoftware'
)