Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #388

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ class Manufacturer(models.Model):
name = models.CharField(max_length=255, unique=True)
country = models.CharField(max_length=255)

class Meta:
ordering = ["name"]

def __str__(self) -> str:
return self.name


class Driver(AbstractUser):
license_number = models.CharField(max_length=255, unique=True)

class Meta:
ordering = ["username"]
verbose_name = "driver"
verbose_name_plural = "drivers"

Expand All @@ -19,3 +26,10 @@ class Car(models.Model):
model = models.CharField(max_length=255)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
drivers = models.ManyToManyField(Driver, related_name="cars")

class Meta:
ordering = ["model"]

def __str__(self) -> str:
return (f"Model: {self.model}, "
f"manufacturer: {self.manufacturer.name}")
34 changes: 32 additions & 2 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
from django.urls import path

from .views import index
from .views import (
index,
ManufacturerListView,
CarListView,
CarDetailView,
DriverListView,
DriverDetailView,
)

urlpatterns = [
path("", index, name="index"),
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(
"drivers/",
DriverListView.as_view(),
name="driver-list"),
path(
"drivers/<int:pk>/",
DriverDetailView.as_view(),
name="driver-detail"),
]

app_name = "taxi"
30 changes: 29 additions & 1 deletion taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.shortcuts import render
from django.views import generic

from .models import Driver, Car, Manufacturer


def index(request):
"""View function for the home page of the site."""

num_drivers = Driver.objects.count()
num_cars = Car.objects.count()
Expand All @@ -17,3 +17,31 @@ def index(request):
}

return render(request, "taxi/index.html", context=context)


class ManufacturerListView(generic.ListView):
model = Manufacturer
context_object_name = "manufacturer_list"
template_name = "taxi/manufacturer_list.html"
queryset = Manufacturer.objects.order_by("name")
paginate_by = 5


class CarListView(generic.ListView):
model = Car
queryset = Car.objects.select_related("manufacturer")
paginate_by = 5


class CarDetailView(generic.DetailView):
model = Car


class DriverListView(generic.ListView):
model = Driver
paginate_by = 5


class DriverDetailView(generic.DetailView):
model = Driver
queryset = Driver.objects.prefetch_related("cars")
6 changes: 6 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

ALLOWED_HOSTS = []

INTERNAL_IPS = [
"127.0.0.1",
]


# Application definition

Expand All @@ -37,11 +41,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",
Expand Down
2 changes: 2 additions & 0 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from django.conf import settings
from django.conf.urls.static import static

from taxi.views import index

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("__debug__/", include("debug_toolbar.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
3 changes: 1 addition & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
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>

<body>
<div class="container-fluid">
<div class="row">
Expand All @@ -29,6 +27,7 @@
{% block content %}{% endblock %}

{% block pagination %}
{% include "includes/pagination.html" %}
{% endblock %}

</div>
Expand Down
17 changes: 17 additions & 0 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item">
<a href="?page= {{ page_obj.previous_page_number }}" class="page-link">prev</a>
</li>
{% endif %}
<li class="page-item active">
<span class="page-link">{{ page_obj.number }} of {{ paginator.num_pages }}</span>
</li>
{% if page_obj.has_next %}
<li class="page-item">
<a href="?page= {{ page_obj.next_page_number }}" class="page-link">next</a>
</li>
{% endif %}
</ul>
{% endif %}
8 changes: 4 additions & 4 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ul class="sidebar-nav">
<li><a href="#">Home page</a></li>
<li><a href="#">Manufacturers</a></li>
<li><a href="#">Cars</a></li>
<li><a href="#">Drivers</a></li>
<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>
15 changes: 15 additions & 0 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}
<h1>Model: {{ car.model }}</h1>
<p><strong>Manufacturer:</strong> {{ car.manufacturer.name }}</p>
<div class="ml-3">
<h3>Drivers</h3>
{% for driver in car.drivers.all %}
<hr>
<p>{{ driver }}</p>
{% empty %}
<p>There is no human to operate this car</p>
{% endfor %}
</div>
{% endblock %}
21 changes: 21 additions & 0 deletions templates/taxi/car_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}

{% block title %}
<title>Car List</title>
{% endblock %}

{% block content %}
<h1>Car List</h1>
{% if car_list %}
<ul>
{% for car in car_list %}
<li>
<a href="{% url 'taxi:car-detail' pk=car.id %}">{{ car.model }}</a>
({{ car.manufacturer.name }}, {{ car.manufacturer.country }})
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no cars available</p>
{% endif %}
{% endblock %}
16 changes: 16 additions & 0 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "base.html" %}

{% block content %}
<h1>Driver: {{ driver.username }}
({{ driver.first_name }} {{ driver.last_name }})</h1>
<p><strong>License number:</strong> {{ driver.license_number }}</p>
<div class="ml-3">
<h3>Cars</h3>
{% for car in driver.cars.all %}
<hr>
{{ car.model }}
{% empty %}
<p>This driver has no cars!</p>
{% endfor %}
</div>
{% endblock %}
21 changes: 21 additions & 0 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}

{% block title %}
<title>Driver List</title>
{% endblock %}

{% block content %}
<h1>Driver List</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list %}
<li>
<a href="{% url 'taxi:driver-detail' pk=driver.id %}">{{ driver.username }}</a>
({{ driver.license_number }})
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no drivers available!</p>
{% endif %}
{% endblock %}
18 changes: 18 additions & 0 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturers</h1>
{% if manufacturer_list %}
<ul>
{% for manufacturer in manufacturer_list %}
<li>
<p>
<b>{{ manufacturer.name }}</b> <i>{{ manufacturer.country }}</i>
</p>
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no manufacturer list</p>
{% endif %}
{% endblock %}
Loading