From 683a6552257a038b997edb3634edcd286fdfe42a Mon Sep 17 00:00:00 2001 From: Denys Raboshchuk Date: Tue, 10 Oct 2023 14:25:05 +0300 Subject: [PATCH 1/4] Done! --- taxi/views.py | 17 +++++++++++------ taxi_service/settings.py | 21 ++++++++++++--------- taxi_service/urls.py | 3 +++ templates/includes/sidebar.html | 9 +++++++++ templates/registration/logged_out.html | 5 +++++ templates/registration/login.html | 17 +++++++++++++++++ templates/taxi/driver_list.html | 8 +++++++- templates/taxi/index.html | 1 + 8 files changed, 65 insertions(+), 16 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..7ea734ce 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,47 +1,52 @@ +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render from django.views import generic 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": 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..a614de60 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -15,7 +15,6 @@ # Build paths inside the project like this: BASE_DIR / "subdir". BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ @@ -29,7 +28,6 @@ ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -39,11 +37,13 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + "debug_toolbar", "taxi", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", + "debug_toolbar.middleware.DebugToolbarMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", @@ -72,7 +72,6 @@ WSGI_APPLICATION = "taxi_service.wsgi.application" - # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases @@ -83,26 +82,25 @@ } } - # Password validation # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { "NAME": "django.contrib.auth.password_validation." - "UserAttributeSimilarityValidator", + "UserAttributeSimilarityValidator", }, { "NAME": "django.contrib.auth.password_validation." - "MinimumLengthValidator", + "MinimumLengthValidator", }, { "NAME": "django.contrib.auth.password_validation." - "CommonPasswordValidator", + "CommonPasswordValidator", }, { "NAME": "django.contrib.auth.password_validation." - "NumericPasswordValidator", + "NumericPasswordValidator", }, ] @@ -119,7 +117,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ @@ -133,3 +130,9 @@ # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +INTERNAL_IPS = [ + "127.0.0.1", +] + +LOGIN_REDIRECT_URL = "/" \ No newline at end of file diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 8b94449f..dcc96c4e 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -19,7 +19,10 @@ from django.conf.urls.static import static + urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), + path("accounts/", include("django.contrib.auth.urls")), + path("__debug__/", include("debug_toolbar.urls")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/templates/includes/sidebar.html b/templates/includes/sidebar.html index b7cd72dc..c5ab7ee4 100644 --- a/templates/includes/sidebar.html +++ b/templates/includes/sidebar.html @@ -1,4 +1,13 @@ +

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

{% endblock %} From 0fc65164665c1b043450124dd5e435ece5c248b7 Mon Sep 17 00:00:00 2001 From: Denys Raboshchuk Date: Tue, 10 Oct 2023 14:27:34 +0300 Subject: [PATCH 2/4] Fix flake8 errors --- taxi/views.py | 1 + taxi_service/settings.py | 2 +- taxi_service/urls.py | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/taxi/views.py b/taxi/views.py index 7ea734ce..e859ab7a 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -5,6 +5,7 @@ from .models import Driver, Car, Manufacturer + @login_required def index(request): """View function for the home page of the site.""" diff --git a/taxi_service/settings.py b/taxi_service/settings.py index a614de60..fa35db7d 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -135,4 +135,4 @@ "127.0.0.1", ] -LOGIN_REDIRECT_URL = "/" \ No newline at end of file +LOGIN_REDIRECT_URL = "/" diff --git a/taxi_service/urls.py b/taxi_service/urls.py index dcc96c4e..a2341345 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -19,7 +19,6 @@ from django.conf.urls.static import static - urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), From 3f3fa35ffe61538719e8f7b6c689e014f453f21f Mon Sep 17 00:00:00 2001 From: Denys Raboshchuk Date: Tue, 10 Oct 2023 14:32:34 +0300 Subject: [PATCH 3/4] Remove debug_toolbar --- taxi_service/settings.py | 2 -- taxi_service/urls.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/taxi_service/settings.py b/taxi_service/settings.py index fa35db7d..2cd06764 100644 --- a/taxi_service/settings.py +++ b/taxi_service/settings.py @@ -37,13 +37,11 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "debug_toolbar", "taxi", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", - "debug_toolbar.middleware.DebugToolbarMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", diff --git a/taxi_service/urls.py b/taxi_service/urls.py index a2341345..4b1cbda4 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -19,9 +19,9 @@ from django.conf.urls.static import static + urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")), path("accounts/", include("django.contrib.auth.urls")), - path("__debug__/", include("debug_toolbar.urls")), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) From 2a82fa1c726f2177f1caff1b22ddb4bdfbb6faf2 Mon Sep 17 00:00:00 2001 From: Denys Raboshchuk Date: Tue, 10 Oct 2023 14:33:02 +0300 Subject: [PATCH 4/4] Remove debug_toolbar --- taxi_service/urls.py | 1 - 1 file changed, 1 deletion(-) diff --git a/taxi_service/urls.py b/taxi_service/urls.py index 4b1cbda4..baf83d1a 100644 --- a/taxi_service/urls.py +++ b/taxi_service/urls.py @@ -19,7 +19,6 @@ from django.conf.urls.static import static - urlpatterns = [ path("admin/", admin.site.urls), path("", include("taxi.urls", namespace="taxi")),