From 38bc5f47a8347bdef20ac6a0d3edec5ba5d6c0b1 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Thu, 3 Feb 2022 16:07:01 +0100 Subject: [PATCH] Add basic login form --- config/settings/base.py | 21 ++++++----- config/urls.py | 4 +++ vbv_lernwelt/core/__init__.py | 0 vbv_lernwelt/core/admin.py | 3 ++ vbv_lernwelt/core/apps.py | 6 ++++ vbv_lernwelt/core/middleware.py | 40 +++++++++++++++++++++ vbv_lernwelt/core/migrations/__init__.py | 0 vbv_lernwelt/core/models.py | 3 ++ vbv_lernwelt/core/templates/core/login.html | 8 +++++ vbv_lernwelt/core/tests.py | 3 ++ vbv_lernwelt/core/views.py | 3 ++ 11 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 vbv_lernwelt/core/__init__.py create mode 100644 vbv_lernwelt/core/admin.py create mode 100644 vbv_lernwelt/core/apps.py create mode 100644 vbv_lernwelt/core/middleware.py create mode 100644 vbv_lernwelt/core/migrations/__init__.py create mode 100644 vbv_lernwelt/core/models.py create mode 100644 vbv_lernwelt/core/templates/core/login.html create mode 100644 vbv_lernwelt/core/tests.py create mode 100644 vbv_lernwelt/core/views.py diff --git a/config/settings/base.py b/config/settings/base.py index 83b912bc..a1c5565c 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -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", diff --git a/config/urls.py b/config/urls.py index e4816a7e..fe70a6f4 100644 --- a/config/urls.py +++ b/config/urls.py @@ -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 diff --git a/vbv_lernwelt/core/__init__.py b/vbv_lernwelt/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vbv_lernwelt/core/admin.py b/vbv_lernwelt/core/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/vbv_lernwelt/core/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/vbv_lernwelt/core/apps.py b/vbv_lernwelt/core/apps.py new file mode 100644 index 00000000..8115ae60 --- /dev/null +++ b/vbv_lernwelt/core/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class CoreConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'core' diff --git a/vbv_lernwelt/core/middleware.py b/vbv_lernwelt/core/middleware.py new file mode 100644 index 00000000..eae2b0ce --- /dev/null +++ b/vbv_lernwelt/core/middleware.py @@ -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 diff --git a/vbv_lernwelt/core/migrations/__init__.py b/vbv_lernwelt/core/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/vbv_lernwelt/core/models.py b/vbv_lernwelt/core/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/vbv_lernwelt/core/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/vbv_lernwelt/core/templates/core/login.html b/vbv_lernwelt/core/templates/core/login.html new file mode 100644 index 00000000..1bd79dcd --- /dev/null +++ b/vbv_lernwelt/core/templates/core/login.html @@ -0,0 +1,8 @@ +
+

Login

+
+ {% csrf_token %} + {{ form }} + +
+
diff --git a/vbv_lernwelt/core/tests.py b/vbv_lernwelt/core/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/vbv_lernwelt/core/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/vbv_lernwelt/core/views.py b/vbv_lernwelt/core/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/vbv_lernwelt/core/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here.