Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
M4Xpy committed Jan 23, 2024
1 parent 7dbe9b0 commit 22e70ed
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 28 deletions.
4 changes: 4 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
body {
margin-top: 20px;
}

.me {
color: red;
}
40 changes: 21 additions & 19 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
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()

context = {
"num_drivers": num_drivers,
"num_cars": num_cars,
"num_manufacturers": num_manufacturers,
}

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


class ManufacturerListView(generic.ListView):
num_visits = request.session.get("num_visits", 0)
request.session["num_visits"] = -~num_visits
return render(request,
"taxi/index.html",
{
"num_drivers": Driver.objects.count(),
"num_cars": Car.objects.count(),
"num_manufacturers": Manufacturer.objects.count(),
"num_visits": num_visits + 1,
}
)


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")
18 changes: 10 additions & 8 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# Build paths inside the project like this: BASE_DIR / "subdir".
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

Expand All @@ -29,6 +28,9 @@

ALLOWED_HOSTS = []

INTERNAL_IPS = [
"127.0.0.1",
]

# Application definition

Expand All @@ -39,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# "debug_toolbar",
"taxi",
]

Expand Down Expand Up @@ -72,7 +75,6 @@

WSGI_APPLICATION = "taxi_service.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

Expand All @@ -83,26 +85,25 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

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

Expand All @@ -119,7 +120,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

Expand All @@ -133,3 +133,5 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/"
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("accounts/", include("django.contrib.auth.urls"), ),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
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" %}">Logout</a></li>
{% else %}
<li><a href="{% url "login" %}">Login</a></li>
{% endif %}
<br>
<li><strong>Visits:</strong> {{ num_visits }}</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>
Expand Down
4 changes: 4 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "base.html" %}
{% block content %}
<h1>See you soon !</h1>
{% endblock %}
13 changes: 13 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}
{% block content %}
<h1>Login</h1>
{% if form.errors %}
<p style="color: red">Invalid credentials</p>
{% endif %}
<form action="{% url 'login' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login">
<input type="hidden" value="{{ next }}"/>
</form>
{% endblock %}
9 changes: 8 additions & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
<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 }})
{% if driver.username == user.username %}
<span class="me">(Me)</span>
{% endif %}
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no drivers in taxi</p>
{% endif %}
{% endblock %}




0 comments on commit 22e70ed

Please sign in to comment.