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

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
21 changes: 14 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
django==4.1
flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
django-debug-toolbar==3.2.4
django-crispy-forms==1.14.0
asgiref==3.8.1
crispy-bootstrap4==2022.1
Django==4.1
django-crispy-forms==1.14.0
django-debug-toolbar==3.2.4
flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
mccabe==0.7.0
pep8-naming==0.13.2
pycodestyle==2.9.1
pyflakes==2.5.0
sqlparse==0.5.3
tzdata==2024.2
30 changes: 30 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
DriverListView,
DriverDetailView,
ManufacturerListView,
CarCreateView,
CarUpdateView,
CarDeleteView,
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
)

urlpatterns = [
Expand All @@ -22,6 +28,30 @@
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path(
"cars/create/", CarCreateView.as_view(), name="car-create"
),
path(
"cars/<int:pk>/update/", CarUpdateView.as_view(), name="car-update"
),
path(
"cars/<int:pk>/delete/", CarDeleteView.as_view(), name="car-delete"
),
path(
"manufacturers/create/",
ManufacturerCreateView.as_view(),
name="manufacturer-create"
),
path(
"manufacturers/<int:pk>/update/",
ManufacturerUpdateView.as_view(),
name="manufacturer-update"
),
path(
"manufacturers/<int:pk>/delete/",
ManufacturerDeleteView.as_view(),
name="manufacturer-delete"
),
]

app_name = "taxi"
40 changes: 40 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

Expand Down Expand Up @@ -52,3 +53,42 @@ class DriverListView(LoginRequiredMixin, generic.ListView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")


class CarCreateView(LoginRequiredMixin, generic.CreateView):
model = Car
fields = "__all__"
success_url = reverse_lazy("taxi:car-list")
template_name = "taxi/car_form.html"


class CarUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Car
fields = "__all__"
success_url = reverse_lazy("taxi:car-list")
template_name = "taxi/car_form.html"


class CarDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Car
success_url = reverse_lazy("taxi:car-list")


class ManufacturerCreateView(LoginRequiredMixin, generic.CreateView):
model = Manufacturer
fields = "__all__"
success_url = reverse_lazy("taxi:manufacturer-list")
template_name = "taxi/manufacturer_form.html"


class ManufacturerUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Manufacturer
fields = "__all__"
success_url = reverse_lazy("taxi:manufacturer-list")
template_name = "taxi/manufacturer_form.html"


class ManufacturerDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Manufacturer
success_url = reverse_lazy("taxi:manufacturer-list")
template_name = "taxi/manufacturer_confirm_delete.html"
5 changes: 5 additions & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"django.contrib.staticfiles",
"debug_toolbar",
"taxi",
"crispy_bootstrap4",
"crispy_forms",
# "debug_toolbar",
Comment on lines +47 to +49

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug_toolbar is listed twice in the INSTALLED_APPS list, once commented and once uncommented. Please remove the commented entry to avoid redundancy.

]

MIDDLEWARE = [
Expand Down Expand Up @@ -140,3 +143,5 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

CRISPY_TEMPLATE_PACK = "bootstrap4"
18 changes: 12 additions & 6 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<html lang="en">

<head>
{% block title %}<title>Taxi Service</title>{% endblock %}
{% block title %}
<title>Taxi Service</title>
{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
Expand All @@ -18,20 +20,24 @@
<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 %}
<div class="col-sm-10 ">
{% block content %}
<h1>Car Creation</h1>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock %}
Comment on lines +29 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content specific to 'Car Creation' should not be hardcoded in the base template. Move this content to a specific template for car creation to avoid duplication and maintain template reusability.


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

</div>
</div>
</div>
Expand Down
13 changes: 11 additions & 2 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{% extends "base.html" %}

{% block content %}
<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<h2>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</h2>
<p>Drivers: </p>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>
<form action="{% url 'taxi:car-update' pk=car.id %}" method="post" style="display: inline;">
{% csrf_token %}
<button type="submit">Update Car</button>
</form>
<form action="{% url 'taxi:car-delete' pk=car.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="_method" value="delete">
Comment on lines +16 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delete form uses a hidden input to specify the HTTP method as 'delete', which is not supported by HTML forms. Change the form method to 'post' and handle the delete action in the view logic.

<button type="submit">Delete Car</button>
</form>
{% endblock %}
27 changes: 14 additions & 13 deletions templates/taxi/car_list.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{% extends "base.html" %}
{% load crispy_forms_filters %}

{% block content %}
<h1>Car list</h1>
{% if car_list %}
<ul>
{% for car in car_list %}
<li>
<a href="{% url "taxi:car-detail" pk=car.id %} ">{{ car.id }}</a>
{{ car.model }} ({{ car.manufacturer.name }})
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no cars in taxi</p>
{% endif %}
<h1>
{% if object %}
Update Car
{% else %}
Create Car
{% endif %}
Comment on lines +6 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining whether to display 'Update Car' or 'Create Car' is based on the presence of object. Ensure this logic accurately reflects the intended context, possibly by checking if the form is bound or by using a more explicit condition.

</h1>

<form action="" method="post" novalidate>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
{% endblock %}
14 changes: 14 additions & 0 deletions templates/taxi/manufacturer_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "base.html" %}

{% block content %}
<h1>Delete Manufacturer</h1>

<p>Are you sure you want to delete this manufacturer?</p>
<p>Name: {{ manufacturer.name }}</p>
<a href="{% url 'taxi:manufacturer-list' %}" class="btn btn-primary">Cancel</a>
<br><br>
<form action="" method="post">
{% csrf_token %}
<input type="submit" value="Yes, delete" class="btn btn-danger" />
</form>
{% endblock %}
36 changes: 8 additions & 28 deletions templates/taxi/manufacturer_list.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
{% extends "base.html" %}

{% block content %}
<h1>Manufacturer List
</h1>

{% if manufacturer_list %}
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
</tr>
{% load crispy_forms_filters %}

{% for manufacturer in manufacturer_list %}
<tr>
<td>
{{ manufacturer.id }}
</td>
<td>
{{ manufacturer.name }}
</td>
<td>
{{ manufacturer.country }}
</td>
</tr>
{% endfor %}
</table>
{% block content %}
<h1>{% if object %}Update{% else %}Create{% endif %} Manufacturer</h1>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for determining whether to display 'Update Manufacturer' or 'Create Manufacturer' is based on the presence of object. Ensure this logic accurately reflects the intended context, possibly by checking if the form is bound or by using a more explicit condition.


{% else %}
<p>There are no manufacturers in the service.</p>
{% endif %}
<form action="" method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Submit">
</form>
{% endblock %}
Loading