diff options
author | 2020-07-15 15:32:23 +0200 | |
---|---|---|
committer | 2020-07-15 15:32:23 +0200 | |
commit | e8a32c717babee132626d6574f7ca706338739dc (patch) | |
tree | e9ab3b0b1b596f1021ab7275d184b60273fc38a2 | |
parent | Add a migration for the new AllowList model. (diff) |
Add a UniqueConstraint to prevent duplicates.
https://github.com/python-discord/site/issues/305
-rw-r--r-- | pydis_site/apps/api/migrations/0057_create_new_allowlist_model.py (renamed from pydis_site/apps/api/migrations/0057_allowlist.py) | 8 | ||||
-rw-r--r-- | pydis_site/apps/api/models/bot/allowlist.py | 13 |
2 files changed, 21 insertions, 0 deletions
diff --git a/pydis_site/apps/api/migrations/0057_allowlist.py b/pydis_site/apps/api/migrations/0057_create_new_allowlist_model.py index 7d815e91..45650d86 100644 --- a/pydis_site/apps/api/migrations/0057_allowlist.py +++ b/pydis_site/apps/api/migrations/0057_create_new_allowlist_model.py @@ -26,4 +26,12 @@ class Migration(migrations.Migration): }, bases=(pydis_site.apps.api.models.mixins.ModelReprMixin, models.Model), ), + migrations.AlterModelTable( + name='allowlist', + table='allow_list', + ), + migrations.AddConstraint( + model_name='allowlist', + constraint=models.UniqueConstraint(fields=('content', 'type'), name='unique_allowlist'), + ) ] diff --git a/pydis_site/apps/api/models/bot/allowlist.py b/pydis_site/apps/api/models/bot/allowlist.py index b0aea066..fc57ef32 100644 --- a/pydis_site/apps/api/models/bot/allowlist.py +++ b/pydis_site/apps/api/models/bot/allowlist.py @@ -24,3 +24,16 @@ class AllowList(ModelTimestampMixin, ModelReprMixin, models.Model): content = models.TextField( help_text="The data to add to the allowlist." ) + + class Meta: + """Metaconfig for this model.""" + + db_table = 'allow_list' + + # This constraint ensures only one allowlist with the same content + # can exist per type.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_allowlist'), + ] |