From 9fd2e3c5aa59f6a3215ecc3cbdad415a2dba569f Mon Sep 17 00:00:00 2001 From: Artem Putsenko Date: Tue, 14 Nov 2023 20:53:37 +0200 Subject: [PATCH 1/3] 'Solution' --- taxi/views.py | 14 ++++++++++---- taxi_service/settings.py | 2 ++ taxi_service/urls.py | 1 + templates/includes/sidebar.html | 6 ++++++ templates/registration/logged_out.html | 7 +++++++ templates/registration/login.html | 16 ++++++++++++++++ templates/taxi/driver_list.html | 4 +++- templates/taxi/index.html | 2 +- 8 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 templates/registration/logged_out.html create mode 100644 templates/registration/login.html diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..8098b7b8 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,26 +1,32 @@ from django.shortcuts import render from django.views import generic +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin from .models import Driver, Car, Manufacturer +@login_required def index(request): """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": request.session["num_visits"] } 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" @@ -33,15 +39,15 @@ class CarListView(generic.ListView): 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..cc71a76f 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -108,6 +108,8 @@ AUTH_USER_MODEL = "taxi.Driver" +LOGIN_REDIRECT_URL = "/" + # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ 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..cbbdba12 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,10 @@ diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..cc443561 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -1,5 +1,4 @@ {% extends "base.html" %} - {% block content %}

Taxi Service Home

Welcome to Best Taxi Ever!

@@ -10,4 +9,5 @@

Dynamic content

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

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

    {% endblock %} From 1da28041d2b21a9e8e22d65b8bf67b77e5c01a09 Mon Sep 17 00:00:00 2001 From: Artem Putsenko Date: Thu, 16 Nov 2023 20:44:50 +0200 Subject: [PATCH 2/3] 'Solution' --- .github/workflows/test.yml | 1 - taxi_service/urls.py | 2 +- templates/includes/sidebar.html | 49 +++++++++++++++++++++----- templates/registration/logged_out.html | 5 ++- templates/registration/login.html | 21 ++++++----- templates/taxi/driver_list.html | 6 ++-- 6 files changed, 56 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index de9ca5f8..22334584 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,4 +30,3 @@ jobs: - name: Run tests timeout-minutes: 5 run: python manage.py test - diff --git a/taxi_service/urls.py b/taxi_service/urls.py index baf83d1a..5d781485 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -22,5 +22,5 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), - path("accounts/", include("django.contrib.auth.urls")), + 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 cbbdba12..706ac966 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,12 +1,45 @@ - From 579e7129d2a5fecfd32a23ee0e35b41af173332c Mon Sep 17 00:00:00 2001 From: Artem Putsenko Date: Thu, 16 Nov 2023 20:59:11 +0200 Subject: [PATCH 3/3] 'Solution' --- taxi/views.py | 6 +++--- taxi_service_db_data.json | 2 +- templates/taxi/index.html | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/taxi/views.py b/taxi/views.py index 8098b7b8..4285d25e 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,6 +1,6 @@ +from django.contrib.auth.decorators import login_required from django.shortcuts import render from django.views import generic -from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from .models import Driver, Car, Manufacturer @@ -20,7 +20,7 @@ def index(request): "num_drivers": num_drivers, "num_cars": num_cars, "num_manufacturers": num_manufacturers, - "num_visits": request.session["num_visits"] + "num_visits": num_visits + 1 } return render(request, "taxi/index.html", context=context) @@ -33,7 +33,7 @@ class ManufacturerListView(LoginRequiredMixin, generic.ListView): paginate_by = 5 -class CarListView(generic.ListView): +class CarListView(LoginRequiredMixin, generic.ListView): model = Car paginate_by = 5 queryset = Car.objects.select_related("manufacturer") diff --git a/taxi_service_db_data.json b/taxi_service_db_data.json index 252fcf94..34679866 100644 --- a/taxi_service_db_data.json +++ b/taxi_service_db_data.json @@ -500,4 +500,4 @@ ] } } -] +] \ No newline at end of file diff --git a/templates/taxi/index.html b/templates/taxi/index.html index cc443561..4f977573 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -1,4 +1,5 @@ {% extends "base.html" %} + {% block content %}

    Taxi Service Home

    Welcome to Best Taxi Ever!

    @@ -9,5 +10,5 @@

    Dynamic content

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

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

    +

    You've visited this page: {{ num_visits }} time{{ num_visits|pluralize}}.

    {% endblock %}