From c2a608ba397e88fd452b92b8aaee64b6682b102c Mon Sep 17 00:00:00 2001 From: Yulia Pylypets Date: Tue, 26 Nov 2024 10:45:21 +0200 Subject: [PATCH] fix --- taxi/models.py | 8 ----- taxi/urls.py | 6 ++-- taxi/views.py | 82 +++++++++----------------------------------------- 3 files changed, 17 insertions(+), 79 deletions(-) diff --git a/taxi/models.py b/taxi/models.py index 41a36e16..76fa4a16 100644 --- a/taxi/models.py +++ b/taxi/models.py @@ -32,11 +32,3 @@ class Car(models.Model): def __str__(self): return self.model - - -class VisitCounter(models.Model): - num_visits = models.PositiveIntegerField(default=0) - - def increment(self): - self.num_visits += 1 - self.save() diff --git a/taxi/urls.py b/taxi/urls.py index 9d29c3a9..84301fd9 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -6,8 +6,6 @@ DriverListView, DriverDetailView, ManufacturerListView, - login_view, - logout_view, ) from django.contrib.auth import views as auth_views @@ -21,8 +19,8 @@ name="driver-list"), path("drivers//", DriverDetailView.as_view(), name="driver-detail"), - path("logout/", logout_view, name="logout"), - path("taxi/login/", auth_views.LoginView.as_view(), name="login"), + path("login/", auth_views.LoginView.as_view(), name="login"), + path("logout/", auth_views.LogoutView.as_view(), name="logout"), ] app_name = "taxi" diff --git a/taxi/views.py b/taxi/views.py index 06597758..0c634933 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -1,25 +1,18 @@ -from django.shortcuts import render, redirect -from django.contrib.auth import login, logout -from django.contrib.auth.forms import AuthenticationForm +from django.shortcuts import render from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin from django.views import generic from .models import Driver, Car, Manufacturer +@login_required(login_url="taxi:login") def index(request): - if not request.user.is_authenticated: - return redirect("taxi:login") - num_drivers = Driver.objects.count() num_cars = Car.objects.count() num_manufacturers = Manufacturer.objects.count() - if "num_visits" not in request.session: - request.session["num_visits"] = 1 - else: - request.session["num_visits"] += 1 - - num_visits = request.session["num_visits"] + num_visits = request.session.get("num_visits", 0) + 1 + request.session["num_visits"] = num_visits context = { "num_drivers": num_drivers, @@ -31,78 +24,33 @@ def index(request): return render(request, "taxi/index.html", context=context) -@login_required -def driver_detail(request, pk): - driver = Driver.objects.get(pk=pk) - return render(request, "taxi/driver_detail.html", {"driver": driver}) - - -def login_view(request): - if request.method == "POST": - form = AuthenticationForm(request, data=request.POST) - if form.is_valid(): - user = form.get_user() - login(request, user) - return redirect("taxi:index") - else: - form = AuthenticationForm() - - return render(request, "taxi/../templates/registration/login.html", - {"form": form}) - - -def logout_view(request): - logout(request) - return redirect("taxi:index") - - -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 + login_url = "taxi:login" - def get(self, request, *args, **kwargs): - if not request.user.is_authenticated: - return redirect("taxi:login") - return super().get(request, *args, **kwargs) - -class CarListView(generic.ListView): +class CarListView(LoginRequiredMixin, generic.ListView): model = Car paginate_by = 5 queryset = Car.objects.select_related("manufacturer").order_by("id") - - def get(self, request, *args, **kwargs): - if not request.user.is_authenticated: - return redirect("taxi:login") - return super().get(request, *args, **kwargs) + login_url = "taxi:login" -class CarDetailView(generic.DetailView): +class CarDetailView(LoginRequiredMixin, generic.DetailView): model = Car + login_url = "taxi:login" - def get(self, request, *args, **kwargs): - if not request.user.is_authenticated: - return redirect("taxi:login") - return super().get(request, *args, **kwargs) - -class DriverListView(generic.ListView): +class DriverListView(LoginRequiredMixin, generic.ListView): model = Driver paginate_by = 5 - - def get(self, request, *args, **kwargs): - if not request.user.is_authenticated: - return redirect("taxi:login") - return super().get(request, *args, **kwargs) + login_url = "taxi:login" -class DriverDetailView(generic.DetailView): +class DriverDetailView(LoginRequiredMixin, generic.DetailView): model = Driver queryset = Driver.objects.prefetch_related("cars__manufacturer") - - def get(self, request, *args, **kwargs): - if not request.user.is_authenticated: - return redirect("taxi:login") - return super().get(request, *args, **kwargs) + login_url = "taxi:login"