feat(core): Meta Model for CenturionModelNotes

ref: #779 #778
This commit is contained in:
2025-05-30 08:03:45 +09:30
parent 695bfa7f51
commit 62e7eb9c47
2 changed files with 72 additions and 6 deletions

View File

@ -1,9 +1,15 @@
from django.conf import settings
from django.contrib.auth.models import ContentType
from django.core.exceptions import (
ValidationError
)
from django.db import models
from access.fields import AutoLastModifiedField
from core.models.centurion import CenturionModel
from core.models.centurion import (
CenturionModel,
CenturionSubModel,
)
@ -12,6 +18,13 @@ class CenturionModelNote(
):
""" Base Centurion Notes Model"""
_audit_enabled = False
@property
def url_model_name(self):
return CenturionModelNote._meta.model_name
class Meta:
@ -77,3 +90,58 @@ class CenturionModelNote(
# This model is not expected to be viewable in a table
# as it's a sub-model
table_fields: list = []
class NoteAuditMetaModel(
CenturionModelNote,
CenturionSubModel,
):
model_notes = None
class Meta:
abstract = True
proxy = False
def clean_fields(self, exclude = None):
if not getattr(self, 'model', None):
raise ValidationError(
code = 'no_model_supplied',
message = 'Unable to process the history, no model was supplied.'
)
self.organization = self.model.organization
if not self.id:
self.created_by = self.context['user']
else:
self.modified_by = self.context['user']
self.content_type = ContentType.objects.get(
app_label = self.model._meta.app_label,
model = self.model._meta.model_name
)
super().clean_fields(exclude = exclude)
def get_url_kwargs(self):
kwargs = {}
kwargs.update({
**super().get_url_kwargs(),
'model_name': str(self._meta.model_name).replace('centurionmodelnote', ''),
})
return kwargs

View File

@ -60,10 +60,8 @@ if apps.models_ready:
if getattr(model, '_notes_enabled', False):
# continue
# audit_meta_name = model().get_history_model_name()
notes_meta_name = f'{model._meta.object_name}CenturionNote'
notes_meta_name = f'{model._meta.object_name}CenturionModelNote'
if notes_meta_name in existing_models:
continue
@ -71,14 +69,14 @@ if apps.models_ready:
NotesMetaModel = type(
notes_meta_name,
( import_string("core.models.centurion_notes.CenturionNotesMetaModel"), ),
( import_string("core.models.centurion_notes.NoteAuditMetaModel"), ),
{
'__module__': module_path,
'__qualname__': notes_meta_name,
'__doc__': f'Auto-generated meta model for {name} Notes.',
'Meta': type('Meta', (), {
'app_label': model._meta.app_label,
'db_table': model._meta.db_table + '_centurionnotes',
'db_table': model._meta.db_table + '_centurionmodelnote',
'managed': True,
'verbose_name': model._meta.verbose_name + ' Note',
'verbose_name_plural': model._meta.verbose_name + ' Notes',