From 71d51d74a0782e8c93854636b546fafed3982bbf Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 20 Sep 2024 13:47:36 +0300 Subject: [PATCH 1/6] Solution --- .gitignore | 6 +++ taxi/urls.py | 48 ++++++++++++++++---- taxi/views.py | 32 ++++++++++---- taxi_service/settings.py | 2 + taxi_service/urls.py | 1 + templates/base.html | 61 ++++++++++++-------------- templates/includes/sidebar.html | 17 +++++-- templates/registration/logged_out.html | 2 + templates/registration/login.html | 6 +++ templates/taxi/driver_list.html | 21 ++++----- templates/taxi/index.html | 13 +++--- 11 files changed, 141 insertions(+), 68 deletions(-) create mode 100644 templates/registration/logged_out.html create mode 100644 templates/registration/login.html diff --git a/.gitignore b/.gitignore index 4a7076c2..545b52f7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,9 @@ venv/ .venv/ .pytest_cache/ **__pycache__/ +*.pyc +**db.sqlite3 +.vs/ +.git/ +**__init__.py + diff --git a/taxi/urls.py b/taxi/urls.py index c663d6e2..8c5d7345 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,26 +1,58 @@ -from django.urls import path +from argparse import Namespace +from django.urls import path, include +from django.contrib.auth import views as auth_views -from .views import ( +from taxi.views import ( index, CarListView, CarDetailView, DriverListView, DriverDetailView, ManufacturerListView, + user_logout ) + urlpatterns = [ - path("", index, name="index"), + path( + "", + index, + name="index" + ), path( "manufacturers/", ManufacturerListView.as_view(), - name="manufacturer-list", + name="manufacturer-list" + ), + path( + "cars/", + CarListView.as_view(), + name="car-list" + ), + path( + "cars//", + CarDetailView.as_view(), + name="car-detail" + ), + path( + "drivers/", + DriverListView.as_view(), + name="driver-list" + ), + path( + "drivers//", + DriverDetailView.as_view(), + name="driver-detail" + ), + path( + "login/", + auth_views.LoginView.as_view(), + name="login" ), - path("cars/", CarListView.as_view(), name="car-list"), - path("cars//", CarDetailView.as_view(), name="car-detail"), - path("drivers/", DriverListView.as_view(), name="driver-list"), path( - "drivers//", DriverDetailView.as_view(), name="driver-detail" + "logout/", + user_logout, + name="logout" ), ] diff --git a/taxi/views.py b/taxi/views.py index 82ad312f..6f3abcbb 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,47 +1,63 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect from django.views import generic +from django.contrib.auth.decorators import login_required +from django.contrib.auth import logout +from django.contrib.auth.mixins import LoginRequiredMixin -from .models import Driver, Car, Manufacturer +from taxi.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 + + request.session.save() + 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" 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") + + +@login_required +def user_logout(request): + """View function to log out the user.""" + logout(request) + return redirect("taxi:index") diff --git a/taxi_service/settings.py b/taxi_service/settings.py index b6c0cf19..e8f0543a 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -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 = "/" 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/base.html b/templates/base.html index f4a0b6cf..6df5f463 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,40 +1,37 @@ - - {% block title %}Taxi Service{% endblock %} - - - - - {% load static %} - + {% block title %} + Taxi Service{% endblock %} + + + + {% load static %} + - -
-
-
- - {% block sidebar %} - {% include "includes/sidebar.html" %} - {% endblock %} - +
+
+
+ {% block sidebar %} + {% include "includes/sidebar.html" %} + {% endblock %} +
+
+ {% if user.is_authenticated %} +

Welcome, {{ user.username }}!

+ Logout + {% else %} + Login + {% endif %} + + {% block content %}{% endblock %} + + {% block pagination %} + {% include "includes/pagination.html" %} + {% endblock %} +
+
-
- - {% block content %}{% endblock %} - - {% block pagination %} - {% include "includes/pagination.html" %} - {% endblock %} - -
-
-
- diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..97da8105 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,6 +1,15 @@ diff --git a/templates/registration/logged_out.html b/templates/registration/logged_out.html new file mode 100644 index 00000000..a539e423 --- /dev/null +++ b/templates/registration/logged_out.html @@ -0,0 +1,2 @@ +

You have been logged out.

+Log in again diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 00000000..9541599c --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,6 @@ +

Login

+
+ {% csrf_token %} + {{ form.as_p }} + +
diff --git a/templates/taxi/driver_list.html b/templates/taxi/driver_list.html index c6c3e823..263fc1a2 100644 --- a/templates/taxi/driver_list.html +++ b/templates/taxi/driver_list.html @@ -1,17 +1,18 @@ {% extends "base.html" %} {% block content %} -

Driver list

+

Driver list

{% if driver_list %} -
    - {% for driver in driver_list%} -
  • - {{ driver.username }} - ({{ driver.first_name }} {{ driver.last_name }}) -
  • - {% endfor %} -
+
    + {% for driver in driver_list %} +
  • + {{ driver.username }} + ({{ driver.first_name }} {{ driver.last_name }}) + {% if driver == user %} (Me){% endif %} +
  • + {% endfor %} +
{% else %} -

There are no drivers in taxi

+

There are no drivers in taxi

{% endif %} {% endblock %} diff --git a/templates/taxi/index.html b/templates/taxi/index.html index 13c59aa8..09eeeab8 100644 --- a/templates/taxi/index.html +++ b/templates/taxi/index.html @@ -1,13 +1,14 @@ {% extends "base.html" %} {% block content %} -

Taxi Service Home

-

Welcome to Best Taxi Ever!

-

Dynamic content

-

The Taxi service has the following record counts:

-
    +

    Taxi Service Home

    +

    Welcome to Best Taxi Ever!

    +

    You have visited this page {{ num_visits }} times.

    +

    Dynamic content

    +

    The Taxi service has the following record counts:

    +
    • Cars: {{ num_cars }}
    • Drivers: {{ num_drivers }}
    • Manufacturers: {{ num_manufacturers }}
    • -
    +
{% endblock %} From 02519dbf808a48936e981cbc6257e81030bc8493 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 20 Sep 2024 15:58:24 +0300 Subject: [PATCH 2/6] Solution --- taxi/urls.py | 5 ++--- taxi/views.py | 12 +----------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/taxi/urls.py b/taxi/urls.py index 8c5d7345..37c1416e 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,5 +1,5 @@ from argparse import Namespace -from django.urls import path, include +from django.urls import path from django.contrib.auth import views as auth_views from taxi.views import ( @@ -9,7 +9,6 @@ DriverListView, DriverDetailView, ManufacturerListView, - user_logout ) @@ -51,7 +50,7 @@ ), path( "logout/", - user_logout, + auth_views.LogoutView.as_view(), name="logout" ), ] diff --git a/taxi/views.py b/taxi/views.py index 6f3abcbb..2a482982 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,7 +1,6 @@ -from django.shortcuts import render, redirect +from django.shortcuts import render from django.views import generic from django.contrib.auth.decorators import login_required -from django.contrib.auth import logout from django.contrib.auth.mixins import LoginRequiredMixin from taxi.models import Driver, Car, Manufacturer @@ -17,8 +16,6 @@ def index(request): num_visits = request.session.get("num_visits", 0) request.session["num_visits"] = num_visits + 1 - request.session.save() - context = { "num_drivers": num_drivers, "num_cars": num_cars, @@ -54,10 +51,3 @@ class DriverListView(LoginRequiredMixin, generic.ListView): class DriverDetailView(LoginRequiredMixin, generic.DetailView): model = Driver queryset = Driver.objects.prefetch_related("cars__manufacturer") - - -@login_required -def user_logout(request): - """View function to log out the user.""" - logout(request) - return redirect("taxi:index") From d031c72fd53186f8cec6d69038cc665dae3dc510 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 21 Sep 2024 18:40:55 +0300 Subject: [PATCH 3/6] Solution --- taxi/urls.py | 18 ++++++------------ templates/base.html | 2 +- templates/includes/sidebar.html | 2 +- .../{logged_out.html => logout.html} | 0 4 files changed, 8 insertions(+), 14 deletions(-) rename templates/registration/{logged_out.html => logout.html} (100%) diff --git a/taxi/urls.py b/taxi/urls.py index 37c1416e..2d42df27 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,6 +1,5 @@ -from argparse import Namespace from django.urls import path -from django.contrib.auth import views as auth_views +# from django.contrib.auth import views as auth_views from taxi.views import ( index, @@ -43,16 +42,11 @@ DriverDetailView.as_view(), name="driver-detail" ), - path( - "login/", - auth_views.LoginView.as_view(), - name="login" - ), - path( - "logout/", - auth_views.LogoutView.as_view(), - name="logout" - ), + # path( + # "logout/", + # auth_views.LogoutView.as_view(), + # name="logout" + # ), ] app_name = "taxi" diff --git a/templates/base.html b/templates/base.html index 6df5f463..f50c8725 100644 --- a/templates/base.html +++ b/templates/base.html @@ -20,7 +20,7 @@
{% if user.is_authenticated %}

Welcome, {{ user.username }}!

- Logout + Logout {% else %} Login {% endif %} diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index 97da8105..27333c2c 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -8,7 +8,7 @@
  • {{ user.username }}{% if user.id == current_user_id %} (Me){% endif %}
  • -
  • Logout
  • +
  • Logout
  • {% else %}
  • Login
  • {% endif %} diff --git a/templates/registration/logged_out.html b/templates/registration/logout.html similarity index 100% rename from templates/registration/logged_out.html rename to templates/registration/logout.html From 710919649f55e3272573ffee0e65ea035bd3d431 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 21 Sep 2024 18:41:40 +0300 Subject: [PATCH 4/6] Solution --- taxi/urls.py | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/taxi/urls.py b/taxi/urls.py index 2d42df27..2e2cfe09 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,7 +1,6 @@ from django.urls import path -# from django.contrib.auth import views as auth_views -from taxi.views import ( +from .views import ( index, CarListView, CarDetailView, @@ -12,41 +11,18 @@ urlpatterns = [ - path( - "", - index, - name="index" - ), + path("", index, name="index"), path( "manufacturers/", ManufacturerListView.as_view(), - name="manufacturer-list" - ), - path( - "cars/", - CarListView.as_view(), - name="car-list" - ), - path( - "cars//", - CarDetailView.as_view(), - name="car-detail" - ), - path( - "drivers/", - DriverListView.as_view(), - name="driver-list" + name="manufacturer-list", ), + path("cars/", CarListView.as_view(), name="car-list"), + path("cars//", CarDetailView.as_view(), name="car-detail"), + path("drivers/", DriverListView.as_view(), name="driver-list"), path( - "drivers//", - DriverDetailView.as_view(), - name="driver-detail" + "drivers//", DriverDetailView.as_view(), name="driver-detail" ), - # path( - # "logout/", - # auth_views.LogoutView.as_view(), - # name="logout" - # ), ] app_name = "taxi" From 48bc6559a2f10965151b634c515a9f90c516f8d0 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 21 Sep 2024 21:34:42 +0300 Subject: [PATCH 5/6] Solution --- templates/base.html | 9 +-------- templates/includes/sidebar.html | 9 +++++---- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/templates/base.html b/templates/base.html index f50c8725..9ac3f6eb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -17,14 +17,7 @@ {% include "includes/sidebar.html" %} {% endblock %}
    -
    - {% if user.is_authenticated %} -

    Welcome, {{ user.username }}!

    - Logout - {% else %} - Login - {% endif %} - +
    {% block content %}{% endblock %} {% block pagination %} diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index 27333c2c..4ec0ceb7 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,13 +1,14 @@