mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Fix: Explicit validation of custom field name unique constraint (#5647)
This commit is contained in:
parent
6cf732e6ec
commit
6b34f592df
@ -81,7 +81,7 @@ class MatchingModelSerializer(serializers.ModelSerializer):
|
||||
slug = SerializerMethodField()
|
||||
|
||||
def validate(self, data):
|
||||
# see https://github.com/encode/django-rest-framework/issues/7173
|
||||
# TODO: remove pending https://github.com/encode/django-rest-framework/issues/7173
|
||||
name = data["name"] if "name" in data else self.instance.name
|
||||
owner = (
|
||||
data["owner"]
|
||||
@ -441,6 +441,17 @@ class CustomFieldSerializer(serializers.ModelSerializer):
|
||||
"data_type",
|
||||
]
|
||||
|
||||
def validate(self, attrs):
|
||||
# TODO: remove pending https://github.com/encode/django-rest-framework/issues/7173
|
||||
name = attrs["name"] if "name" in attrs else self.instance.name
|
||||
if ("name" in attrs) and self.Meta.model.objects.filter(
|
||||
name=name,
|
||||
).exists():
|
||||
raise serializers.ValidationError(
|
||||
{"error": "Object violates name unique constraint"},
|
||||
)
|
||||
return super().validate(attrs)
|
||||
|
||||
|
||||
class ReadWriteSerializerMethodField(serializers.SerializerMethodField):
|
||||
"""
|
||||
|
@ -53,6 +53,29 @@ class TestCustomField(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(data["name"], name)
|
||||
self.assertEqual(data["data_type"], field_type)
|
||||
|
||||
def test_create_custom_field_nonunique_name(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Custom field exists
|
||||
WHEN:
|
||||
- API request to create custom field with the same name
|
||||
THEN:
|
||||
- HTTP 400 is returned
|
||||
"""
|
||||
CustomField.objects.create(
|
||||
name="Test Custom Field",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
|
||||
resp = self.client.post(
|
||||
self.ENDPOINT,
|
||||
data={
|
||||
"data_type": "string",
|
||||
"name": "Test Custom Field",
|
||||
},
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_custom_field_instance(self):
|
||||
"""
|
||||
GIVEN:
|
||||
|
Loading…
x
Reference in New Issue
Block a user