-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<int:pk>/", 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/<int:pk>/", 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") |