diff --git a/taxi/urls.py b/taxi/urls.py index 72b986e1..5ea662c3 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -1,32 +1,53 @@ -from django.contrib.auth import views as auth_views -from django.urls import path - -from . import views - -from .views import ( - index, - CarListView, - CarDetailView, - DriverListView, - DriverDetailView, - ManufacturerListView, -) - -urlpatterns = [ - 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("login/", auth_views.LoginView.as_view(), name="login"), - path("logout/", auth_views.LogoutView.as_view(), name="logout"), - path("drivers/", DriverListView.as_view(), name="driver-list"), - path( - "drivers//", DriverDetailView.as_view(), name="driver-detail" - ), -] - -app_name = "taxi" +from django.contrib.auth.decorators import login_required +from django.contrib.auth.mixins import LoginRequiredMixin +from django.utils.decorators import method_decorator +from django.shortcuts import render +from django.views import generic + +from .models import Driver, Car, Manufacturer + + +@login_required +def index(request): + num_visits = request.session.get("num_visits", 0) + request.session["num_visits"] = num_visits + 1 + + num_drivers = Driver.objects.count() + num_cars = Car.objects.count() + num_manufacturers = Manufacturer.objects.count() + + context = { + "num_drivers": num_drivers, + "num_cars": num_cars, + "num_manufacturers": num_manufacturers, + "num_visits": num_visits, + } + + return render(request, "taxi/index.html", context) + + +class ManufacturerListView(LoginRequiredMixin, generic.ListView): + model = Manufacturer + context_object_name = "manufacturer_list" + template_name = "taxi/manufacturer_list.html" + paginate_by = 5 + + +class CarListView(LoginRequiredMixin, generic.ListView): + model = Car + paginate_by = 5 + queryset = Car.objects.select_related("manufacturer").order_by("id") + + +class CarDetailView(LoginRequiredMixin, generic.DetailView): + model = Car + + +class DriverListView(LoginRequiredMixin, generic.ListView): + model = Driver + paginate_by = 5 + + +class DriverDetailView(LoginRequiredMixin, generic.DetailView): + model = Driver + queryset = Driver.objects.prefetch_related("cars__manufacturer")