diff --git a/cbreader/settings.py b/cbreader/settings.py index 8b37e29..e2f5154 100644 --- a/cbreader/settings.py +++ b/cbreader/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'comic', + 'comic_auth', ) MIDDLEWARE_CLASSES = ( @@ -101,3 +102,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = '/static/' + +LOGIN_REDIRECT_URL = '/comic/' + +LOGIN_URL = '/login/' \ No newline at end of file diff --git a/cbreader/urls.py b/cbreader/urls.py index a4ebe04..daccb1a 100644 --- a/cbreader/urls.py +++ b/cbreader/urls.py @@ -17,6 +17,8 @@ from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ - url(r'^comic/', include('comic.urls')), - url(r'^admin/', include(admin.site.urls)), + url(r'^login/', 'comic_auth.views.comic_login'), + url(r'^logout/', 'comic_auth.views.comic_logout'), + url(r'^comic/', include('comic.urls')), + url(r'^admin/', include(admin.site.urls)), ] diff --git a/comic/templates/base.html b/comic/templates/base.html index e7f1228..4470eb5 100644 --- a/comic/templates/base.html +++ b/comic/templates/base.html @@ -6,7 +6,7 @@ - + {% block title %}CB Reader{% endblock %} @@ -36,14 +36,14 @@ - CBReader + CBReader @@ -51,9 +51,7 @@
diff --git a/comic/views.py b/comic/views.py index d346c46..4362d58 100644 --- a/comic/views.py +++ b/comic/views.py @@ -2,13 +2,14 @@ from django.http import HttpResponse from django.template import RequestContext from django.utils.http import urlsafe_base64_decode from django.shortcuts import render +from django.contrib.auth.decorators import login_required from comic.models import Setting, ComicBook from util import generate_breadcrumbs, generate_directory from os import path - +@login_required def comic_list(request, comic_path=''): base_dir = Setting.objects.get(name='BASE_DIR').value 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) - +@login_required def read_comic(request, comic_path, page): base_dir = Setting.objects.get(name='BASE_DIR').value page = int(page) @@ -42,7 +43,7 @@ def read_comic(request, comic_path, page): }) return render(request, 'comic/read_comic.html', context) - +@login_required def get_image(_, comic_path, page): base_dir = Setting.objects.get(name='BASE_DIR').value page = int(page) diff --git a/comic_auth/__init__.py b/comic_auth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/comic_auth/admin.py b/comic_auth/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/comic_auth/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/comic_auth/migrations/__init__.py b/comic_auth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/comic_auth/models.py b/comic_auth/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/comic_auth/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/comic_auth/templates/comic_auth/login.html b/comic_auth/templates/comic_auth/login.html new file mode 100644 index 0000000..cead66f --- /dev/null +++ b/comic_auth/templates/comic_auth/login.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block title %}CBreader Login{% endblock %} +{% block content %} + +
+ {% if error %} + + {% endif %} + +
+ +{% endblock %} \ No newline at end of file diff --git a/comic_auth/tests.py b/comic_auth/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/comic_auth/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/comic_auth/views.py b/comic_auth/views.py new file mode 100644 index 0000000..bfc904b --- /dev/null +++ b/comic_auth/views.py @@ -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/') \ No newline at end of file