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

'Solution' #725

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
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
django-debug-toolbar==3.8.1


21 changes: 14 additions & 7 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

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

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")
queryset = Car.objects.all().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")
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")
8 changes: 8 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"taxi",
"debug_toolbar",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand Down Expand Up @@ -133,3 +135,9 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/"

INTERNAL_IPS = [
"127.0.0.1",
]
2 changes: 2 additions & 0 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("accounts/", include("django.contrib.auth.urls")),
path("__debug__/", include("debug_toolbar.urls")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
19 changes: 14 additions & 5 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 class="sidebar-nav list-group">
{% if user.is_authenticated %}
<li class="list-group-item">User: <a href="{{ user.get_absolute_url }}">{{ user.get_username }}</a></li>
<li class="list-group-item"><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
{% else %}
<li class="list-group-item"><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}

<br>

<li class="list-group-item"><a href="{% url 'taxi:index' %}">Home</a></li>
<li class="list-group-item"><a href="{% url 'taxi:driver-list' %}">All drivers</a></li>
<li class="list-group-item"><a href="{% url 'taxi:car-list' %}">All cars</a></li>
<li class="list-group-item"><a href="{% url 'taxi:manufacturer-list' %}">All manufacturers</a></li>
</ul>
7 changes: 7 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}

{% block content %}
<p>Logged out!</p>

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

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

<h1>Login</h1>

{% if next %}
<p>Please login to see this page.</p>
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}

<input type="submit" value="Login" class="btn btn-primary" />
<input type="hidden" name="next" value="{{ next }}" />
</form>

{% endblock %}
32 changes: 20 additions & 12 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
{% extends "base.html" %}

{% block content %}
<p><strong>
{{ driver.first_name }} {{ driver.last_name }}.</strong>
License number: {{ driver.license_number }}
</p>
{% for car in driver.cars.all %}
<hr>
<p><strong>Id:</strong>{{ car.id }}
<strong>Car model:</strong> {{ car.model }}
</p>
<h1>
Username: {{ driver.username }}
</h1>

{% empty %}
<p>No cars!</p>
{% endfor %}
<p><strong>First name:</strong> {{ driver.first_name }}</p>
<p><strong>Last name:</strong> {{ driver.last_name }}</p>
<p><strong>License number:</strong> {{ driver.license_number }}</p>
<p><strong>Is staff:</strong> {{ driver.is_staff }}</p>

<div class="ml-3">
<h4>Cars</h4>

{% for car in driver.cars.all %}
<hr>
<p><strong>Model:</strong> {{ car.model }}</p>
<p><strong>Manufacturer:</strong> {{ car.manufacturer.name }}</p>
<p class="text-muted"><strong>Id:</strong> {{car.id}}</p>

{% empty %}
<p>No cars!</p>
{% endfor %}
</div>
{% endblock %}
38 changes: 25 additions & 13 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
{% extends "base.html" %}

{% 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.first_name }} {{ driver.last_name }})
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no drivers in taxi</p>
{% endif %}
<h1>Driver List</h1>

{% if driver_list %}
<table class="table">
<tr>
<th>ID</th>
<th>Username</th>
<th>First name</th>
<th>Last name</th>
<th>License number</th>
</tr>
{% for driver in driver_list %}
<tr>
<td>{{ driver.id }}</td>
<td><a href="{{ driver.get_absolute_url }}">{{ driver.username }} {% if user == driver %} (Me){% endif %}</a></td>
<td>{{ driver.first_name }}</td>
<td>{{ driver.last_name }}</td>
<td>{{ driver.license_number }}</td>
</tr>
{% endfor %}

</table>
{% else %}
<p>There are no drivers in the service.</p>
{% endif %}
{% endblock %}
5 changes: 5 additions & 0 deletions templates/taxi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ <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 %}
37 changes: 28 additions & 9 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturer list</h1>

{% if manufacturer_list %}
<ul>
{% for manufacturer in manufacturer_list %}
<li>{{ manufacturer.name }} {{ manufacturer.country }}</li>
{% endfor %}
</ul>
{% endif %}
<h1>Manufacturer List</h1>

{% if manufacturer_list %}
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
</tr>

{% for manufacturer in manufacturer_list %}
<tr>
<td>
{{ manufacturer.id }}
</td>
<td>
{{ manufacturer.name }}
</td>
<td>
{{ manufacturer.country }}
</td>
</tr>
{% endfor %}
</table>

{% else %}
<p>There are no manufacturers in the service.</p>
{% endif %}
{% endblock %}
Loading