From c4cabca8087cb96fc63c29b9b1bb7e90abd70e72 Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Tue, 13 Aug 2024 16:46:58 +0300 Subject: [PATCH 1/7] Implemented solution --- .gitignore | 1 + taxi/views.py | 21 ++++++++++++++------- taxi_service/settings.py | 4 +++- taxi_service/urls.py | 1 + templates/includes/sidebar.html | 13 +++++++++++++ templates/registration/logged_out.html | 5 +++++ templates/registration/login.html | 14 ++++++++++++++ templates/taxi/driver_list.html | 3 +++ templates/taxi/index.html | 1 + 9 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 templates/registration/logged_out.html create mode 100644 templates/registration/login.html diff --git a/.gitignore b/.gitignore index b26d6116..a48fb488 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ venv/ .pytest_cache/ **__pycache__/ + diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..10908dfc 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,47 +1,54 @@ +from django.contrib.auth import logout +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin +from django.http import HttpRequest, HttpResponse from django.shortcuts import render from django.views import generic from .models import Driver, Car, Manufacturer -def index(request): +@login_required +def index(request: HttpRequest) -> HttpResponse: """View function for the home page of the site.""" num_drivers = Driver.objects.count() num_cars = Car.objects.count() num_manufacturers = Manufacturer.objects.count() - + num_visits = request.session.get("num_visits", 0) + request.session["num_visits"] = num_visits + 1 context = { "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, + "num_visits": num_visits + 1 } return render(request, "taxi/index.html", context=context) -class ManufacturerListView(generic.ListView): +class ManufacturerListView(LoginRequiredMixin, generic.ListView): model = Manufacturer context_object_name = "manufacturer_list" template_name = "taxi/manufacturer_list.html" paginate_by = 5 -class CarListView(generic.ListView): +class CarListView(LoginRequiredMixin, generic.ListView): model = Car paginate_by = 5 queryset = Car.objects.select_related("manufacturer") -class CarDetailView(generic.DetailView): +class CarDetailView(LoginRequiredMixin, generic.DetailView): model = Car -class DriverListView(generic.ListView): +class DriverListView(LoginRequiredMixin, generic.ListView): model = Driver paginate_by = 5 -class DriverDetailView(generic.DetailView): +class DriverDetailView(LoginRequiredMixin, generic.DetailView): model = Driver queryset = Driver.objects.prefetch_related("cars__manufacturer") diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b6c0cf19..422083c9 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -9,7 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ - +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / "subdir". @@ -133,3 +133,5 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +LOGIN_REDIRECT_URL = "/catalog/" diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..baf83d1a 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -22,4 +22,5 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), + path("accounts/", include("django.contrib.auth.urls")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..7419c971 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,17 @@ diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..10571c3a 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -10,4 +10,5 @@

Dynamic content

  • Drivers: {{ num_drivers }}
  • Manufacturers: {{ num_manufacturers }}
  • +

    You have visited this page {{ num_visits }} time{{num_visits|pluralize }}

    {% endblock %} From 64eb4a8e23a6ed91d67ff8410c5f3dbfac5970fe Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Tue, 13 Aug 2024 17:20:12 +0300 Subject: [PATCH 2/7] Fixed --- templates/includes/sidebar.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index 7419c971..84d15ca6 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,7 +1,7 @@ \ No newline at end of file From 7074a1398691ecf3836ce3062666d785b1451b35 Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Tue, 13 Aug 2024 17:41:43 +0300 Subject: [PATCH 4/7] Fixed --- templates/includes/sidebar.html | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index 8d4a99b3..7419c971 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,15 +1,19 @@ \ No newline at end of file + From f40ff2f8f78c7d16858806e634f9183792b0f81c Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Tue, 13 Aug 2024 17:42:23 +0300 Subject: [PATCH 5/7] Fixed --- taxi_service/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taxi_service/settings.py b/taxi_service/settings.py index bbcba102..422083c9 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -134,4 +134,4 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" -LOGIN_REDIRECT_URL = "/" +LOGIN_REDIRECT_URL = "/catalog/" From eab1e50d9c949adb3179b191ce87a830e00c3690 Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Tue, 13 Aug 2024 17:58:54 +0300 Subject: [PATCH 6/7] Fixed --- taxi_service/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taxi_service/settings.py b/taxi_service/settings.py index 422083c9..bbcba102 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -134,4 +134,4 @@ DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" -LOGIN_REDIRECT_URL = "/catalog/" +LOGIN_REDIRECT_URL = "/" From 600e7e0db9dd1f57039f997e9379efc1d390cbc8 Mon Sep 17 00:00:00 2001 From: Dmytro Kolchak Date: Mon, 9 Sep 2024 15:36:17 +0300 Subject: [PATCH 7/7] Fix --- taxi/views.py | 1 - taxi_service/settings.py | 1 - 2 files changed, 2 deletions(-) diff --git a/taxi/views.py b/taxi/views.py index 10908dfc..4e5ec24a 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,4 +1,3 @@ -from django.contrib.auth import logout from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpRequest, HttpResponse diff --git a/taxi_service/settings.py b/taxi_service/settings.py index bbcba102..973afd6b 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -9,7 +9,6 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ -import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / "subdir".