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

Solution #784

Closed
wants to merge 4 commits into from
Closed
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
Binary file added db.sqlite3
Binary file not shown.
80 changes: 53 additions & 27 deletions taxi/urls.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
from django.urls import path

from .views import (
index,
CarListView,
CarDetailView,
DriverListView,
DriverDetailView,
ManufacturerListView,
)

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"
),
]

app_name = "taxi"
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.decorators import method_decorator
from django.shortcuts import render
from django.views import generic

from .models import Driver, Car, Manufacturer


@login_required
def index(request):
num_visits = request.session.get("num_visits", 0)
request.session["num_visits"] = num_visits + 1

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,
"num_visits": num_visits,
}

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


class ManufacturerListView(LoginRequiredMixin, generic.ListView):
model = Manufacturer
context_object_name = "manufacturer_list"
template_name = "taxi/manufacturer_list.html"
paginate_by = 5


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


class CarDetailView(LoginRequiredMixin, generic.DetailView):
model = Car


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


class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.prefetch_related("cars__manufacturer")
39 changes: 31 additions & 8 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,70 @@
<<<<<<< HEAD
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils.decorators import method_decorator
=======
from django.contrib.auth.mixins import LoginRequiredMixin
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
from django.shortcuts import render
from django.views import generic
from django.contrib.auth.decorators import login_required

from .models import Driver, Car, Manufacturer


@login_required
def index(request):
"""View function for the home page of the site."""
<<<<<<< HEAD
if "num_visits" not in request.session:
request.session["num_visits"] = 0

request.session["num_visits"] += 1

=======
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
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,
<<<<<<< HEAD
"num_visits": request.session["num_visits"],
=======
"num_visits": num_visits,
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
}

return render(request, "taxi/index.html", context=context)
return render(request, "taxi/index.html", 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")
queryset = Car.objects.select_related("manufacturer").order_by("id")


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")
39 changes: 30 additions & 9 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"django-insecure-8ovil3xu6=eaoqd#-#&ricv159p0pypoh5_lgm*)-dfcjqe=yc"
)

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

ALLOWED_HOSTS = []
Expand Down Expand Up @@ -52,6 +52,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]


ROOT_URLCONF = "taxi_service.urls"

TEMPLATES = [
Expand Down Expand Up @@ -89,20 +90,28 @@

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

Expand Down Expand Up @@ -133,3 +142,15 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/"

<<<<<<< HEAD
LOGOUT_REDIRECT_URL = "/login/"

LOGIN_URL = "/login/"
=======
LOGOUT_REDIRECT_URL = "/"

SESSION_ENGINE = "django.contrib.sessions.backends.db"
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
15 changes: 14 additions & 1 deletion taxi_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<<<<<<< HEAD
=======
"""taxi_service URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
Expand All @@ -13,13 +15,24 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path("blog/", include("blog.urls"))
"""

>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static

from django.contrib.auth import views as auth_views

urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/login/", auth_views.LoginView.as_view(), name="login"),
path("accounts/logout/", auth_views.LogoutView.as_view(), name="logout"),
path("", include("taxi.urls", namespace="taxi")),
<<<<<<< HEAD
path("login/", auth_views.LoginView.as_view(), name="login"),
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
=======
]
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
1 change: 0 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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 -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
Expand Down
25 changes: 25 additions & 0 deletions templates/includes/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
<<<<<<< HEAD
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: <a href="{% url 'taxi:driver-detail' user.pk %}">{{ user.username }}</a></li>
<li><a href="{% url 'logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'login' %}">Login</a></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>
</ul>
=======
<div class="sidebar">
<ul>
<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>

{% if user.is_authenticated %}
<p>Logged in as <strong>{{ user.username }}</strong></p>
<a href="{% url 'taxi:logout' %}">Logout</a>
{% else %}
<a href="{% url 'taxi:login' %}">Login</a>
{% endif %}
</div>
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
10 changes: 10 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div>
<h2>Dynamic content</h2>
<p>The Taxi service has the following record counts:</p>
<ul>
<li>Cars: {{ num_cars }}</li>
<li>Drivers: {{ num_drivers }}</li>
<li>Manufacturers: {{ num_manufacturers }}</li>
</ul>
<p>You have visited this page {{ num_visits }} time{{ num_visits|pluralize }}.</p>
</div>
Empty file.
28 changes: 28 additions & 0 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<<<<<<< HEAD
{% extends "base.html" %}

{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
=======
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
</body>
</html>
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
34 changes: 26 additions & 8 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
{% extends "base.html" %}

{% block content %}
<<<<<<< HEAD
<h1>Drivers</h1>
<ul>
{% for driver in object_list %}
<li>
<a href="{% url 'taxi:driver-detail' driver.pk %}">
{{ driver.username }}
</a>
{% if driver == user %}
(Me)
{% endif %}
</li>
{% endfor %}
</ul>
=======
<h1>Driver list</h1>
{% if driver_list %}
<ul>
{% for driver in driver_list%}
<ul>
{% for driver in object_list %}
<li>
<a href="{% url "taxi:driver-detail" pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
<a href="{% url 'taxi:driver-detail' pk=driver.id %}">{{ driver.username }}</a>
({{ driver.first_name }} {{ driver.last_name }})
{% if driver == user %}
(Me)
{% endif %}
</li>
{% endfor %}
</ul>
{% else %}
{% endfor %}
</ul>
{% if not object_list %}
<p>There are no drivers in taxi</p>
{% endif %}
>>>>>>> 9ec6aba500bb01d17ec55bca21b477b65d9d5c38
{% endblock %}
Loading
Loading