added authentication

This commit is contained in:
2015-06-19 15:09:50 +01:00
parent e6791bf41a
commit 16c74cebe9
11 changed files with 74 additions and 11 deletions

View File

@@ -38,6 +38,7 @@ INSTALLED_APPS = (
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'comic', 'comic',
'comic_auth',
) )
MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (
@@ -101,3 +102,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.8/howto/static-files/ # https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/comic/'
LOGIN_URL = '/login/'

View File

@@ -17,6 +17,8 @@ from django.conf.urls import include, url
from django.contrib import admin from django.contrib import admin
urlpatterns = [ urlpatterns = [
url(r'^comic/', include('comic.urls')), url(r'^login/', 'comic_auth.views.comic_login'),
url(r'^admin/', include(admin.site.urls)), url(r'^logout/', 'comic_auth.views.comic_logout'),
url(r'^comic/', include('comic.urls')),
url(r'^admin/', include(admin.site.urls)),
] ]

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content=""> <meta name="description" content="">
<meta name="author" content=""> <meta name="author" content="Ajurna">
<link rel="icon" href="../../favicon.ico"> <link rel="icon" href="../../favicon.ico">
<title>{% block title %}CB Reader{% endblock %}</title> <title>{% block title %}CB Reader{% endblock %}</title>
@@ -36,14 +36,14 @@
<span class="icon-bar"></span> <span class="icon-bar"></span>
<span class="icon-bar"></span> <span class="icon-bar"></span>
</button> </button>
<a class="navbar-brand" href="#">CBReader</a> <a class="navbar-brand" href="/comic/">CBReader</a>
</div> </div>
<div id="navbar" class="collapse navbar-collapse"> <div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
{% block navbar %} {% block navbar %}
<li class="active"><a href="/comic/">Home</a></li> <li class="active"><a href="/comic/">Home</a></li>
<li><a href="settings/">Settings</a></li> <li><a href="settings/">Settings</a></li>
<li><a href="#contact">Contact</a></li> <li><a href="/logout/">Logout</a></li>
{% endblock %} {% endblock %}
</ul> </ul>
</div><!--/.nav-collapse --> </div><!--/.nav-collapse -->
@@ -51,9 +51,7 @@
</nav> </nav>
<ol class="breadcrumb"> <ol class="breadcrumb">
{% block breadcrumb %} {% block breadcrumb %}
<li><a href="#">Home</a></li> <li><a href="/">Home</a></li>
<li><a href="#">Library</a></li>
<li class="active">Data</li>
{% endblock %} {% endblock %}
</ol> </ol>
<div class="container"> <div class="container">

View File

@@ -2,13 +2,14 @@ from django.http import HttpResponse
from django.template import RequestContext from django.template import RequestContext
from django.utils.http import urlsafe_base64_decode from django.utils.http import urlsafe_base64_decode
from django.shortcuts import render from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from comic.models import Setting, ComicBook from comic.models import Setting, ComicBook
from util import generate_breadcrumbs, generate_directory from util import generate_breadcrumbs, generate_directory
from os import path from os import path
@login_required
def comic_list(request, comic_path=''): def comic_list(request, comic_path=''):
base_dir = Setting.objects.get(name='BASE_DIR').value base_dir = Setting.objects.get(name='BASE_DIR').value
comic_path = urlsafe_base64_decode(comic_path) comic_path = urlsafe_base64_decode(comic_path)
@@ -20,7 +21,7 @@ def comic_list(request, comic_path=''):
}) })
return render(request, 'comic/comic_list.html', context) return render(request, 'comic/comic_list.html', context)
@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
page = int(page) page = int(page)
@@ -42,7 +43,7 @@ def read_comic(request, comic_path, page):
}) })
return render(request, 'comic/read_comic.html', context) return render(request, 'comic/read_comic.html', context)
@login_required
def get_image(_, comic_path, page): def get_image(_, comic_path, page):
base_dir = Setting.objects.get(name='BASE_DIR').value base_dir = Setting.objects.get(name='BASE_DIR').value
page = int(page) page = int(page)

0
comic_auth/__init__.py Normal file
View File

3
comic_auth/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

3
comic_auth/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block title %}CBreader Login{% endblock %}
{% block content %}
<div class="col-md-4 col-md-offset-4">
{% if error %}
<div class="alert alert-danger" role="alert"><p>Your username and password didn't match. Please try again.</p></div>
{% endif %}
<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>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
{% endblock %}

3
comic_auth/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

28
comic_auth/views.py Normal file
View File

@@ -0,0 +1,28 @@
from django.shortcuts import render, redirect
from django.shortcuts import RequestContext
from django.contrib.auth import authenticate, login, logout
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'])
else:
return redirect('/comic/')
else:
context = RequestContext(request, {
'error': True
})
return render(request, 'comic_auth/login.html', context)
else:
context = RequestContext(request, {})
return render(request, 'comic_auth/login.html', context)
def comic_logout(request):
logout(request)
return redirect('/login/')