Просмотр исходного кода

Fixes #4702: Catch IntegrityError exception when adding a non-unique secret

Jeremy Stretch 5 лет назад
Родитель
Сommit
edf15532d2
2 измененных файлов с 18 добавлено и 0 удалено
  1. 8 0
      docs/release-notes/version-2.8.md
  2. 10 0
      netbox/secrets/forms.py

+ 8 - 0
docs/release-notes/version-2.8.md

@@ -1,5 +1,13 @@
 # NetBox v2.8
 
+## v2.8.6 (FUTURE)
+
+### Bug Fixes
+
+* [#4702](https://github.com/netbox-community/netbox/issues/4702) - Catch IntegrityError exception when adding a non-unique secret
+
+---
+
 ## v2.8.5 (2020-05-26)
 
 **Note:** The minimum required version of PostgreSQL is now 9.6.

+ 10 - 0
netbox/secrets/forms.py

@@ -115,6 +115,16 @@ class SecretForm(BootstrapMixin, CustomFieldModelForm):
                 'plaintext2': "The two given plaintext values do not match. Please check your input."
             })
 
+        # Validate uniqueness
+        if Secret.objects.filter(
+            device=self.cleaned_data['device'],
+            role=self.cleaned_data['role'],
+            name=self.cleaned_data['name']
+        ).exists():
+            raise forms.ValidationError(
+                "Each secret assigned to a device must have a unique combination of role and name"
+            )
+
 
 class SecretCSVForm(CustomFieldModelCSVForm):
     device = CSVModelChoiceField(