From 6c4b342bc7747a98cf7b98efc04531f227244137 Mon Sep 17 00:00:00 2001 From: Terje Kvernes Date: Mon, 2 Dec 2024 13:18:37 +0100 Subject: [PATCH] Inherit from BaseModel to get correct created_at/updated_at. --- ...tom_approvedmodelforpolicy_policyrole_and_more.py} | 11 +++++++---- mreg/models/policy.py | 11 +++++------ 2 files changed, 12 insertions(+), 10 deletions(-) rename mreg/migrations/{0014_policyatom_approvedmodel_policyrole_policyassignment.py => 0014_policyatom_approvedmodelforpolicy_policyrole_and_more.py} (85%) diff --git a/mreg/migrations/0014_policyatom_approvedmodel_policyrole_policyassignment.py b/mreg/migrations/0014_policyatom_approvedmodelforpolicy_policyrole_and_more.py similarity index 85% rename from mreg/migrations/0014_policyatom_approvedmodel_policyrole_policyassignment.py rename to mreg/migrations/0014_policyatom_approvedmodelforpolicy_policyrole_and_more.py index c40f8651..1ac55e4f 100644 --- a/mreg/migrations/0014_policyatom_approvedmodel_policyrole_policyassignment.py +++ b/mreg/migrations/0014_policyatom_approvedmodelforpolicy_policyrole_and_more.py @@ -1,6 +1,5 @@ -# Generated by Django 5.0.9 on 2024-11-28 11:39 +# Generated by Django 5.0.9 on 2024-12-02 12:17 -import datetime import django.db.models.deletion import mreg.fields import mreg.models.policy @@ -19,8 +18,8 @@ class Migration(migrations.Migration): name='PolicyAtom', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), - ('create_date', models.DateField(default=datetime.date.today)), ('description', models.CharField(max_length=150)), ('name', mreg.fields.LowerCaseCharField(max_length=64, unique=True, validators=[mreg.models.policy._validate_atom_name])), ], @@ -33,6 +32,8 @@ class Migration(migrations.Migration): name='ApprovedModelForPolicy', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), ], options={ @@ -43,8 +44,8 @@ class Migration(migrations.Migration): name='PolicyRole', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), - ('create_date', models.DateField(default=datetime.date.today)), ('description', models.CharField(max_length=150)), ('name', mreg.fields.LowerCaseCharField(max_length=64, unique=True, validators=[mreg.models.policy._validate_role_name])), ('atoms', models.ManyToManyField(related_name='roles', to='mreg.policyatom')), @@ -59,6 +60,8 @@ class Migration(migrations.Migration): name='PolicyAssignment', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), ('object_id', models.PositiveIntegerField()), ('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), ('policy_role', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='assignments', to='mreg.policyrole')), diff --git a/mreg/models/policy.py b/mreg/models/policy.py index 2161185f..03808cc1 100644 --- a/mreg/models/policy.py +++ b/mreg/models/policy.py @@ -1,4 +1,3 @@ -import datetime from abc import abstractmethod @@ -7,8 +6,10 @@ from django.core.exceptions import ValidationError from django.db import models + from structlog import get_logger +from mreg.models.base import BaseModel from mreg.fields import LowerCaseCharField from mreg.managers import LowerCaseManager from mreg.models.base import Label @@ -16,15 +17,13 @@ logger = get_logger() -class PolicyComponent(models.Model): +class PolicyComponent(BaseModel): """Abstract base class for policy components. This class provides common fields and methods for policy-related models. Subclasses are expected to define a 'name' field. """ - updated_at = models.DateTimeField(auto_now=True) - create_date = models.DateField(default=datetime.date.today) description = models.CharField(max_length=150) class Meta: @@ -93,7 +92,7 @@ class Meta: ordering = ("name",) -class ApprovedModelForPolicy(models.Model): +class ApprovedModelForPolicy(BaseModel): """ Model representing an approved model for policy assignments. @@ -112,7 +111,7 @@ class Meta: db_table = "approved_model_for_policy" -class PolicyAssignment(models.Model): +class PolicyAssignment(BaseModel): """ Model representing the assignment of a policy role to an object.