Skip to content

Commit

Permalink
taxi service authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Мар'яна Тиха committed Dec 19, 2024
1 parent e4d52bc commit a28fa9c
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 62 deletions.
25 changes: 3 additions & 22 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
from django.urls import path
from . import views

from .views import (
index,
CarListView,
CarDetailView,
DriverListView,
DriverDetailView,
ManufacturerListView,
)
app_name = "taxi"

urlpatterns = [
path("", index, name="index"),
path(
"manufacturers/",
ManufacturerListView.as_view(),
name="manufacturer-list",
),
path("cars/", CarListView.as_view(), name="car-list"),
path("cars/<int:pk>/", CarDetailView.as_view(), name="car-detail"),
path("drivers/", DriverListView.as_view(), name="driver-list"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path("", views.index, name="index"),
]

app_name = "taxi"
40 changes: 5 additions & 35 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
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):
model = Manufacturer
context_object_name = "manufacturer_list"
template_name = "taxi/manufacturer_list.html"
paginate_by = 5


class CarListView(generic.ListView):
model = Car
paginate_by = 5
queryset = Car.objects.select_related("manufacturer")


class CarDetailView(generic.DetailView):
model = Car


class DriverListView(generic.ListView):
model = Driver
paginate_by = 5


class DriverDetailView(generic.DetailView):
model = Driver
queryset = Driver.objects.prefetch_related("cars__manufacturer")
num_visits = request.session.get("num_visits", 1)
request.session["num_visits"] = num_visits + 1
6 changes: 6 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"taxi",
]

LOGIN_URL = "/login/"

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/"

LOGOUT_REDIRECT_URL = "/"
4 changes: 1 addition & 3 deletions taxi_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""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:
Expand All @@ -17,9 +16,8 @@
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static


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)
6 changes: 6 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{% if user.is_authenticated %}
<p>Username: {{ user.username }}</p>
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<a href="{% url 'login' %}">Login</a>
{% endif %}
<ul class="sidebar-nav">
<li><a href="{% url "taxi:index" %}">Home page</a></li>
<li><a href="{% url "taxi:manufacturer-list" %}">Manufacturers</a></li>
Expand Down
12 changes: 12 additions & 0 deletions templates/registration/logged_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'base.html' %}

{% block sidebar %}
<a href="{% url 'taxi:index' %}">Home page</a>
{% endblock %}

{% block content %}
<div class="text-center">
<h1 class="mb-4">You're Logged Out</h1>
<a href="{% url 'login' %}" class="btn btn-primary btn-lg">Log in</a>
</div>
{% 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 sidebar %}
<a href="{% url 'taxi:index' %}">Home page</a>
{% endblock %}

{% block content %}
<form method="post" class="form-group">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Login" class="btn btn-primary">
</form>
{% endblock %}
4 changes: 3 additions & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "base.html" %}

{% block content %}
<h1>Driver list</h1>
{% if driver_list %}
Expand All @@ -8,6 +7,9 @@ <h1>Driver list</h1>
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
{% if driver.id == user.id %}
(Me)
{% endif %}
</li>
{% endfor %}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion templates/taxi/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% extends "base.html" %}

{% block content %}
<h1>Taxi Service Home</h1>
<p>Welcome to Best Taxi Ever!</p>
Expand All @@ -10,4 +9,5 @@ <h2>Dynamic content</h2>
<li><strong>Drivers:</strong> {{ num_drivers }}</li>
<li><strong>Manufacturers:</strong> {{ num_manufacturers }}</li>
</ul>
<p>You've visited this page {{ num_visits }} time{{ num_visits|pluralize }}</p>
{% endblock %}

0 comments on commit a28fa9c

Please sign in to comment.