| 1 |
# Generated by Django 3.2.7 on 2021-09-13 15:16 |
| 2 |
|
| 3 |
from django.db import migrations |
| 4 |
from django.db.models import Count |
| 5 |
|
| 6 |
|
| 7 |
def deduplicate_attempts(apps, schema_editor): |
| 8 |
AccessAttempt = apps.get_model("axes", "AccessAttempt") |
| 9 |
duplicated_attempts = ( |
| 10 |
AccessAttempt.objects.values("username", "user_agent", "ip_address") |
| 11 |
.annotate(Count("id")) |
| 12 |
.order_by() |
| 13 |
.filter(id__count__gt=1) |
| 14 |
) |
| 15 |
|
| 16 |
for attempt in duplicated_attempts: |
| 17 |
redundant_attempts = AccessAttempt.objects.filter( |
| 18 |
username=attempt["username"], |
| 19 |
user_agent=attempt["user_agent"], |
| 20 |
ip_address=attempt["ip_address"], |
| 21 |
)[1:] |
| 22 |
for redundant_attempt in redundant_attempts: |
| 23 |
redundant_attempt.delete() |
| 24 |
|
| 25 |
|
| 26 |
class Migration(migrations.Migration): |
| 27 |
|
| 28 |
dependencies = [ |
| 29 |
("axes", "0006_remove_accesslog_trusted"), |
| 30 |
] |
| 31 |
|
| 32 |
operations = [ |
| 33 |
migrations.RunPython(deduplicate_attempts), |
| 34 |
migrations.AlterUniqueTogether( |
| 35 |
name="accessattempt", |
| 36 |
unique_together={("username", "ip_address", "user_agent")}, |
| 37 |
), |
| 38 |
] |