Sfoglia il codice sorgente

Prevent duplicate contact assignments

jeremystretch 4 anni fa
parent
commit
b44a5ea609
2 ha cambiato i file con 23 aggiunte e 32 eliminazioni
  1. 22 32
      netbox/tenancy/migrations/0003_contacts.py
  2. 1 0
      netbox/tenancy/models.py

+ 22 - 32
netbox/tenancy/migrations/0003_contacts.py

@@ -14,24 +14,6 @@ class Migration(migrations.Migration):
     ]
 
     operations = [
-        migrations.CreateModel(
-            name='Contact',
-            fields=[
-                ('created', models.DateField(auto_now_add=True, null=True)),
-                ('last_updated', models.DateTimeField(auto_now=True, null=True)),
-                ('custom_field_data', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
-                ('id', models.BigAutoField(primary_key=True, serialize=False)),
-                ('name', models.CharField(max_length=100)),
-                ('title', models.CharField(blank=True, max_length=100)),
-                ('phone', models.CharField(blank=True, max_length=50)),
-                ('email', models.EmailField(blank=True, max_length=254)),
-                ('address', models.CharField(blank=True, max_length=200)),
-                ('comments', models.TextField(blank=True)),
-            ],
-            options={
-                'ordering': ['name'],
-            },
-        ),
         migrations.CreateModel(
             name='ContactRole',
             fields=[
@@ -68,6 +50,27 @@ class Migration(migrations.Migration):
                 'unique_together': {('parent', 'name')},
             },
         ),
+        migrations.CreateModel(
+            name='Contact',
+            fields=[
+                ('created', models.DateField(auto_now_add=True, null=True)),
+                ('last_updated', models.DateTimeField(auto_now=True, null=True)),
+                ('custom_field_data', models.JSONField(blank=True, default=dict, encoder=django.core.serializers.json.DjangoJSONEncoder)),
+                ('id', models.BigAutoField(primary_key=True, serialize=False)),
+                ('name', models.CharField(max_length=100)),
+                ('title', models.CharField(blank=True, max_length=100)),
+                ('phone', models.CharField(blank=True, max_length=50)),
+                ('email', models.EmailField(blank=True, max_length=254)),
+                ('address', models.CharField(blank=True, max_length=200)),
+                ('comments', models.TextField(blank=True)),
+                ('group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contacts', to='tenancy.contactgroup')),
+                ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
+            ],
+            options={
+                'ordering': ['name'],
+                'unique_together': {('group', 'name')},
+            },
+        ),
         migrations.CreateModel(
             name='ContactAssignment',
             fields=[
@@ -82,20 +85,7 @@ class Migration(migrations.Migration):
             ],
             options={
                 'ordering': ('priority', 'contact'),
+                'unique_together': {('content_type', 'object_id', 'contact', 'role', 'priority')},
             },
         ),
-        migrations.AddField(
-            model_name='contact',
-            name='group',
-            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contacts', to='tenancy.contactgroup'),
-        ),
-        migrations.AddField(
-            model_name='contact',
-            name='tags',
-            field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
-        ),
-        migrations.AlterUniqueTogether(
-            name='contact',
-            unique_together={('group', 'name')},
-        ),
     ]

+ 1 - 0
netbox/tenancy/models.py

@@ -259,6 +259,7 @@ class ContactAssignment(ChangeLoggedModel):
 
     class Meta:
         ordering = ('priority', 'contact')
+        unique_together = ('content_type', 'object_id', 'contact', 'role', 'priority')
 
     def __str__(self):
         return f"{self.contact} ({self.get_priority_display()})" if self.priority else self.name