Skip to content

Commit

Permalink
Added solution
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGrynyshyn committed Nov 16, 2023
1 parent 46d9023 commit e172ce6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
venv/
.pytest_cache/
**__pycache__/
db.sqlite3
2 changes: 1 addition & 1 deletion taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
path("drivers/", DriverListView.as_view(), name="driver-list"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
)
]

app_name = "taxi"
14 changes: 9 additions & 5 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from .models import Driver, Car, Manufacturer

Expand All @@ -10,38 +11,41 @@ def index(request):
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)


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")
2 changes: 2 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
]

AUTH_USER_MODEL = "taxi.Driver"
LOGIN_REDIRECT_URL = "taxi:index"
LOGIN_URL = "login"

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
Expand Down
1 change: 1 addition & 0 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("taxi.urls", namespace="taxi")),
path("", include("django.contrib.auth.urls"))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
9 changes: 9 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li><h2>Hi, {{ user.username }}</h2></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>
<li><a href="{% url "taxi:driver-list" %}">Drivers</a></li>
{% if user.is_authenticated %}
<li><a href="{% url "logout" %}">Log out</a></li>
{% else %}
<li><a href="{% url "login" %}">Login</a></li>

{% endif %}
</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 %}
<div class="container">
<h1>You have successfully logged out</h1>
<a href="{% url "taxi:index" %}">Back to the home page</a>
</div>
{% endblock content %}
26 changes: 26 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "base.html" %}
{% block content %}
<div style="width:500px; padding: 200px 0; margin: auto auto" class="container">
<div class="text-center">
{% if form.errors %}
<div class="alert alert-danger" role="alert">
<strong>Error!</strong> Please correct the following errors:
<ul>
{% for field in form %}
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
<form class="form-signin" method="post">
{% csrf_token %}
<h1 class="h3 mb-3 font-weight-normal">Sign in</h1>
{{ form.as_p }}
<button style="margin: 20px 0" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
</div>
{% endblock content %}

5 changes: 3 additions & 2 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
<h1>Driver list</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list%}
{% for driver in driver_list %}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
({{ driver.first_name }} {{ driver.last_name }} {% if user.get_full_name == driver.get_full_name %}
(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>
<h4>You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}!</h4>
{% endblock %}

0 comments on commit e172ce6

Please sign in to comment.