expephalon/core/helpers/auth.py
Klaus-Uwe Mitterer 3a9515c885 Fixed ratelimits
Didn't really do anything with cronjobs
2020-05-24 19:22:17 +02:00

31 lines
No EOL
1.3 KiB
Python

from core.helpers.mail import get_template
from core.helpers.urls import relative_to_absolute as reltoabs
from core.models.auth import LoginLog
from core.helpers.request import get_client_ip
from django.urls import reverse
from django.contrib import messages
from django.utils import timezone
from dbsettings.functions import getValue
def generate_pwreset_mail(user, token):
link = reltoabs(reverse("pwreset", kwargs={"pk": str(token.token)}))
template = get_template("backend/auth/pwreset", first_name=user.first_name, link=link, sitename=getValue("core.title", "Expephalon"))
return template
def login_fail(request, user=None, message=None):
LoginLog.objects.create(user=user, ip=get_client_ip(request), success=False)
if message:
messages.error(request, message)
def login_success(request, user):
LoginLog.objects.create(user=user, ip=get_client_ip(request), success=True)
def clear_login_log(maxage=int(getValue("core.auth.ratelimit.period", 600))):
timestamp = timezone.now() - timezone.timedelta(seconds=maxage)
LoginLog.objects.filter(timestamp__lt=timestamp).delete()
def clear_ratelimits(maxage=int(getValue("core.auth.ratelimit.block", 3600))):
timestamp = timezone.now() - timezone.timedelta(seconds=maxage)
LoginLog.objects.filter(timestamp__lt=timestamp).delete()