From 134d7288a08e668ea95e2ea8e77642952dc6de9d Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 27 Jul 2014 23:34:23 +0200 Subject: [PATCH] [TASK] Wrap login and logout for proper templateing. --- citavi_mapper/settings.py | 8 ++ citavi_mapper/urls.py | 7 +- frontend/templates/layout/base.html | 14 +++- frontend/templates/register.html | 2 +- .../logged_out.html} | 2 +- frontend/templates/registration/login.html | 15 ++++ frontend/views.py | 73 +++++++++++++------ 7 files changed, 92 insertions(+), 29 deletions(-) rename frontend/templates/{login.html => registration/logged_out.html} (58%) create mode 100644 frontend/templates/registration/login.html diff --git a/citavi_mapper/settings.py b/citavi_mapper/settings.py index 97595c5..4942e5b 100644 --- a/citavi_mapper/settings.py +++ b/citavi_mapper/settings.py @@ -24,6 +24,14 @@ DEBUG = True TEMPLATE_DEBUG = True +TEMPLATE_DIRS = ( + 'frontend/templates' +) + +LOGIN_URL = '/login' +LOGOUT_URL = '/logout' +LOGIN_REDIRECT_URL = '/' + ALLOWED_HOSTS = [] # Application definition diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index 7367d47..6a7e1ab 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -5,14 +5,17 @@ from django.conf.urls import patterns, include, url from django.contrib import admin admin.autodiscover() -from frontend.views import IndexView, RegisterView, LoginView +from frontend.views import login_wrap, logout_wrap +from frontend.views import IndexView, RegisterView + urlpatterns = patterns('', # Examples: # url(r'^$', 'citavi_mapper.views.home', name='home'), # url(r'^blog/', include('blog.urls')), url(r'^$', IndexView.as_view(), name='frontend-index'), + url(r'^login/$', login_wrap, name='frontend-login'), + url(r'^logout/$', logout_wrap, name='frontend-logout'), url(r'^register/', RegisterView.as_view(), name='frontend-register'), - url(r'^login/', LoginView.as_view(), name='frontend-login'), url(r'^admin/', include(admin.site.urls)), ) \ No newline at end of file diff --git a/frontend/templates/layout/base.html b/frontend/templates/layout/base.html index 66a573b..0b6cb56 100644 --- a/frontend/templates/layout/base.html +++ b/frontend/templates/layout/base.html @@ -6,7 +6,7 @@ - {% block title %}{{page_name}} :: {{page_title}}{% endblock %} + {% block title %}{{page.name}} :: {{page.title}}{% endblock %} {% compressed_css 'bootstrap' %} {% compressed_js 'bootstrap' %} @@ -16,18 +16,24 @@
diff --git a/frontend/templates/register.html b/frontend/templates/register.html index bb1b002..d3eae2d 100644 --- a/frontend/templates/register.html +++ b/frontend/templates/register.html @@ -1,6 +1,6 @@ {% extends "layout/base.html" %} {% block content %}

- REGISTER FORM HERE + Please register here.

{% endblock %} \ No newline at end of file diff --git a/frontend/templates/login.html b/frontend/templates/registration/logged_out.html similarity index 58% rename from frontend/templates/login.html rename to frontend/templates/registration/logged_out.html index 7e293b9..52e1e84 100644 --- a/frontend/templates/login.html +++ b/frontend/templates/registration/logged_out.html @@ -1,6 +1,6 @@ {% extends "layout/base.html" %} {% block content %}

- LOGIN FORM HERE + You are logged out. Goodbye!

{% endblock %} \ No newline at end of file diff --git a/frontend/templates/registration/login.html b/frontend/templates/registration/login.html new file mode 100644 index 0000000..65fab6a --- /dev/null +++ b/frontend/templates/registration/login.html @@ -0,0 +1,15 @@ +{% extends "layout/base.html" %} +{% block content %} + {% if form.errors %} +

Your username and password did not match. Please try again.

+ {% endif %} +

+

+ {% csrf_token %} + {{form.username.label_tag}} {{form.username}}
+ {{form.password.label_tag}} {{form.password}} + + +
+

+{% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index 67bc9a4..896570a 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -1,30 +1,61 @@ # -*- coding: utf-8 -*- -from django.views.generic import TemplateView -class BaseSiteView(TemplateView): - template_name = "base.html" - page_name = "Citavi Mapper" - page_title = "BASE" - +from django.contrib.auth.decorators import login_required +from django.utils.decorators import method_decorator +from django.views.generic import TemplateView +from django.contrib.auth.views import login, logout + +FRONTEND_PAGE_NAME = 'Citavi Mapper' + +def login_wrap(*args, **kwargs): + kwargs['extra_context'] = { + 'page': { + 'name': FRONTEND_PAGE_NAME, + 'title': 'Login' + } + } + return login(*args, **kwargs) + +def logout_wrap(*args, **kwargs): + kwargs['extra_context'] = { + 'page': { + 'name': FRONTEND_PAGE_NAME, + 'title': 'Logout' + } + } + return logout(*args, **kwargs) + + +class BaseView(TemplateView): + template_name = 'base.html' + page_name = FRONTEND_PAGE_NAME + page_title = 'BASE' + + def get_page_data(self): + return { + u'name': self.page_name, + u'title': self.page_title + } def get(self, request, **kwargs): - self._request = request - return super(BaseSiteView, self).get(request, **kwargs) + return super(BaseView, self).get(request, **kwargs) def get_context_data(self, **kwargs): - context = super(BaseSiteView, self).get_context_data(**kwargs) - context['page_name'] = self.page_name - context['page_title'] = self.page_title - return context + kwargs[u'page'] = self.get_page_data() + return super(BaseView, self).get_context_data(**kwargs) -class IndexView(BaseSiteView): - template_name = "index.html" - page_title = "Index" +class LoggedInMixin(object): + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(LoggedInMixin, self).dispatch(*args, **kwargs) -class RegisterView(BaseSiteView): - template_name = "register.html" - page_title = "Register" +class ProtectedSiteView(LoggedInMixin, BaseView): + pass -class LoginView(BaseSiteView): - template_name = "login.html" - page_title = "Login" +class IndexView(ProtectedSiteView): + template_name = 'index.html' + page_title = 'Index' + +class RegisterView(BaseView): + template_name = 'register.html' + page_title = 'Register'