8356 action_object_type cleanup migration

This commit is contained in:
Arthur 2023-11-09 10:37:06 -08:00
parent a679740626
commit 73047c979b
3 changed files with 17 additions and 23 deletions

View File

@ -254,7 +254,7 @@ class EventRuleForm(NetBoxModelForm):
(_('EventRule'), ('name', 'content_types', 'enabled', 'tags')),
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
(_('Conditions'), ('conditions',)),
(_('Action'), ('action_type', 'action_choice', 'parameters')),
(_('Action'), ('action_type', 'action_choice', 'parameters', 'action_object_type', 'action_object_id', 'action_object_identifier')),
)
class Meta:
@ -270,6 +270,9 @@ class EventRuleForm(NetBoxModelForm):
widgets = {
'conditions': forms.Textarea(attrs={'class': 'font-monospace'}),
'action_type': HTMXSelect(),
'action_object_type': forms.HiddenInput,
'action_object_id': forms.HiddenInput,
'action_object_identifier': forms.HiddenInput,
}
def get_script_choices(self):
@ -278,13 +281,14 @@ class EventRuleForm(NetBoxModelForm):
for module in ScriptModule.objects.all():
scripts = []
for script_name in module.scripts.keys():
name = f"{str(module).lower()}:{script_name.lower()}"
name = f"{str(module.pk)}:{script_name.lower()}"
scripts.append((name, script_name))
if scripts:
choices.append((str(module), scripts))
self.fields['action_choice'].choices = choices
self.fields['action_choice'].initial = get_field_value(self, 'action_object_identifier')
def get_webhook_choices(self):
self.fields['action_choice'] = DynamicModelChoiceField(

View File

@ -26,8 +26,8 @@ def move_webhooks(apps, schema_editor):
event.conditions = webhook.conditions
event.action_type = EventRuleActionChoices.WEBHOOK
event.object_type_id = ContentType.objects.get_for_model(webhook).id
event.object_id = webhook.id
event.action_object_type_id = ContentType.objects.get_for_model(webhook).id
event.action_object_id = webhook.id
event.save()
event.content_types.add(*webhook.content_types.all())
@ -58,7 +58,7 @@ class Migration(migrations.Migration):
('enabled', models.BooleanField(default=True)),
('conditions', models.JSONField(blank=True, null=True)),
('action_type', models.CharField(default='webhook', max_length=30)),
('object_id', models.PositiveBigIntegerField(blank=True, null=True)),
('action_object_id', models.PositiveBigIntegerField(blank=True, null=True)),
(
'content_types',
models.ManyToManyField(
@ -68,15 +68,14 @@ class Migration(migrations.Migration):
),
),
(
'object_type',
'action_object_type',
models.ForeignKey(
limit_choices_to=models.Q(('app_label', 'extras'), ('model__in', ('webhook', 'script'))),
on_delete=django.db.models.deletion.CASCADE,
related_name='eventrule_actions',
to='contenttypes.contenttype',
),
),
('object_identifier', models.CharField(max_length=80, blank=True)),
('action_object_identifier', models.CharField(max_length=80, blank=True)),
('parameters', models.JSONField(blank=True, null=True)),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
],
@ -123,13 +122,4 @@ class Migration(migrations.Migration):
model_name='webhook',
name='type_update',
),
migrations.AlterField(
model_name='eventrule',
name='object_type',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name='eventrule_actions',
to='contenttypes.contenttype',
),
),
]

View File

@ -100,22 +100,22 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
default=EventRuleActionChoices.WEBHOOK,
verbose_name=_('event type')
)
object_type = models.ForeignKey(
action_object_type = models.ForeignKey(
to=ContentType,
related_name='eventrule_actions',
on_delete=models.CASCADE,
)
object_id = models.PositiveBigIntegerField(
action_object_id = models.PositiveBigIntegerField(
blank=True,
null=True
)
object = GenericForeignKey(
ct_field='object_type',
fk_field='object_id',
action_object = GenericForeignKey(
ct_field='action_object_type',
fk_field='action_object_id',
)
# internal (not show in UI) - used by scripts to store function name
object_identifier = models.CharField(
action_object_identifier = models.CharField(
max_length=80,
blank=True
)