aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-07-29 19:07:03 +0200
committerGravatar Leon Sandøy <[email protected]>2020-07-29 19:07:03 +0200
commit12f447bc9636382a623e6b30ae43ccc036c488b5 (patch)
tree9e96bf98d684dcb91c359e66b882fdeee6bef4d6
parentFix multiple leafs for migration graph. (diff)
Add a constraint in the DB model as well.
This really should've been handled automatically by DRF, and in the future, it will be. But for now, we need to have constraints both on the serializer (to get status code 400), and on the model (to prevent direct database constraint violations). See https://github.com/encode/django-rest-framework/issues/7173
-rw-r--r--pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py4
-rw-r--r--pydis_site/apps/api/models/bot/filter_list.py11
2 files changed, 15 insertions, 0 deletions
diff --git a/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py b/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py
index fffb8159..aecfdad7 100644
--- a/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py
+++ b/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py
@@ -25,5 +25,9 @@ class Migration(migrations.Migration):
('comment', models.TextField(help_text="Optional comment on this entry.", null=True)),
],
bases=(pydis_site.apps.api.models.mixins.ModelReprMixin, models.Model),
+ ),
+ migrations.AddConstraint(
+ model_name='filterlist',
+ constraint=models.UniqueConstraint(fields=('content', 'type'), name='unique_filter_list')
)
]
diff --git a/pydis_site/apps/api/models/bot/filter_list.py b/pydis_site/apps/api/models/bot/filter_list.py
index 5961aed7..d279e137 100644
--- a/pydis_site/apps/api/models/bot/filter_list.py
+++ b/pydis_site/apps/api/models/bot/filter_list.py
@@ -28,3 +28,14 @@ class FilterList(ModelTimestampMixin, ModelReprMixin, models.Model):
help_text="Optional comment on this entry.",
null=True
)
+
+ class Meta:
+ """Metaconfig for this model."""
+
+ # This constraint ensures only one filterlist with the
+ # same content can exist. This means that we cannot have both an allow
+ # and a deny for the same item, and we cannot have duplicates of the
+ # same item.
+ constraints = [
+ models.UniqueConstraint(fields=['content', 'type'], name='unique_filter_list'),
+ ]