mirror of
https://github.com/ajurna/cbwebreader.git
synced 2025-12-06 06:17:17 +00:00
added initial setup page so that you can now configure a user if one isnt already configured.
This commit is contained in:
@@ -20,7 +20,7 @@ urlpatterns = [
|
||||
url(r'^$', 'comic.views.comic_redirect'),
|
||||
url(r'^login/', 'comic_auth.views.comic_login'),
|
||||
url(r'^logout/', 'comic_auth.views.comic_logout'),
|
||||
url(r'^setup/', include(admin.site.urls)),
|
||||
url(r'^setup/', 'comic.views.initial_setup'),
|
||||
url(r'^comic/', include('comic.urls')),
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
|
||||
|
||||
@@ -6,6 +6,53 @@ from os import path
|
||||
from comic.models import Setting
|
||||
|
||||
|
||||
class InitialSetupForm(forms.Form):
|
||||
username = forms.CharField(help_text='Username',
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': 'form-control',
|
||||
}
|
||||
))
|
||||
email = forms.CharField(help_text='Email Address',
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': 'form-control'
|
||||
}
|
||||
))
|
||||
password = forms.CharField(help_text='New Password',
|
||||
widget=forms.PasswordInput(
|
||||
attrs={
|
||||
'class': 'form-control',
|
||||
}
|
||||
))
|
||||
password_confirm = forms.CharField(help_text='New Password Confirmation',
|
||||
widget=forms.PasswordInput(
|
||||
attrs={
|
||||
'class': 'form-control',
|
||||
}
|
||||
))
|
||||
base_dir = forms.CharField(help_text='Base Directory',
|
||||
widget=forms.TextInput(
|
||||
attrs={
|
||||
'class': 'form-control'
|
||||
}
|
||||
))
|
||||
|
||||
def clean_base_dir(self):
|
||||
data = self.cleaned_data['base_dir']
|
||||
if not path.isdir(data):
|
||||
raise forms.ValidationError('This is not a valid Directory')
|
||||
return data
|
||||
|
||||
def clean(self):
|
||||
form_data = self.cleaned_data
|
||||
if form_data['password'] != form_data['password_confirm']:
|
||||
raise forms.ValidationError('Passwords do not match.')
|
||||
if len(form_data['password']) < 8:
|
||||
raise forms.ValidationError('Password is too short')
|
||||
return form_data
|
||||
|
||||
|
||||
class AccountForm(forms.Form):
|
||||
username = forms.CharField(help_text='Username',
|
||||
required=False,
|
||||
|
||||
46
comic/templates/comic/setup.html
Normal file
46
comic/templates/comic/setup.html
Normal file
@@ -0,0 +1,46 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{{ title }}{% endblock %}
|
||||
|
||||
{% block breadcrumb %}
|
||||
{% for crumb in breadcrumbs %}
|
||||
{% if not forloop.last %}
|
||||
<li><a href="{{ crumb.url }}">{{ crumb.name }}</a></li>
|
||||
{% else %}
|
||||
<li class="active">{{ crumb.name }}</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% if error_message %}
|
||||
<div class="alert alert-danger" role="alert">{{ error_message|safe }}</div>
|
||||
{% endif %}
|
||||
{% if success_message %}
|
||||
<div class="alert alert-success" role="alert">{{ success_message|safe }}</div>
|
||||
{% endif %}
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{% for item in form %}
|
||||
<div class="form-group">
|
||||
<label for="{{ item.id_for_label }}">{{ 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">
|
||||
<label for="base_directory">Base Directory</label>
|
||||
<input type="text" class="form-control" id="base_directory" name="base_directory" placeholder="Base Directory" value="{{ base_dir.value }}">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-default">Submit</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -4,10 +4,11 @@ from django.utils.http import urlsafe_base64_decode
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.contrib.auth.decorators import login_required, user_passes_test
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth import login, authenticate
|
||||
|
||||
from comic.models import Setting, ComicBook, ComicStatus
|
||||
from util import generate_breadcrumbs_from_path, generate_breadcrumbs_from_menu, generate_title_from_path
|
||||
from forms import SettingsForm, AccountForm, EditUserForm, AddUserForm
|
||||
from forms import SettingsForm, AccountForm, EditUserForm, AddUserForm, InitialSetupForm
|
||||
from util import Menu
|
||||
from os import path
|
||||
|
||||
@@ -220,5 +221,36 @@ def get_image(_, comic_path, page):
|
||||
return HttpResponse(img.read(), content_type=content)
|
||||
|
||||
|
||||
def initial_setup(request):
|
||||
if User.objects.all().exists():
|
||||
return redirect('/comic/')
|
||||
if request.POST:
|
||||
form = InitialSetupForm(request.POST)
|
||||
if form.is_valid():
|
||||
user = User(
|
||||
username=form.cleaned_data['username'],
|
||||
email=form.cleaned_data['email'],
|
||||
is_staff=True,
|
||||
is_superuser=True,
|
||||
)
|
||||
user.set_password(form.cleaned_data['password'])
|
||||
user.save()
|
||||
base_dir, _ = Setting.objects.get_or_create(name='BASE_DIR')
|
||||
base_dir.value = form.cleaned_data['base_dir']
|
||||
base_dir.save()
|
||||
user = authenticate(username=form.cleaned_data['username'],
|
||||
password=form.cleaned_data['password'])
|
||||
login(request, user)
|
||||
return redirect('/comic/')
|
||||
else:
|
||||
form = InitialSetupForm()
|
||||
context = {
|
||||
'form': form,
|
||||
'title': 'CBWebReader - Setup',
|
||||
'error_message': form.errors,
|
||||
}
|
||||
return render(request, 'comic/settings_page.html', context)
|
||||
|
||||
|
||||
def comic_redirect(_):
|
||||
return redirect('/comic/')
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from django.shortcuts import render, redirect, RequestContext
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from comic_auth.forms import LoginForm
|
||||
|
||||
@@ -30,6 +31,8 @@ def comic_login(request):
|
||||
})
|
||||
return render(request, 'comic_auth/login.html', context)
|
||||
else:
|
||||
if not User.objects.all().exists():
|
||||
return redirect('/setup/')
|
||||
form = LoginForm()
|
||||
context = RequestContext(request, {
|
||||
'form': form
|
||||
|
||||
Reference in New Issue
Block a user