def clear_login_attempts(sender, request, user, **kwargs): ip_address = get_ip(request) if ip_address: LoginAttempt.objects.all().filter(ip_address=ip_address).delete()
def login(request, template_name="login.html", authentication_form=AuthenticationForm, login_url=None, current_app=None, extra_context={}): if request.user.is_superuser: return redirect(reverse('super_login')) elif request.user.is_authenticated(): return redirect(reverse('home')) # Log the login attempt. ip_address = get_ip(request) if ip_address: LoginAttempt.objects.create(ip_address=ip_address) half_day_ago = datetime.now() - timedelta(hours=12) login_attempts = LoginAttempt.objects.all().filter( ip_address=ip_address).filter( attempt_datetime__gt=half_day_ago).count() extra_context.update({ 'show_captcha': (login_attempts >= 10), 'invalid_captcha': False, 'RECAPTCHA_PUBLIC_KEY': s.RECAPTCHA_PUBLIC_KEY, }) form = authentication_form(data=request.POST) if not form.is_valid(): if re.search(m.not_activated, str(form.errors)): extra_context.update({'show_resend_activation_email_form': True}) else: extra_context.update({'show_resend_activation_email_form': False}) if request.method == 'POST' and login_attempts > 10: captcha_params = urllib.urlencode({ 'privatekey': s.RECAPTCHA_PRIVATE_KEY, 'remoteip': ip_address, 'challenge': request.POST.get('recaptcha_challenge_field', ''), 'response': request.POST.get('recaptcha_response_field', '') }) captcha_req = urllib2.Request( url="http://www.google.com/recaptcha/api/verify", data=captcha_params, headers={ "Content-type": "application/x-www-form-urlencoded", "User-agent": "reCAPTCHA Umeqo" }) captcha_resp = urllib2.urlopen(captcha_req) return_values = captcha_resp.read().splitlines() captcha_resp.close() if return_values[0] != 'true': form = authentication_form(data=request.POST) current_site = get_current_site(request) context = extra_context context.update({ 'invalid_captcha': True, 'form': form, 'site': current_site, 'site_name': current_site.name, }) return context response = auth_login_view(request, template_name=template_name, authentication_form=AuthenticationForm, current_app=current_app, extra_context=extra_context) if request.user.is_staff: return redirect(reverse('super_login')) else: us_user_logged_in.send(sender=request.user.__class__, request=request, user=request.user) return response
def login( request, template_name="login.html", authentication_form=AuthenticationForm, login_url=None, current_app=None, extra_context={}, ): if request.user.is_superuser: return redirect(reverse("super_login")) elif request.user.is_authenticated(): return redirect(reverse("home")) # Log the login attempt. ip_address = get_ip(request) if ip_address: LoginAttempt.objects.create(ip_address=ip_address) half_day_ago = datetime.now() - timedelta(hours=12) login_attempts = ( LoginAttempt.objects.all().filter(ip_address=ip_address).filter(attempt_datetime__gt=half_day_ago).count() ) extra_context.update( { "show_captcha": (login_attempts >= 10), "invalid_captcha": False, "RECAPTCHA_PUBLIC_KEY": s.RECAPTCHA_PUBLIC_KEY, } ) form = authentication_form(data=request.POST) if not form.is_valid(): if re.search(m.not_activated, str(form.errors)): extra_context.update({"show_resend_activation_email_form": True}) else: extra_context.update({"show_resend_activation_email_form": False}) if request.method == "POST" and login_attempts > 10: captcha_params = urllib.urlencode( { "privatekey": s.RECAPTCHA_PRIVATE_KEY, "remoteip": ip_address, "challenge": request.POST.get("recaptcha_challenge_field", ""), "response": request.POST.get("recaptcha_response_field", ""), } ) captcha_req = urllib2.Request( url="http://www.google.com/recaptcha/api/verify", data=captcha_params, headers={"Content-type": "application/x-www-form-urlencoded", "User-agent": "reCAPTCHA Umeqo"}, ) captcha_resp = urllib2.urlopen(captcha_req) return_values = captcha_resp.read().splitlines() captcha_resp.close() if return_values[0] != "true": form = authentication_form(data=request.POST) current_site = get_current_site(request) context = extra_context context.update( {"invalid_captcha": True, "form": form, "site": current_site, "site_name": current_site.name} ) return context response = auth_login_view( request, template_name=template_name, authentication_form=AuthenticationForm, current_app=current_app, extra_context=extra_context, ) if request.user.is_staff: return redirect(reverse("super_login")) else: us_user_logged_in.send(sender=request.user.__class__, request=request, user=request.user) return response