Switch from using the admin login/logout pages to custom 'lemonauth' pages

This commit is contained in:
Danielle McLean 2017-10-24 23:50:57 +11:00
parent 1670b6d427
commit c210650ca7
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
12 changed files with 89 additions and 3 deletions

0
lemonauth/__init__.py Normal file
View file

View file

@ -0,0 +1,4 @@
.container
display flex
flex-direction column
justify-content center

View file

@ -0,0 +1,46 @@
{% extends 'lemoncurry/layout.html' %}
{% load lemonauth_tags static %}
{% block title %}log in ~ {{ block.super }}{% endblock %}
{% block styles %}
<link rel="stylesheet" type="text/stylus" href="{% static 'lemonauth/css/login.styl' %}" />
{% endblock %}
{% block main %}
<div class="container">
{% if form.errors %}
<p class="alert alert-danger">
<strong>Uh oh!</strong> Your username and password didn't match. Please try again.
</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p class="alert alert-warning">
<strong>Hang on!</strong> Your account doesn't have access to this page. To proceed, please log in to an account that does have access.
</p>
{% else %}
<p class="alert alert-warning">
<strong>Oops!</strong> Please log in to see this page.
</p>
{% endif %}
{% endif %}
<form class="card" method="post" action="{% url 'lemonauth:login' %}">
<div class="card-body">
{% form_field form.username %}
{% form_field form.password %}
</div>
<div class="card-footer">
<button class="btn btn-primary" type="submit">
<i class="fa fa-sign-in"></i>
log in
</button>
</div>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
{% endblock %}

View file

@ -0,0 +1,6 @@
<div class="form-group{% if field.errors %} has-error{% endif %}">
<label for="{{ field.id_for_label }}">{{ field.label | lower }}</label>
<input id="{{ field.id_for_label }}" class="form-control" type="{{ field.field.widget.input_type }}"
name="{{ field.html_name }}"{% if field.value %} value="{{ field.value }}"{% endif %}{% if field.help_text %} aria-describedby="{{ field.id_for_label }}-help"{% endif %} />
{% if field.help_text %}<p id="{{ field.id_for_label }}-help" class="form-text">{{ field.help_text }}</p>{% endif %}
</div>

View file

View file

@ -0,0 +1,8 @@
from django import template
register = template.Library()
@register.inclusion_tag('lemonauth/tags/form_field.html')
def form_field(field):
return {'field': field}

8
lemonauth/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.conf.urls import url
from . import views
app_name = 'lemonauth'
urlpatterns = [
url('^login$', views.login, name='login'),
url('^logout$', views.logout, name='logout'),
]

7
lemonauth/views.py Normal file
View file

@ -0,0 +1,7 @@
from django.contrib.auth import views as auth_views
login = auth_views.LoginView.as_view(
template_name='lemonauth/login.html',
redirect_authenticated_user=True,
)
logout = auth_views.LogoutView.as_view()

View file

@ -70,6 +70,7 @@ INSTALLED_APPS = [
'lemoncurry', 'lemoncurry',
'home', 'home',
'lemonauth',
'users', 'users',
'wellknowns', 'wellknowns',
] ]
@ -147,6 +148,10 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
LOGIN_URL = 'lemonauth:login'
LOGIN_REDIRECT_URL = 'home:index'
LOGOUT_REDIRECT_URL = LOGIN_REDIRECT_URL
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/ # https://docs.djangoproject.com/en/1.11/topics/i18n/

View file

@ -1,7 +1,7 @@
{% load compress favtags lemoncurry_tags meta static %}<!doctype html> {% load compress favtags lemoncurry_tags meta static %}<!doctype html>
<html lang="en" class="{% block html_class %}{% endblock %}"> <html lang="en" class="{% block html_class %}{% endblock %}">
<head{% meta_namespaces %}> <head{% meta_namespaces %}>
<title class="p-name">{% site_name %}</title> <title class="p-name">{% block title %}{% site_name %}{% endblock %}</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
{% include 'meta/meta.html' %} {% include 'meta/meta.html' %}
@ -11,6 +11,7 @@
integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous" /> integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous" />
{% compress css %} {% compress css %}
<link rel="stylesheet" type="text/stylus" href="{% static 'lemoncurry/css/layout.styl' %}" /> <link rel="stylesheet" type="text/stylus" href="{% static 'lemoncurry/css/layout.styl' %}" />
{% block styles %}{% endblock %}
{% endcompress %} {% endcompress %}
<script type="text/javascript" src="https://use.fontawesome.com/4fbab4ae27.js"></script> <script type="text/javascript" src="https://use.fontawesome.com/4fbab4ae27.js"></script>
</head> </head>

View file

@ -43,10 +43,10 @@ def nav_right(request):
if request.user.is_authenticated(): if request.user.is_authenticated():
items = ( items = (
MenuItem(label='admin', icon='fa fa-gear', url='admin:index'), MenuItem(label='admin', icon='fa fa-gear', url='admin:index'),
MenuItem(label='log out', icon='fa fa-sign-out', url='admin:logout'), MenuItem(label='log out', icon='fa fa-sign-out', url='lemonauth:logout'),
) )
else: else:
items = ( items = (
MenuItem(label='log in', icon='fa fa-sign-in', url='admin:login'), MenuItem(label='log in', icon='fa fa-sign-in', url='lemonauth:login'),
) )
return {'items': items} return {'items': items}

View file

@ -28,6 +28,7 @@ urlpatterns = [
url('', include('home.urls')), url('', include('home.urls')),
url('^.well-known/', include('wellknowns.urls')), url('^.well-known/', include('wellknowns.urls')),
url('^admin/', admin.site.urls), url('^admin/', admin.site.urls),
url('^auth/', include('lemonauth.urls')),
url(r'^sitemap\.xml$', sitemap.index, maps), url(r'^sitemap\.xml$', sitemap.index, maps),
url(r'^sitemaps/(?P<section>.+)\.xml$', sitemap.sitemap, maps, url(r'^sitemaps/(?P<section>.+)\.xml$', sitemap.sitemap, maps,