aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/serializers.py
diff options
context:
space:
mode:
authorGravatar mbaruh <[email protected]>2023-01-27 23:37:26 +0200
committerGravatar mbaruh <[email protected]>2023-01-28 03:09:23 +0200
commit5f538e9e876adc7f4a459fe230b46bdde61b3f64 (patch)
tree566637dc16d73e0c2cd025db46b85999f136246b /pydis_site/apps/api/serializers.py
parentMerge branch 'main' into new-filter-schema (diff)
Make filter unique constraint use NULLS NOT DISTINCT
The existing constraint was ineffective as null values were considered distinct, and so two filters with the same content and no overrides were considered different. This change uses a new PSQL 15 feature unsupported in django currently, and so it is added with raw SQL.
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r--pydis_site/apps/api/serializers.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
index 7f9461ec..8da47802 100644
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -257,6 +257,15 @@ class FilterSerializer(ModelSerializer):
) + SETTINGS_FIELDS
extra_kwargs = _create_filter_meta_extra_kwargs()
+ def create(self, validated_data: dict) -> User:
+ """Override the create method to catch violations of the custom uniqueness constraint."""
+ try:
+ return super().create(validated_data)
+ except IntegrityError:
+ raise ValidationError(
+ "Check if a filter with this combination of content and settings already exists in this filter list."
+ )
+
def to_representation(self, instance: Filter) -> dict:
"""
Provides a custom JSON representation to the Filter Serializers.