14438 update migration for event rules

This commit is contained in:
Arthur 2024-02-12 09:10:34 -08:00
parent 0100857e92
commit 1a6099fce9
2 changed files with 16 additions and 13 deletions

View File

@ -1,6 +1,5 @@
# Generated by Django 4.2.9 on 2024-02-05 21:37
from django.contrib.contenttypes.models import ContentType
from django.db import migrations, models
import django.db.models.deletion
@ -9,7 +8,7 @@ def update_scripts(apps, schema_editor):
from extras.models import ScriptModule
ScriptModuleNew = apps.get_model('extras', 'ScriptModule')
Script = apps.get_model('extras', 'Script')
# ContentType = apps.get_model('contenttypes', 'ContentType')
ContentType = apps.get_model('contenttypes', 'ContentType')
ct = ContentType.objects.filter(app_label='extras', model='script').first()
for module in ScriptModule.objects.all():
@ -20,18 +19,22 @@ def update_scripts(apps, schema_editor):
)
# update all jobs associated with this module/name to point to the new script obj
module.jobs.filter(name=script).update(object_type=ct, object_id=obj.id)
if ct:
module.jobs.filter(name=script).update(object_type=ct, object_id=obj.id)
EventRule = apps.get_model('extras', 'EventRule')
ct_script_module = ContentType.objects.filter(app_label='extras', model='scriptmodule').first()
for rule in EventRule.objects.filter(action_object_type=ct_script_module):
name = rule.action_parameters.get('script_name')
if name:
obj = Script.objects.filter(module=rule.action_object, name=name).first()
if obj:
rule.action_paramter = obj
rule.action_parameters.pop(name, None)
rule.save()
obj, created = Script.objects.get_or_create(
module_id=rule.action_object_id,
name=name,
)
if created:
# script in action_parameters was deleted?
obj.is_valid = False
obj.save()
class Migration(migrations.Migration):
@ -65,4 +68,8 @@ class Migration(migrations.Migration):
code=update_scripts,
reverse_code=migrations.RunPython.noop
),
migrations.RemoveField(
model_name='eventrule',
name='action_parameters',
),
]

View File

@ -115,10 +115,6 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
ct_field='action_object_type',
fk_field='action_object_id'
)
action_parameters = models.JSONField(
blank=True,
null=True
)
action_data = models.JSONField(
verbose_name=_('data'),
blank=True,