diff options
author | 2019-10-08 01:22:15 +0200 | |
---|---|---|
committer | 2019-10-08 01:22:15 +0200 | |
commit | 0d383cbe925fdec97cb678c0168ecf7e90d3d729 (patch) | |
tree | 2aa529c345ffe5000452c78aedc19867b227a43c /pydis_site/apps/api/models/bot | |
parent | Migrate undesirable active infraction to inactive (diff) |
Prevent double active infractions with constraint
https://github.com/python-discord/site/issues/273
This commits adds a UniqueConstraint for active infractions on a
combination of the `user` and `type` field. This means that a user
can only have one active infraction of a given type in the database
at any time.
I've also added tests to make sure that this behaves as expected.
Diffstat (limited to 'pydis_site/apps/api/models/bot')
-rw-r--r-- | pydis_site/apps/api/models/bot/infraction.py | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index dfb32a97..108fd3a2 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -71,3 +71,10 @@ class Infraction(ModelReprMixin, models.Model): """Defines the meta options for the infraction model.""" ordering = ['-inserted_at'] + constraints = ( + models.UniqueConstraint( + fields=["user", "type"], + condition=models.Q(active=True), + name="unique_active_infraction_per_type_per_user" + ), + ) |