-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47d6a71
commit 8ef0420
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import timedelta | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from ...models import AccessAttempt | ||
from ... import config | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Cleans up django-defender AccessAttempt table" | ||
|
||
def handle(self, **options): | ||
""" | ||
Removes any entries in the AccessAttempt that are older | ||
than your DEFENDER_ACCESS_ATTEMPT_EXPIRATION config, default 24 HOURS. | ||
""" | ||
print("Starting clean up of django-defender table") | ||
now = timezone.now() | ||
cleanup_delta = timedelta(hours=config.ACCESS_ATTEMPT_EXPIRATION) | ||
min_attempt_time = now - cleanup_delta | ||
|
||
attempts_to_clean = AccessAttempt.objects.filter( | ||
attempt_time__lt=min_attempt_time, | ||
) | ||
attempts_to_clean_count = attempts_to_clean.count() | ||
|
||
attempts_to_clean.delete() | ||
|
||
print( | ||
"Finished. Removed {0} AccessAttempt entries.".format( | ||
attempts_to_clean_count) | ||
) |