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

View File

@@ -8,6 +8,7 @@ This is for if you have a collection of comics on a media server and want to rea
- [python 2.7](https://www.python.org/) - [python 2.7](https://www.python.org/)
- [rarfile python library by Marko Kreen](https://github.com/markokr/rarfile) (included) - [rarfile python library by Marko Kreen](https://github.com/markokr/rarfile) (included)
- [Unrar by winrar](http://rarlabs.com) - [Unrar by winrar](http://rarlabs.com)
- [django-recaptcha bt praekelt](https://github.com/praekelt/django-recaptcha)
# Installation # Installation
Pull from git and use like any django project. Pull from git and use like any django project.

View File

@@ -37,6 +37,7 @@ INSTALLED_APPS = (
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'captcha',
'comic', 'comic',
'comic_auth', 'comic_auth',
) )

50
comic/forms.py Normal file
View File

@@ -0,0 +1,50 @@
from django import forms
from comic.models import Setting
class SettingsForm(forms.Form):
base_dir = forms.CharField(help_text='Base Directory',
widget=forms.TextInput(
attrs={
'class': 'form-control'
}
))
recaptcha = forms.BooleanField(help_text='Use Recaptcha',
required=False,
widget=forms.CheckboxInput(
attrs={
'class': 'checkbox'
}
))
recaptcha_public_key = forms.CharField(help_text='Recaptcha Public Key',
required=False,
widget=forms.TextInput(
attrs={
'class': 'form-control'
}
))
recaptcha_private_key = forms.CharField(help_text='Recaptcha Private Key',
required=False,
widget=forms.TextInput(
attrs={
'class': 'form-control'
}
))
@staticmethod
def get_initial_values():
base_dir, created = Setting.objects.get_or_create(name='BASE_DIR')
recaptcha_public_key, created = Setting.objects.get_or_create(name='RECAPTCHA_PUBLIC_KEY')
recaptcha_private_key, created = Setting.objects.get_or_create(name='RECAPTCHA_PRIVATE_KEY')
recaptcha, created = Setting.objects.get_or_create(name='RECAPTCHA_PRIVATE_KEY')
if recaptcha == '1':
recaptcha = True
else:
recaptcha = False
initial = {
'base_dir': base_dir.value,
'recaptcha': recaptcha,
'recaptcha_public_key': recaptcha_public_key.value,
'recaptcha_private_key': recaptcha_private_key.value,
}
return initial

View File

@@ -6,6 +6,22 @@
{% if error_message %} {% if error_message %}
<div class="alert alert-danger" role="alert">{{ error_message }}</div> <div class="alert alert-danger" role="alert">{{ error_message }}</div>
{% endif %} {% endif %}
<form method="POST">
{% csrf_token %}
{% for item in form %}
<div class="form-group">
<label for="base_directory">{{ item.help_text }}</label>
{{ item }}
</div>
{% endfor %}
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock %}
{% block content2 %}
{% if error_message %}
<div class="alert alert-danger" role="alert">{{ error_message }}</div>
{% endif %}
<form method="POST"> <form method="POST">
{% csrf_token %} {% csrf_token %}
<div class="form-group"> <div class="form-group">

View File

@@ -6,6 +6,7 @@ from django.contrib.auth.decorators import login_required
from comic.models import Setting, ComicBook, ComicStatus from comic.models import Setting, ComicBook, ComicStatus
from util import generate_breadcrumbs from util import generate_breadcrumbs
from forms import SettingsForm
from os import path from os import path
@@ -27,26 +28,40 @@ def comic_list(request, comic_path=''):
}) })
return render(request, 'comic/comic_list.html', context) return render(request, 'comic/comic_list.html', context)
@login_required @login_required
def settings_page(request): def settings_page(request):
obj, created = Setting.objects.get_or_create(name='BASE_DIR')
error_message = '' error_message = ''
if request.POST: if request.POST:
if path.isdir(request.POST['base_directory']): form = SettingsForm(request.POST)
obj.value = request.POST['base_directory'] if form.is_valid():
obj.save() if path.isdir(form.cleaned_data['base_dir']):
base_dir = Setting.objects.get(name='BASE_DIR')
base_dir.value = form.cleaned_data['base_dir']
base_dir.save()
else: else:
error_message = 'This is not a valid Directory' error_message = 'This is not a valid Directory'
elif obj.value == '': recap = Setting.objects.get(name='RECAPTCHA')
error_message = 'Base Directory cannot be blank' if form.cleaned_data['recaptcha']:
elif not path.isdir(obj.value): recap.value = '1'
error_message = 'Base Directory does not exist' else:
recap.value = '0'
recap.save()
rprik = Setting.objects.get(name='RECAPTCHA_PRIVATE_KEY')
rprik.value = form.cleaned_data['recaptcha_private_key']
rprik.save()
rpubk = Setting.objects.get(name='RECAPTCHA_PUBLIC_KEY')
rpubk.value = form.cleaned_data['recaptcha_public_key']
rpubk.save()
form = SettingsForm(initial=SettingsForm.get_initial_values())
context = RequestContext(request, { context = RequestContext(request, {
'base_dir': obj,
'error_message': error_message, 'error_message': error_message,
'form': form,
}) })
return render(request, 'comic/settings_page.html', context) return render(request, 'comic/settings_page.html', context)
@login_required @login_required
def read_comic(request, comic_path, page): def read_comic(request, comic_path, page):
base_dir = Setting.objects.get(name='BASE_DIR').value base_dir = Setting.objects.get(name='BASE_DIR').value

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" %} {% extends "base.html" %}
{% block title %}CBreader Login{% endblock %} {% block title %}CBWebReader - Login{% endblock %}
{% block content %} {% block content %}
<div class="col-md-4 col-md-offset-4"> <div class="col-md-4 col-md-offset-4">
@@ -9,10 +9,7 @@
<form method="post" class="form-signin"> <form method="post" class="form-signin">
{% csrf_token %} {% csrf_token %}
<h2 class="form-signin-heading">Please sign in</h2> <h2 class="form-signin-heading">Please sign in</h2>
<label for="inputUser" class="sr-only">Username</label> {{form}}
<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>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form> </form>
</div> </div>

View File

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