Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandra committed Jan 30, 2024
1 parent 46d9023 commit 9a072f3
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 167 deletions.
Binary file added db.sqlite3
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
django-bootstrap-icons==0.8.7
21 changes: 20 additions & 1 deletion static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
body {
margin-top: 20px;
margin: 0;
padding: 0;
}

hr {
margin: 6px 0 18px;
}

.title {
padding: 16px 0;
margin: 0;
}

.subtitle {
padding-bottom: 16px;
margin: 0;
}

.page {
padding-left: 30px;
}
9 changes: 9 additions & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Callable

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.urls import reverse


class Manufacturer(models.Model):
Expand All @@ -20,6 +23,9 @@ class Meta:
verbose_name = "driver"
verbose_name_plural = "drivers"

def get_absolute_url(self) -> Callable:
return reverse("taxi:driver-detail", args=[str(self.id)])

def __str__(self):
return f"{self.username} ({self.first_name} {self.last_name})"

Expand All @@ -29,5 +35,8 @@ class Car(models.Model):
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
drivers = models.ManyToManyField(Driver, related_name="cars")

def get_absolute_url(self) -> Callable:
return reverse("taxi:car-detail", args=[str(self.id)])

def __str__(self):
return self.model
25 changes: 17 additions & 8 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
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)
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,
"url_home": True
}

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


class ManufacturerListView(generic.ListView):
class ManufacturerListView(LoginRequiredMixin, generic.ListView):
model = Manufacturer
extra_context = {"url_manufacturer": True}
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
extra_context = {"url_car": True}
paginate_by = 5
queryset = Car.objects.select_related("manufacturer")


class CarDetailView(generic.DetailView):
class CarDetailView(LoginRequiredMixin, generic.DetailView):
model = Car
extra_context = {"url_car": True}


class DriverListView(generic.ListView):
class DriverListView(LoginRequiredMixin, generic.ListView):
model = Driver
extra_context = {"url_driver": True}
paginate_by = 5


class DriverDetailView(generic.DetailView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
extra_context = {"url_driver": True}
queryset = Driver.objects.prefetch_related("cars__manufacturer")
9 changes: 0 additions & 9 deletions taxi_service/asgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
"""
ASGI config for taxi_service project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application
Expand Down
41 changes: 2 additions & 39 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
"""
Django settings for taxi_service project.
Generated by "django-admin startproject" using Django 4.0.2.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# 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/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = (
"django-insecure-8ovil3xu6=eaoqd#-#&ricv159p0pypoh5_lgm*)-dfcjqe=yc"
)

# SECURITY WARNING: don"t run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
Expand All @@ -40,6 +18,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"taxi",
"django_bootstrap_icons"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -72,21 +51,13 @@

WSGI_APPLICATION = "taxi_service.wsgi.application"


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

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


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

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation."
Expand All @@ -108,8 +79,7 @@

AUTH_USER_MODEL = "taxi.Driver"

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

LANGUAGE_CODE = "en-us"

Expand All @@ -119,17 +89,10 @@

USE_TZ = True


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

STATIC_URL = "static/"

STATICFILES_DIRS = (BASE_DIR / "static",)

STATIC_ROOT = BASE_DIR / "staticfiles"

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
16 changes: 1 addition & 15 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
"""taxi_service URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path("", views.home, name="home")
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path("", Home.as_view(), name="home")
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
Expand All @@ -22,4 +7,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)
9 changes: 0 additions & 9 deletions taxi_service/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
"""
WSGI config for taxi_service project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application
Expand Down
42 changes: 23 additions & 19 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@
crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<link rel="stylesheet" href={% static "css/styles.css" %}>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">

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

</div>
<div class="col-sm-10 ">

{% block content %}{% endblock %}

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

{% block header %}
{% include "includes/header.html" %}
{% endblock %}
<hr>
<div class="container-fluid px-4">
<div class="row">
<div class="col-sm-2">

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

</div>
<div class="col-sm-10 page">

{% block content %}{% endblock %}

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

</div>
</div>
</div>
</div>
</body>

</html>
18 changes: 18 additions & 0 deletions templates/includes/header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% load bootstrap_icons %}

<header class="d-flex justify-content-between align-items-center px-4">
<h1 class="title">Taxi Service</h1>

<div class="d-flex justify-content-between align-items-center">
{% if user.is_authenticated %}
<span>Welcome, <a href="{% url "taxi:driver-detail" user.id %}">{{ user.first_name }}</a></span>
<a class="btn btn-primary d-flex align-items-center ml-3" href="{% url "logout" %}" role="button">
{% bs_icon "box-arrow-right" size='1.1em' extra_classes="mr-2" %}Logout
</a>
{% else %}
<a class="btn btn-primary d-flex align-items-center ml-3" href="{% url "login" %}" role="button">
{% bs_icon "box-arrow-in-right" size='1.1em' extra_classes="mr-2" %}Login
</a>
{% endif %}
</div>
</header>
24 changes: 11 additions & 13 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{% 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>
<ul class="pagination justify-content-end mt-3">
<li class="page-item {% if not page_obj.has_previous %}disabled{% endif %}">
<a class="page-link" href="{% if page_obj.has_previous %}?page={{ page_obj.previous_page_number }}{% endif %}">
<span>&laquo;</span>
</a>
</li>
<li class="page-item bg-primary"><span class="page-link">{{ page_obj.number }} / {{ paginator.num_pages }}</span></li>
<li class="page-item {% if not page_obj.has_next %}disabled{% endif %}">
<a class="page-link" href="{% if page_obj.has_next %}?page={{ page_obj.next_page_number }}{% endif %}">
<span>&raquo;</span>
</a>
</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 %}
Loading

0 comments on commit 9a072f3

Please sign in to comment.