Add basic login form

This commit is contained in:
Daniel Egger 2022-02-03 16:07:01 +01:00
parent 1c340aace3
commit 38bc5f47a8
11 changed files with 82 additions and 9 deletions

View File

@ -7,15 +7,17 @@ from pathlib import Path
import structlog
from environs import Env
ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
SERVER_ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
# vbv_lernwelt/
APPS_DIR = ROOT_DIR / "vbv_lernwelt"
APPS_DIR = SERVER_ROOT_DIR / "vbv_lernwelt"
env = Env()
READ_DOT_ENV_FILE = env.bool("VBV_DJANGO_READ_DOT_ENV_FILE", default=False)
if READ_DOT_ENV_FILE:
# OS environment variables take precedence over variables from .env
env.read_env(str(ROOT_DIR / "example.env"))
env.read_env(str(SERVER_ROOT_DIR / "example.env"))
DJANGO_DEV_MODE = env("VBV_DJANGO_DEV_MODE", default="development")
# GENERAL
# ------------------------------------------------------------------------------
@ -37,7 +39,7 @@ USE_L10N = True
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths
LOCALE_PATHS = [str(ROOT_DIR / "locale")]
LOCALE_PATHS = [str(SERVER_ROOT_DIR / "locale")]
# DATABASES
# ------------------------------------------------------------------------------
@ -76,6 +78,7 @@ THIRD_PARTY_APPS = [
]
LOCAL_APPS = [
"vbv_lernwelt.core",
"vbv_lernwelt.users",
# Your stuff: custom apps go here
]
@ -96,9 +99,9 @@ AUTHENTICATION_BACKENDS = [
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model
AUTH_USER_MODEL = "users.User"
# https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url
LOGIN_REDIRECT_URL = "users:redirect"
# LOGIN_REDIRECT_URL = "users:redirect"
# https://docs.djangoproject.com/en/dev/ref/settings/#login-url
LOGIN_URL = "account_login"
LOGIN_URL = "/login/"
# PASSWORDS
# ------------------------------------------------------------------------------
@ -135,12 +138,13 @@ MIDDLEWARE = [
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.common.BrokenLinkEmailsMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"vbv_lernwelt.core.middleware.AuthenticationRequiredMiddleware",
]
# STATIC
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR / "staticfiles")
STATIC_ROOT = str(SERVER_ROOT_DIR / "staticfiles")
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
@ -295,7 +299,7 @@ else:
'handlers': {
'file': {
'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
'filename': f'{ROOT_DIR}/log/myservice.log',
'filename': f'{SERVER_ROOT_DIR}/log/myservice.log',
'maxBytes': 1024 * 1024 * 100,
'backupCount': 50,
'formatter': 'json',
@ -371,7 +375,6 @@ SPECTACULAR_SETTINGS = {
# Your stuff...
# ------------------------------------------------------------------------------
DJANGO_DEV_MODE = env("VBV_DJANGO_DEV_MODE", default="development")
SECRET_KEY = env(
"VBV_DJANGO_SECRET_KEY",

View File

@ -1,6 +1,7 @@
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import include, path
from django.views import defaults as default_views
@ -8,12 +9,15 @@ from django.views.generic import TemplateView
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from rest_framework.authtoken.views import obtain_auth_token
from vbv_lernwelt.core.middleware import django_view_authentication_exempt
urlpatterns = [
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
path("about/", TemplateView.as_view(template_name="pages/about.html"), name="about"),
# Django Admin, use {% url 'admin:index' %}
path(settings.ADMIN_URL, admin.site.urls),
# Your stuff: custom urls includes go here
path('login/', django_view_authentication_exempt(auth_views.LoginView.as_view(template_name='core/login.html'))),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# Static file serving when using Gunicorn + Uvicorn for local web socket development

View File

View File

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

View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'

View File

@ -0,0 +1,40 @@
from functools import wraps
from django.conf import settings
from django.contrib.auth.views import redirect_to_login
from django.utils.deprecation import MiddlewareMixin
class AuthenticationRequiredMiddleware(MiddlewareMixin):
def process_view(self, request, callback, callback_args, callback_kwargs):
if getattr(callback, 'authentication_exempt', False):
return None
if not request.user.is_authenticated:
return redirect_to_login(request.build_absolute_uri(), settings.LOGIN_URL)
return None
def django_view_authentication_exempt(view_func):
def wrapped_view(*args, **kwargs):
return view_func(*args, **kwargs)
wrapped_view.authentication_exempt = True
return wraps(view_func)(wrapped_view)
class DjangoViewAuthenticationExemptDRFViewMixin:
@classmethod
def as_view(cls, **initkwargs):
view = super(DjangoViewAuthenticationExemptDRFViewMixin, cls).as_view(**initkwargs)
view.authentication_exempt = True
return view
class DjangoViewAuthenticationExemptDRFViewSetMixin:
@classmethod
def as_view(cls, actions=None, **initkwargs):
view = super(DjangoViewAuthenticationExemptDRFViewSetMixin, cls).as_view(actions=actions, **initkwargs)
view.authentication_exempt = True
return view

View File

View File

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

View File

@ -0,0 +1,8 @@
<div class="container py-5">
<h1>Login</h1>
<form method="POST">
{% csrf_token %}
{{ form }}
<button class="btn btn-primary" type="submit">Login</button>
</form>
</div>

View File

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

View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.