Source

django-doorkeeper / doorkeeper / views.py

# Create your views here.
from django.http import Http404

from models import BotRecord
import datetime
from django.conf import settings

TERM = getattr(settings,'DOORKEEPER_TERM', 1200)
PATIENCE= getattr(settings,'DOORKEEPER_PATIENCE', 5)

def inspect(request):
    """
    View to register suspicious client and raise Http404

    """
    sagent  =request.META.get('HTTP_USER_AGENT',"n/a")
    saddr=request.META.get('REMOTE_ADDR',"n/a")
    shash="%s %s"% (saddr, sagent)
    try:
        br=BotRecord.objects.get(bot_hash=shash)
        now=datetime.datetime.now()
        delta=datetime.timedelta(0,TERM)
        if br.last_date+delta > now:
            br.penalty_count+=1
            if br.penalty_count > PATIENCE:
                br.detention_upto=now+delta
                br.jail_count+=1
        br.save()
    except BotRecord.DoesNotExist:
        br=BotRecord.objects.create(bot_hash=shash,bot_name=sagent, bot_adr=saddr)
        br.save()
    raise Http404()