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

Implement py-taxi-service-authentication #474

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.idea/
.vscode/

*.iml
.env
.DS_Store

venv/
.pytest_cache/
**__pycache__/
3 changes: 2 additions & 1 deletion static/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
body {
margin-top: 20px;
height: 100vh;
overflow: hidden;
}
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)
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")


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 @@ -108,6 +108,8 @@

AUTH_USER_MODEL = "taxi.Driver"

LOGIN_REDIRECT_URL = "/"

# 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("accounts/", include("django.contrib.auth.urls"))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
13 changes: 6 additions & 7 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@
{% block title %}<title>Taxi Service</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet"
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 -->
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"/>
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="container-fluid p-0 h-100">
<div class="row h-100">
<div class="col-sm-2">

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

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

{% block content %}{% endblock %}

Expand Down
8 changes: 6 additions & 2 deletions templates/includes/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
<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>
<a href="?page= {{ page_obj.previous_page_number }}" class="page-link d-flex align-items-center"><span class="material-symbols-outlined">
arrow_back_ios
</span></a>
</li>
{% endif %}
<li class="page-item active">
<span class="page-link">{{ page_obj.number }} of {{ paginator.num_pages }}</span>
</li>
{% if page_obj.has_next %}
<li class="page-item">
<a href="?page= {{ page_obj.next_page_number }}" class="page-link">next</a>
<a href="?page= {{ page_obj.next_page_number }}" class="page-link d-flex align-items-center"><span class="material-symbols-outlined">
arrow_forward_ios
</span></a>
</li>
{% endif %}
</ul>
Expand Down
55 changes: 49 additions & 6 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
<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>
<div class="d-flex flex-column flex-shrink-0 p-3 pt-1 bg-light h-100" style="width: 100%;">
{% if user.is_authenticated %}
<div class="d-flex align-items-center">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
account_circle
</span>
<a href="{% url 'taxi:driver-detail' pk=user.id %}"
class="fs-4 mb-3 mb-md-0 me-md-auto link-dark text-decoration-none">{{ user.username }}</a>
</div>
<div class="d-flex align-items-center">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
input
</span>
<a class="fs-5 mb-3 mb-md-0 me-md-auto link-dark text-decoration-none" href="{% url 'logout' %}">
Logout
</a>
</div>
{% else %}
<div class="d-flex align-items-center">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
input
</span>
<a class="fs-5 mb-3 mb-md-0 me-md-auto link-dark text-decoration-none" href="{% url 'login' %}">
Login
</a>
</div>
{% endif %}
<hr>
{% with request.resolver_match.url_name as url_name %}
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="{% url "taxi:index" %}"
class="nav-link {% if url_name == 'index' %}active{% else %}link-dark{% endif %}">Home page</a>
</li>
<li>
<a href="{% url "taxi:manufacturer-list" %}"
class="nav-link {% if url_name == 'manufacturer-list' %}active{% else %}link-dark{% endif %}">Manufacturers</a>
</li>
<li>
<a href="{% url "taxi:car-list" %}"
class="nav-link {% if url_name == 'car-list' %}active{% else %}link-dark{% endif %}">Cars</a>
</li>
<li>
<a href="{% url "taxi:driver-list" %}"
class="nav-link {% if url_name == 'driver-list' %}active{% else %}link-dark{% endif %}">Drivers</a>
</li>
</ul>
{% endwith %}
</div>
10 changes: 10 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}

{% block content %}
<div class="d-flex align-items-center mb-2 mt-3">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
logout
</span>
<p class="mb-0">Logged out!</p>
</div>
{% endblock %}
16 changes: 16 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% 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="Login">
<input type="hidden" name="next" value="{{ next }}">
</form>
{% endblock %}
42 changes: 34 additions & 8 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
{% extends "base.html" %}

{% block content %}
<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.first_name }} {{ driver.last_name }}</li>
{% endfor %}
</ul>
<div class="d-flex align-items-center mb-2 mt-3">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
local_taxi
</span>
<p class="m-0"><strong>
{{ car.model }}</strong>
</p>
</div>
<i>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</i>
<hr class="w-50">
<h5><b>Drivers:</b></h5>

<table class="table table-striped w-50">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First name</th>
<th scope="col">Last name</th>
<th scope="col">License number</th>
</tr>
</thead>
<tbody>
{% for driver in car.drivers.all %}
<tr>
<th scope="row">{{ driver.id }}</th>
<td>{{ driver.first_name }}</td>
<td>{{ driver.last_name }}</td>
<td>{{ driver.license_number }}</td>
</tr>
{% empty %}
<p>No cars!</p>
{% endfor %}
</tbody>
</table>
{% endblock %}
7 changes: 3 additions & 4 deletions templates/taxi/car_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
{% block content %}
<h1>Car list</h1>
{% if car_list %}
<ul>
<ul class="list-group col-4 col-md-6 mb-3">
{% for car in car_list %}
<li>
<a href="{% url "taxi:car-detail" pk=car.id %} ">{{ car.id }}</a>
{{ car.model }} ({{ car.manufacturer.name }})
<li class="list-group-item list-group-item-action">
<a href="{% url "taxi:car-detail" pk=car.id %} " class="link-dark text-decoration-none">{{ car.id }}. {{ car.model }} ({{ car.manufacturer.name }})</a>
</li>
{% endfor %}
</ul>
Expand Down
39 changes: 28 additions & 11 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
{% 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 }}
<div class="d-flex align-items-center mb-2 mt-3">
<span class="material-symbols-outlined" style="margin-right: 0.5rem;">
airline_seat_recline_extra
</span>
<p class="m-0"><strong>
{{ driver.first_name }} {{ driver.last_name }}</strong>
</p>
</div>
<i>License number: {{ driver.license_number }}</i>

{% empty %}
<p>No cars!</p>
{% endfor %}
<table class="table table-striped w-50">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Car model</th>
<th scope="col">Manufacturer</th>
</tr>
</thead>
<tbody>
{% for car in driver.cars.all %}
<tr>
<th scope="row">{{ car.id }}</th>
<td>{{ car.model }}</td>
<td>{{ car.manufacturer.name }}</td>
</tr>
{% empty %}
<p>No cars!</p>
{% endfor %}
</tbody>
</table>

{% endblock %}
10 changes: 6 additions & 4 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
{% block content %}
<h1>Driver list</h1>
{% if driver_list %}
<ul>
<ul class="list-group col-4 col-md-6 mb-3">
{% 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 class="list-group-item list-group-item-action">
<a href="{% url "taxi:driver-detail" pk=driver.id %}" class="link-dark text-decoration-none">{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})
{% if driver.id == user.id %}
(Me)
{% endif %}</a>
</li>
{% endfor %}
</ul>
Expand Down
9 changes: 5 additions & 4 deletions templates/taxi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ <h1>Taxi Service Home</h1>
<p>Welcome to Best Taxi Ever!</p>
<h2>Dynamic content</h2>
<p>The Taxi service has the following record counts:</p>
<ul>
<li><strong>Cars:</strong> {{ num_cars }}</li>
<li><strong>Drivers:</strong> {{ num_drivers }}</li>
<li><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
<ul class="list-group col-4 col-md-6 mb-2">
<li class="list-group-item list-group-item-action"><strong>Cars:</strong> {{ num_cars }}</li>
<li class="list-group-item list-group-item-action"><strong>Drivers:</strong> {{ num_drivers }}</li>
<li class="list-group-item list-group-item-action"><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
</ul>
<i class="position-absolute" style="right: 30px; bottom: 0;">You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}</i>
{% endblock %}
4 changes: 2 additions & 2 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<h1>Manufacturer list</h1>

{% if manufacturer_list %}
<ul>
<ul class="list-group col-4 col-md-6 mb-3">
{% for manufacturer in manufacturer_list %}
<li>{{ manufacturer.name }} {{ manufacturer.country }}</li>
<li class="list-group-item list-group-item-action">{{ manufacturer.name }} {{ manufacturer.country }}</li>
{% endfor %}
</ul>
{% endif %}
Expand Down
Loading