Add basic login form
This commit is contained in:
parent
1c340aace3
commit
38bc5f47a8
|
|
@ -7,15 +7,17 @@ from pathlib import Path
|
||||||
import structlog
|
import structlog
|
||||||
from environs import Env
|
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/
|
# vbv_lernwelt/
|
||||||
APPS_DIR = ROOT_DIR / "vbv_lernwelt"
|
APPS_DIR = SERVER_ROOT_DIR / "vbv_lernwelt"
|
||||||
env = Env()
|
env = Env()
|
||||||
|
|
||||||
READ_DOT_ENV_FILE = env.bool("VBV_DJANGO_READ_DOT_ENV_FILE", default=False)
|
READ_DOT_ENV_FILE = env.bool("VBV_DJANGO_READ_DOT_ENV_FILE", default=False)
|
||||||
if READ_DOT_ENV_FILE:
|
if READ_DOT_ENV_FILE:
|
||||||
# OS environment variables take precedence over variables from .env
|
# 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
|
# GENERAL
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
@ -37,7 +39,7 @@ USE_L10N = True
|
||||||
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
|
# https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
|
||||||
USE_TZ = True
|
USE_TZ = True
|
||||||
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths
|
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths
|
||||||
LOCALE_PATHS = [str(ROOT_DIR / "locale")]
|
LOCALE_PATHS = [str(SERVER_ROOT_DIR / "locale")]
|
||||||
|
|
||||||
# DATABASES
|
# DATABASES
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
@ -76,6 +78,7 @@ THIRD_PARTY_APPS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
LOCAL_APPS = [
|
LOCAL_APPS = [
|
||||||
|
"vbv_lernwelt.core",
|
||||||
"vbv_lernwelt.users",
|
"vbv_lernwelt.users",
|
||||||
# Your stuff: custom apps go here
|
# Your stuff: custom apps go here
|
||||||
]
|
]
|
||||||
|
|
@ -96,9 +99,9 @@ AUTHENTICATION_BACKENDS = [
|
||||||
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model
|
# https://docs.djangoproject.com/en/dev/ref/settings/#auth-user-model
|
||||||
AUTH_USER_MODEL = "users.User"
|
AUTH_USER_MODEL = "users.User"
|
||||||
# https://docs.djangoproject.com/en/dev/ref/settings/#login-redirect-url
|
# 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
|
# https://docs.djangoproject.com/en/dev/ref/settings/#login-url
|
||||||
LOGIN_URL = "account_login"
|
LOGIN_URL = "/login/"
|
||||||
|
|
||||||
# PASSWORDS
|
# PASSWORDS
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
@ -135,12 +138,13 @@ MIDDLEWARE = [
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.common.BrokenLinkEmailsMiddleware",
|
"django.middleware.common.BrokenLinkEmailsMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
|
"vbv_lernwelt.core.middleware.AuthenticationRequiredMiddleware",
|
||||||
]
|
]
|
||||||
|
|
||||||
# STATIC
|
# STATIC
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
|
# 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
|
# https://docs.djangoproject.com/en/dev/ref/settings/#static-url
|
||||||
STATIC_URL = "/static/"
|
STATIC_URL = "/static/"
|
||||||
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
|
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
|
||||||
|
|
@ -295,7 +299,7 @@ else:
|
||||||
'handlers': {
|
'handlers': {
|
||||||
'file': {
|
'file': {
|
||||||
'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
|
'class': 'concurrent_log_handler.ConcurrentRotatingFileHandler',
|
||||||
'filename': f'{ROOT_DIR}/log/myservice.log',
|
'filename': f'{SERVER_ROOT_DIR}/log/myservice.log',
|
||||||
'maxBytes': 1024 * 1024 * 100,
|
'maxBytes': 1024 * 1024 * 100,
|
||||||
'backupCount': 50,
|
'backupCount': 50,
|
||||||
'formatter': 'json',
|
'formatter': 'json',
|
||||||
|
|
@ -371,7 +375,6 @@ SPECTACULAR_SETTINGS = {
|
||||||
# Your stuff...
|
# Your stuff...
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
DJANGO_DEV_MODE = env("VBV_DJANGO_DEV_MODE", default="development")
|
|
||||||
|
|
||||||
SECRET_KEY = env(
|
SECRET_KEY = env(
|
||||||
"VBV_DJANGO_SECRET_KEY",
|
"VBV_DJANGO_SECRET_KEY",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.contrib.auth import views as auth_views
|
||||||
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
from django.views import defaults as default_views
|
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 drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
|
||||||
from rest_framework.authtoken.views import obtain_auth_token
|
from rest_framework.authtoken.views import obtain_auth_token
|
||||||
|
|
||||||
|
from vbv_lernwelt.core.middleware import django_view_authentication_exempt
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
|
path("", TemplateView.as_view(template_name="pages/home.html"), name="home"),
|
||||||
path("about/", TemplateView.as_view(template_name="pages/about.html"), name="about"),
|
path("about/", TemplateView.as_view(template_name="pages/about.html"), name="about"),
|
||||||
# Django Admin, use {% url 'admin:index' %}
|
# Django Admin, use {% url 'admin:index' %}
|
||||||
path(settings.ADMIN_URL, admin.site.urls),
|
path(settings.ADMIN_URL, admin.site.urls),
|
||||||
# Your stuff: custom urls includes go here
|
# 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)
|
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
# Static file serving when using Gunicorn + Uvicorn for local web socket development
|
# Static file serving when using Gunicorn + Uvicorn for local web socket development
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class CoreConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'core'
|
||||||
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Create your models here.
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
||||||
Loading…
Reference in New Issue