Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMozhar committed Dec 14, 2023
1 parent 46d9023 commit fd862d2
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 51 deletions.
5 changes: 5 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
body {
margin-top: 20px;
}

.sidebar-nav {
padding: 0;
list-style: none;
}
49 changes: 27 additions & 22 deletions taxi/migrations/0002_alter_manufacturer_options_and_more.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# Generated by Django 4.0.2 on 2022-04-12 06:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('taxi', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='manufacturer',
options={'ordering': ['name']},
),
migrations.AlterField(
model_name='driver',
name='license_number',
field=models.CharField(max_length=255, unique=True),
),
]
# Generated by Django 4.1 on 2023-12-14 07:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('taxi', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='manufacturer',
options={'ordering': ['name']},
),
migrations.AlterField(
model_name='driver',
name='license_number',
field=models.CharField(max_length=255, unique=True),
),
migrations.AlterField(
model_name='manufacturer',
name='name',
field=models.CharField(max_length=255, unique=True),
),
]
18 changes: 0 additions & 18 deletions taxi/migrations/0003_alter_manufacturer_name.py

This file was deleted.

2 changes: 1 addition & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import path
from django.urls import path, include

from .views import (
index,
Expand Down
16 changes: 11 additions & 5 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.views import generic

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) + 1
request.session["num_visits"] = num_visits

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)


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")


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")
6 changes: 4 additions & 2 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / "subdir".
Expand Down Expand Up @@ -57,7 +57,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"DIRS": [os.path.join(BASE_DIR, "templates")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down Expand Up @@ -108,6 +108,8 @@

AUTH_USER_MODEL = "taxi.Driver"

LOGIN_REDIRECT_URL = "/taxi/"

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

Expand Down
3 changes: 2 additions & 1 deletion taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("taxi/", include("taxi.urls", namespace="taxi")),
path("accounts/", include("django.contrib.auth.urls")),
] + 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
8 changes: 8 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: <a href="{% url "taxi:driver-detail" pk=user.id %}">
{{ user.username }}
</a></li>
<li><a href="{% url "logout" %}">Click to log out</a></li>
{% else %}
<li><a href="{% url "login" %}">Click to log in</a></li>
{% endif %}
<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>
Expand Down
6 changes: 6 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "base.html" %}

{% block content %}
<p>Logged out!</p>
<a href="{% url 'login'%}">Click here to login again.</a>
{% endblock %}
15 changes: 15 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}
<h1>Login</h1>

{% if form.errors %}
<p style="color: red">Invalid credentials</p>
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
5 changes: 4 additions & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ <h1>Driver list</h1>
<ul>
{% for driver in driver_list%}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">
{{ driver.username }}
</a>
({{ driver.first_name }} {{ driver.last_name }})
{% if driver == request.user %} (Me){% endif %}
</li>
{% endfor %}
</ul>
Expand Down
1 change: 1 addition & 0 deletions templates/taxi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ <h2>Dynamic content</h2>
<li><strong>Drivers:</strong> {{ num_drivers }}</li>
<li><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
</ul>
<p>You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}</p>
{% endblock %}

0 comments on commit fd862d2

Please sign in to comment.