From 13cc1c3f579335a83747c1469a6e5aafef419fe0 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Fri, 1 Apr 2022 21:41:51 +0200 Subject: [PATCH] Added authentication --- container/views.py | 28 +++---- homelog/settings.py | 2 + homelog/templates/base.html | 7 +- .../templates/registration/logged_out.html | 18 +++++ homelog/templates/registration/login.html | 76 +++++++++++++++++++ homelog/urls.py | 2 +- 6 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 homelog/templates/registration/logged_out.html create mode 100644 homelog/templates/registration/login.html diff --git a/container/views.py b/container/views.py index fdae1ef..a1eae54 100644 --- a/container/views.py +++ b/container/views.py @@ -2,11 +2,13 @@ from django.views import generic from .models import Container, ContainerType import logging, json, re from django.core.exceptions import ObjectDoesNotExist +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin logger = logging.getLogger(__name__) -class ContainerListView(generic.ListView): +class ContainerListView(LoginRequiredMixin, generic.ListView): model = Container template_name = 'container/container_list.html' context_object_name = 'container_list' @@ -19,7 +21,7 @@ class ContainerListView(generic.ListView): ''' -class ContainerCreateView(generic.CreateView): +class ContainerCreateView(LoginRequiredMixin, generic.CreateView): model = Container # template_name = 'container/detail.html' fields = ['named_id', 'description', 'color', 'container_type'] @@ -30,7 +32,7 @@ class ContainerCreateView(generic.CreateView): return super().form_valid(form) -class ContainerImportView(generic.TemplateView): +class ContainerImportView(LoginRequiredMixin, generic.TemplateView): template_name = 'container_import.html' def get_context_data(self, **kwargs): @@ -47,7 +49,7 @@ class ContainerImportView(generic.TemplateView): return super().render_to_response(self) -class ContainerUpdateView(generic.UpdateView): +class ContainerUpdateView(LoginRequiredMixin, generic.UpdateView): model = Container # template_name = 'container/detail.html' fields = ['named_id', 'description', 'color', 'container_type'] @@ -57,15 +59,15 @@ class ContainerUpdateView(generic.UpdateView): return super().form_valid(form) -class ContainerDetailView(generic.DetailView): +class ContainerDetailView(LoginRequiredMixin, generic.DetailView): model = Container -class ContainerDeleteView(generic.DetailView): +class ContainerDeleteView(LoginRequiredMixin, generic.DetailView): model = Container -class ContainerPrintLabelView(generic.DetailView): +class ContainerPrintLabelView(LoginRequiredMixin, generic.DetailView): model = Container template_name = 'container/container_print_label.html' @@ -160,7 +162,7 @@ class ContainerPrintLabelView(generic.DetailView): return context -class ContainerTypeListView(generic.ListView): +class ContainerTypeListView(LoginRequiredMixin, generic.ListView): template_name = 'container/container_type_list.html' context_object_name = 'container_type_list' paginate_by = 20 @@ -173,13 +175,13 @@ class ContainerTypeListView(generic.ListView): ''' -class ContainerTypeDetailView(generic.DetailView): +class ContainerTypeDetailView(LoginRequiredMixin, generic.DetailView): model = ContainerType context_object_name = 'container_type' template_name = 'container/container_type_detail.html' -class ContainerTypeCreateView(generic.CreateView): +class ContainerTypeCreateView(LoginRequiredMixin, generic.CreateView): model = ContainerType template_name = 'container/container_type_form.html' fields = ['named_id', 'description', 'width', 'length', 'height', 'inner_width', 'inner_length', 'inner_height', @@ -191,7 +193,7 @@ class ContainerTypeCreateView(generic.CreateView): return super().form_valid(form) -class ContainerTypeUpdateView(generic.UpdateView): +class ContainerTypeUpdateView(LoginRequiredMixin, generic.UpdateView): model = ContainerType # template_name = 'container/detail.html' template_name = 'container/container_type_form.html' @@ -203,11 +205,11 @@ class ContainerTypeUpdateView(generic.UpdateView): return super().form_valid(form) -class ContainerTypeDeleteView(generic.DetailView): +class ContainerTypeDeleteView(LoginRequiredMixin, generic.DetailView): model = ContainerType -class ContainerTypeImportView(generic.TemplateView): +class ContainerTypeImportView(LoginRequiredMixin, generic.TemplateView): template_name = 'container/container_type_import.html' def get_context_data(self, **kwargs): diff --git a/homelog/settings.py b/homelog/settings.py index 1bec30f..0f06615 100644 --- a/homelog/settings.py +++ b/homelog/settings.py @@ -132,3 +132,5 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + +LOGIN_URL = '/accounts/login/' diff --git a/homelog/templates/base.html b/homelog/templates/base.html index 9e05f2f..6f8077b 100644 --- a/homelog/templates/base.html +++ b/homelog/templates/base.html @@ -61,12 +61,11 @@ {% if request.user.is_authenticated %} {% else %} {% if ACCOUNT_ALLOW_REGISTRATION %} @@ -77,7 +76,7 @@ {% endif %} {% endif %} diff --git a/homelog/templates/registration/logged_out.html b/homelog/templates/registration/logged_out.html new file mode 100644 index 0000000..e5e976e --- /dev/null +++ b/homelog/templates/registration/logged_out.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block breadcrumbs %}{% endblock %} + +{% block nav-sidebar %}{% endblock %} + +{% block content %} + +
+ {% translate "Thanks for spending some quality time with the web site today." %} +
+ +
+ {% translate 'Log in again' %} +
+ +{% endblock %} diff --git a/homelog/templates/registration/login.html b/homelog/templates/registration/login.html new file mode 100644 index 0000000..4738bff --- /dev/null +++ b/homelog/templates/registration/login.html @@ -0,0 +1,76 @@ +{% extends "base.html" %} +{% load i18n static %} + +{% block extrastyle %}{{ block.super }} +{{ form.media }} +{% endblock %} + +{% block bodyclass %}{{ block.super }} login{% endblock %} + +{% block usertools %}{% endblock %} + +{% block nav-global %}{% endblock %} + +{% block nav-sidebar %}{% endblock %} + +{% block content_title %}{% endblock %} + +{% block breadcrumbs %}{% endblock %} + +{% block content %} +{% if form.errors and not form.non_field_errors %} +

+{% if form.errors.items|length == 1 %}{% translate "Please correct the error below." %}{% else %}{% translate "Please correct the errors below." %}{% endif %} +

+{% endif %} + +{% if form.non_field_errors %} +{% for error in form.non_field_errors %} +

+ {{ error }} +

+{% endfor %} +{% endif %} + +
+ +{% if user.is_authenticated %} +

+{% blocktranslate trimmed %} + You are authenticated as {{ username }}, but are not authorized to + access this page. Would you like to login to a different account? +{% endblocktranslate %} +

+{% endif %} +
+
{% csrf_token %} +
+
+ {{ form.username.errors }} + {{ form.username.label_tag }} {{ form.username }} +
+
+
+
+ {{ form.password.errors }} + {{ form.password.label_tag }} {{ form.password }} + +
+
+ {% url 'admin_password_reset' as password_reset_url %} + {% if password_reset_url %} + + {% endif %} +
+
+ +
+
+
+
+ + +
+{% endblock %} diff --git a/homelog/urls.py b/homelog/urls.py index 857b49c..bae9990 100644 --- a/homelog/urls.py +++ b/homelog/urls.py @@ -29,5 +29,5 @@ urlpatterns = [ path('container/', include('container.urls')), path('asset/', include('asset.urls')), path('admin/', admin.site.urls), - path('accounts/login/', auth_views.LoginView.as_view(), name='account_login'), + path('accounts/', include('django.contrib.auth.urls')), ]