mirror of
https://github.com/ajurna/cbwebreader.git
synced 2025-12-06 06:17:17 +00:00
changed all settings and login forms to use the django forms model.
added support for recaptcha via django-recaptcha
This commit is contained in:
50
comic/forms.py
Normal file
50
comic/forms.py
Normal 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
|
||||
@@ -6,6 +6,22 @@
|
||||
{% if error_message %}
|
||||
<div class="alert alert-danger" role="alert">{{ error_message }}</div>
|
||||
{% 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">
|
||||
{% csrf_token %}
|
||||
<div class="form-group">
|
||||
|
||||
@@ -6,6 +6,7 @@ from django.contrib.auth.decorators import login_required
|
||||
|
||||
from comic.models import Setting, ComicBook, ComicStatus
|
||||
from util import generate_breadcrumbs
|
||||
from forms import SettingsForm
|
||||
|
||||
from os import path
|
||||
|
||||
@@ -27,26 +28,40 @@ def comic_list(request, comic_path=''):
|
||||
})
|
||||
return render(request, 'comic/comic_list.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
def settings_page(request):
|
||||
obj, created = Setting.objects.get_or_create(name='BASE_DIR')
|
||||
error_message = ''
|
||||
|
||||
if request.POST:
|
||||
if path.isdir(request.POST['base_directory']):
|
||||
obj.value = request.POST['base_directory']
|
||||
obj.save()
|
||||
else:
|
||||
error_message = 'This is not a valid Directory'
|
||||
elif obj.value == '':
|
||||
error_message = 'Base Directory cannot be blank'
|
||||
elif not path.isdir(obj.value):
|
||||
error_message = 'Base Directory does not exist'
|
||||
form = SettingsForm(request.POST)
|
||||
if form.is_valid():
|
||||
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:
|
||||
error_message = 'This is not a valid Directory'
|
||||
recap = Setting.objects.get(name='RECAPTCHA')
|
||||
if form.cleaned_data['recaptcha']:
|
||||
recap.value = '1'
|
||||
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, {
|
||||
'base_dir': obj,
|
||||
'error_message': error_message,
|
||||
'form': form,
|
||||
})
|
||||
return render(request, 'comic/settings_page.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
def read_comic(request, comic_path, page):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR').value
|
||||
|
||||
Reference in New Issue
Block a user