[TASK] Wrap login and logout for proper templateing.

This commit is contained in:
Jan Philipp Timme 2014-07-27 23:34:23 +02:00
parent b233e3ea90
commit 134d7288a0
7 changed files with 92 additions and 29 deletions

View File

@ -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

View File

@ -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)),
)

View File

@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Merging Citavi projects and mapping existing datasets.">
<title>{% block title %}{{page_name}} :: {{page_title}}{% endblock %}</title>
<title>{% block title %}{{page.name}} :: {{page.title}}{% endblock %}</title>
<!-- Bootstrap -->
{% compressed_css 'bootstrap' %}
{% compressed_js 'bootstrap' %}
@ -16,18 +16,24 @@
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">{{page_name}}</a>
<a class="navbar-brand" href="/">{{page.name}}</a>
<ul class="nav navbar-nav">
<li><a href="/">Index</a></li>
<li><a href="/register">Register</a></li>
<li><a href="/login">Login</a></li>
<li>
{% if user.is_anonymous %}
<a href="/login">Login</a>
{% else %}
<a href="/logout">Logout</a>
{% endif %}
</li>
</ul>
</div>
</div>
</nav>
<div class="page-header">
<div id="header" class="container">
<h1>{{page_title}}</h1>
<h1>{{page.title}}</h1>
</div>
</div>
<div id="content" class="container">

View File

@ -1,6 +1,6 @@
{% extends "layout/base.html" %}
{% block content %}
<p>
REGISTER FORM HERE
Please register here.
</p>
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends "layout/base.html" %}
{% block content %}
<p>
LOGIN FORM HERE
You are logged out. Goodbye!
</p>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "layout/base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password did not match. Please try again.</p>
{% endif %}
<p>
<form method="POST" action="{% url 'frontend-login' %}">
{% csrf_token %}
{{form.username.label_tag}} {{form.username}}<br>
{{form.password.label_tag}} {{form.password}}
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next}}" />
</form>
</p>
{% endblock %}

View File

@ -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'