1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#14132: Fix migration

This commit is contained in:
Jeremy Stretch
2023-12-04 10:21:37 -05:00
parent 625825d482
commit cfc20f910e

View File

@ -11,9 +11,11 @@ def move_webhooks(apps, schema_editor):
Webhook = apps.get_model("extras", "Webhook")
EventRule = apps.get_model("extras", "EventRule")
webhook_ct = ContentType.objects.get_for_model(Webhook).pk
for webhook in Webhook.objects.all():
event = EventRule()
# Replicate attributes from Webhook instance
event.name = webhook.name
event.type_create = webhook.type_create
event.type_update = webhook.type_update
@ -24,7 +26,7 @@ def move_webhooks(apps, schema_editor):
event.conditions = webhook.conditions
event.action_type = EventRuleActionChoices.WEBHOOK
event.action_object_type_id = ContentType.objects.get_for_model(webhook).id
event.action_object_type_id = webhook_ct
event.action_object_id = webhook.id
event.save()
event.content_types.add(*webhook.content_types.all())
@ -37,6 +39,8 @@ class Migration(migrations.Migration):
]
operations = [
# Create the EventRule model
migrations.CreateModel(
name='EventRule',
fields=[
@ -68,7 +72,30 @@ class Migration(migrations.Migration):
'ordering': ('name',),
},
),
migrations.AddField(
model_name='eventrule',
name='action_object_type',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='eventrule_actions',
to='contenttypes.contenttype',
),
),
migrations.AddField(
model_name='eventrule',
name='content_types',
field=models.ManyToManyField(related_name='eventrules', to='contenttypes.contenttype'),
),
migrations.AddField(
model_name='eventrule',
name='tags',
field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
),
# Replicate Webhook data
migrations.RunPython(move_webhooks),
# Remove obsolete fields from Webhook
migrations.RemoveConstraint(
model_name='webhook',
name='extras_webhook_unique_payload_url_types',
@ -105,25 +132,8 @@ class Migration(migrations.Migration):
model_name='webhook',
name='type_update',
),
migrations.AddField(
model_name='eventrule',
name='action_object_type',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='eventrule_actions',
to='contenttypes.contenttype',
),
),
migrations.AddField(
model_name='eventrule',
name='content_types',
field=models.ManyToManyField(related_name='eventrules', to='contenttypes.contenttype'),
),
migrations.AddField(
model_name='eventrule',
name='tags',
field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
),
# Add description field to Webhook
migrations.AddField(
model_name='webhook',
name='description',