mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 21:18:16 -06:00
14438 fix migration
This commit is contained in:
parent
4fdce65e35
commit
63d3ce6e77
@ -4,6 +4,31 @@ from django.db import migrations, models
|
|||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
def update_event_rules(apps, schema_editor):
|
||||||
|
from extras.models import ScriptModule
|
||||||
|
Script = apps.get_model('extras', 'Script')
|
||||||
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||||
|
ct = ContentType.objects.filter(app_label='extras', model='script').first()
|
||||||
|
|
||||||
|
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')
|
||||||
|
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()
|
||||||
|
|
||||||
|
rule.action_object_type = ct
|
||||||
|
rule.action_object_id = obj.id
|
||||||
|
rule.save()
|
||||||
|
|
||||||
|
|
||||||
def update_scripts(apps, schema_editor):
|
def update_scripts(apps, schema_editor):
|
||||||
from extras.models import ScriptModule
|
from extras.models import ScriptModule
|
||||||
ScriptModuleNew = apps.get_model('extras', 'ScriptModule')
|
ScriptModuleNew = apps.get_model('extras', 'ScriptModule')
|
||||||
@ -22,24 +47,6 @@ def update_scripts(apps, schema_editor):
|
|||||||
if ct:
|
if ct:
|
||||||
module.jobs.filter(name=script).update(object_type=ct, object_id=obj.id)
|
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')
|
|
||||||
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()
|
|
||||||
|
|
||||||
rule.action_object_type = ct
|
|
||||||
rule.action_object_id = obj.id
|
|
||||||
rule.save()
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
@ -72,8 +79,18 @@ class Migration(migrations.Migration):
|
|||||||
code=update_scripts,
|
code=update_scripts,
|
||||||
reverse_code=migrations.RunPython.noop
|
reverse_code=migrations.RunPython.noop
|
||||||
),
|
),
|
||||||
migrations.RemoveField(
|
migrations.SeparateDatabaseAndState(
|
||||||
model_name='eventrule',
|
database_operations=[
|
||||||
name='action_parameters',
|
migrations.RunPython(
|
||||||
|
code=update_event_rules,
|
||||||
|
reverse_code=migrations.RunPython.noop
|
||||||
|
),
|
||||||
|
],
|
||||||
|
state_operations=[
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='eventrule',
|
||||||
|
name='action_parameters',
|
||||||
|
),
|
||||||
|
]
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user