changed all settings and login forms to use the django forms model.

added support for recaptcha via django-recaptcha
This commit is contained in:
2015-06-26 15:46:49 +01:00
parent 80a8a74fde
commit fc1121b194
8 changed files with 168 additions and 31 deletions

44
comic_auth/forms.py Normal file
View File

@@ -0,0 +1,44 @@
from django import forms
from captcha.fields import ReCaptchaField
from comic.models import Setting
class LoginForm(forms.Form):
username = forms.CharField(max_length=50,
label='',
widget=forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Username',
'autofocus': True,
'required': True,
}
))
password = forms.CharField(label='Password',
widget=forms.PasswordInput(
attrs={
'class': 'form-control',
'placeholder': 'Username',
'required': True,
}
))
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
setting, created = Setting.objects.get_or_create(name='RECAPTCHA')
if created:
setting.value = '0'
if setting.value == '1':
public_key = Setting.objects.get(name='RECAPTCHA_PUBLIC_KEY').value
private_key = Setting.objects.get(name='RECAPTCHA_PRIVATE_KEY').value
captcha = ReCaptchaField(
label='',
public_key=public_key,
private_key=private_key,
attrs={
'theme': 'white',
'class': 'form-control',
}
)
self.fields['captcha'] = captcha

View File

@@ -1,5 +1,5 @@
{% extends "base.html" %}
{% block title %}CBreader Login{% endblock %}
{% block title %}CBWebReader - Login{% endblock %}
{% block content %}
<div class="col-md-4 col-md-offset-4">
@@ -9,10 +9,7 @@
<form method="post" class="form-signin">
{% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputUser" class="sr-only">Username</label>
<input type="text" name="user" id="inputUser" class="form-control" placeholder="Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>
{{form}}
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>

View File

@@ -1,25 +1,38 @@
from django.shortcuts import render, redirect, RequestContext
from django.contrib.auth import authenticate, login, logout
from comic_auth.forms import LoginForm
def comic_login(request):
if request.POST:
user = authenticate(username=request.POST['user'],
password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
if request.GET.has_key('next'):
return redirect(request.GET['next'])
form = LoginForm(request.POST)
if form.is_valid():
user = authenticate(username=form.cleaned_data['username'],
password=form.cleaned_data['password'])
if user is not None:
if user.is_active:
login(request, user)
if request.GET.has_key('next'):
return redirect(request.GET['next'])
else:
return redirect('/comic/')
else:
return redirect('/comic/')
else:
context = RequestContext(request, {
'error': True
})
return render(request, 'comic_auth/login.html', context)
context = RequestContext(request, {
'error': True,
})
return render(request, 'comic_auth/login.html', context)
else:
context = RequestContext(request, {
'error': True,
'form': form
})
return render(request, 'comic_auth/login.html', context)
else:
context = RequestContext(request, {})
form = LoginForm()
context = RequestContext(request, {
'form': form
})
return render(request, 'comic_auth/login.html', context)
def comic_logout(request):