Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dpiuro committed Sep 10, 2024
1 parent 46d9023 commit 9ec6aba
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 35 deletions.
Binary file added db.sqlite3
Binary file not shown.
5 changes: 5 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib.auth import views as auth_views
from django.urls import path

from . import views

from .views import (
index,
CarListView,
Expand All @@ -18,6 +21,8 @@
),
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"
Expand Down
23 changes: 14 additions & 9 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.decorators import login_required

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,
}

return render(request, "taxi/index.html", context=context)
return render(request, "taxi/index.html", 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")
queryset = Car.objects.select_related("manufacturer").order_by("id")


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")
32 changes: 23 additions & 9 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"django-insecure-8ovil3xu6=eaoqd#-#&ricv159p0pypoh5_lgm*)-dfcjqe=yc"
)

# SECURITY WARNING: don"t run with debug turned on in production!
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
Expand Down Expand Up @@ -89,20 +89,28 @@

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator",
"NAME": (
"django.contrib.auth.password_validation."
"UserAttributeSimilarityValidator"
),
},
{
"NAME": "django.contrib.auth.password_validation."
"MinimumLengthValidator",
"NAME": (
"django.contrib.auth.password_validation."
"MinimumLengthValidator"
),
},
{
"NAME": "django.contrib.auth.password_validation."
"CommonPasswordValidator",
"NAME": (
"django.contrib.auth.password_validation."
"CommonPasswordValidator"
),
},
{
"NAME": "django.contrib.auth.password_validation."
"NumericPasswordValidator",
"NAME": (
"django.contrib.auth.password_validation."
"NumericPasswordValidator"
),
},
]

Expand Down Expand Up @@ -133,3 +141,9 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/"

LOGOUT_REDIRECT_URL = "/"

SESSION_ENGINE = "django.contrib.sessions.backends.db"
7 changes: 5 additions & 2 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/login/", auth_views.LoginView.as_view(), name="login"),
path("accounts/logout/", auth_views.LogoutView.as_view(), name="logout"),
path("", include("taxi.urls", namespace="taxi")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
]
1 change: 0 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
Expand Down
21 changes: 15 additions & 6 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<ul class="sidebar-nav">
<li><a href="{% url "taxi:index" %}">Home page</a></li>
<li><a href="{% url "taxi:manufacturer-list" %}">Manufacturers</a></li>
<li><a href="{% url "taxi:car-list" %}">Cars</a></li>
<li><a href="{% url "taxi:driver-list" %}">Drivers</a></li>
</ul>
<div class="sidebar">
<ul>
<li><a href="{% url 'taxi:index' %}">Home page</a></li>
<li><a href="{% url 'taxi:manufacturer-list' %}">Manufacturers</a></li>
<li><a href="{% url 'taxi:car-list' %}">Cars</a></li>
<li><a href="{% url 'taxi:driver-list' %}">Drivers</a></li>
</ul>

{% if user.is_authenticated %}
<p>Logged in as <strong>{{ user.username }}</strong></p>
<a href="{% url 'taxi:logout' %}">Logout</a>
{% else %}
<a href="{% url 'taxi:login' %}">Login</a>
{% endif %}
</div>
10 changes: 10 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div>
<h2>Dynamic content</h2>
<p>The Taxi service has the following record counts:</p>
<ul>
<li>Cars: {{ num_cars }}</li>
<li>Drivers: {{ num_drivers }}</li>
<li>Manufacturers: {{ num_manufacturers }}</li>
</ul>
<p>You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}.</p>
</div>
Empty file.
15 changes: 15 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
18 changes: 10 additions & 8 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

{% block content %}
<h1>Driver list</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list%}
<ul>
{% for driver in object_list %}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
<a href="{% url 'taxi:driver-detail' pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
{% if driver == user %}
(Me)
{% endif %}
</li>
{% endfor %}
</ul>
{% else %}
{% endfor %}
</ul>
{% if not object_list %}
<p>There are no drivers in taxi</p>
{% endif %}
{% endblock %}

0 comments on commit 9ec6aba

Please sign in to comment.