# -*- coding: utf-8 -*-

from django.contrib.sites.models import Site
from django.core.mail import mail_admins
from django.template import loader, Context
from django.utils.timezone import now
from logging import getLogger as get_logger
from socket import gethostbyaddr, error as SocketError

logger = get_logger(__name__)


#----------------------------------------------------------------------
def notify(access_attempt, **kwargs):
    site = Site.objects.get_current()
    ip_address = access_attempt.ip_address
    fqdn = _resolve_ip_address_to_fqdn(ip_address)

    if access_attempt.failures == 0:
        login_success_msg = 'successful'
        login_success = True
    else:
        login_success_msg = 'failed'
        login_success = False

    context = dict(
        current_time=now(),
        attempt_time=access_attempt.attempt_time,
        failures=access_attempt.failures,
        fqdn=fqdn,
        site_domain=site.domain,
        site=site,
        ip_address=ip_address,
        login_success=login_success,
        login_success_msg=login_success_msg,
        user_agent=access_attempt.user_agent,
        username=access_attempt.username)

    subject = 'Security: %(site_domain)s: %(login_success_msg)s login attempt by %(ip_address)s (%(fqdn)s)' % context
    message = _render_email_message(context)

    mail_admins(subject, message, fail_silently=False)


#----------------------------------------------------------------------
def _resolve_ip_address_to_fqdn(ip_address):
    if ip_address is None:
        return None
    try:
        return gethostbyaddr(ip_address)[0]
    except SocketError as e:
        logger.warn('IP -> FQDN resolution failed for "%s": %s', ip_address, str(e))


#----------------------------------------------------------------------
def _render_email_message(context):
    template = loader.get_template("axes_login_actions/email_notify.txt")
    return template.render(Context(context))

@@ -18,16 +18,116 @@
 -8 -*-%0A%0A
+from logging import getLogger as get_logger%0Afrom socket import gethostbyaddr, error as SocketError%0A%0A
 from dja
@@ -163,16 +163,16 @@
 rt Site%0A
-
 from dja
@@ -290,107 +290,9 @@
 now%0A
-from logging import getLogger as get_logger%0Afrom socket import gethostbyaddr, error as SocketError
 %0A
+
 %0Alog
@@ -593,16 +593,28 @@
 failures
+_since_start
  == 0:%0A 
@@ -858,16 +858,16 @@
 t_time,%0A
-
         
@@ -898,16 +898,28 @@
 failures
+_since_start
 ,%0A      
@@ -1952,22 +1952,14 @@
 der(
-Context(
 context)
 )%0A
@@ -1956,10 +1956,9 @@
 context)
-)
 %0A

Support Django 2.x and django-axes 5.x