mirror of
https://github.com/jazzband/django-axes.git
synced 2026-05-25 15:43:44 +00:00
fix duplicated AccessAttempts
This commit is contained in:
parent
53b243c4aa
commit
b5bac68f90
2 changed files with 39 additions and 0 deletions
38
axes/migrations/0007_alter_accessattempt_unique_together.py
Normal file
38
axes/migrations/0007_alter_accessattempt_unique_together.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# Generated by Django 3.2.7 on 2021-09-13 15:16
|
||||
|
||||
from django.db import migrations
|
||||
from django.db.models import Count
|
||||
|
||||
|
||||
def deduplicate_attempts(apps, schema_editor):
|
||||
AccessAttempt = apps.get_model("axes", "AccessAttempt")
|
||||
duplicated_attempts = (
|
||||
AccessAttempt.objects.values("username", "user_agent", "ip_address")
|
||||
.annotate(Count("id"))
|
||||
.order_by()
|
||||
.filter(id__count__gt=1)
|
||||
)
|
||||
|
||||
for attempt in duplicated_attempts:
|
||||
redundant_attempts = AccessAttempt.objects.filter(
|
||||
username=attempt["username"],
|
||||
user_agent=attempt["user_agent"],
|
||||
ip_address=attempt["ip_address"],
|
||||
)[1:]
|
||||
for redundant_attempt in redundant_attempts:
|
||||
redundant_attempt.delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("axes", "0006_remove_accesslog_trusted"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(deduplicate_attempts),
|
||||
migrations.AlterUniqueTogether(
|
||||
name="accessattempt",
|
||||
unique_together={("username", "ip_address", "user_agent")},
|
||||
),
|
||||
]
|
||||
|
|
@ -34,6 +34,7 @@ class AccessAttempt(AccessBase):
|
|||
class Meta:
|
||||
verbose_name = _("access attempt")
|
||||
verbose_name_plural = _("access attempts")
|
||||
unique_together = [["username", "ip_address", "user_agent"]]
|
||||
|
||||
|
||||
class AccessLog(AccessBase):
|
||||
|
|
|
|||
Loading…
Reference in a new issue