Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHilly committed Dec 10, 2024
1 parent 74effaf commit 8d33709
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 12 deletions.
44 changes: 44 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# import re

from django.contrib.auth.forms import UserCreationForm
# from django.core.exceptions import ValidationError
from django.forms import ModelForm, CheckboxSelectMultiple

from taxi.models import Driver, Car


class DriverCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = Driver
fields = UserCreationForm.Meta.fields + (
"first_name", "last_name", "license_number"
)

# def clean_license_number(self):
# data = self.cleaned_data["license_number"]
#
# if not re.fullmatch(r"^[A-Z]{3}[0-9]{5}$", data):
# raise ValidationError("The license number is incorrect.")
#
# return data


class DriverLicenseUpdateForm(ModelForm):
class Meta:
model = Driver
fields = ("first_name", "last_name", "license_number")

# def clean_license_number(self):
# data = self.cleaned_data["license_number"]
#
# if not re.fullmatch(r"^[A-Z]{3}[0-9]{5}$", data):
# raise ValidationError("The license number is incorrect.")
#
# return data


class CarCreateForm(ModelForm):
class Meta:
model = Car
fields = "__all__"
widgets = {"drivers": CheckboxSelectMultiple}
11 changes: 11 additions & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re

from django.core.exceptions import ValidationError
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.urls import reverse
Expand Down Expand Up @@ -27,6 +30,14 @@ def __str__(self):
def get_absolute_url(self):
return reverse("taxi:driver-detail", kwargs={"pk": self.pk})

def clean_license_number(self):
if not re.fullmatch(r"^[A-Z]{3}[0-9]{5}$", self.license_number):
raise ValidationError("The license number is incorrect.")

def clean(self):
super().clean()
self.clean_license_number()


class Car(models.Model):
model = models.CharField(max_length=255)
Expand Down
14 changes: 14 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
DriverCreateView,
DriverDeleteView,
DriverUpdateView,
)

urlpatterns = [
Expand Down Expand Up @@ -46,6 +49,17 @@
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path("drivers/create/", DriverCreateView.as_view(), name="driver-create"),
path(
"drivers/<int:pk>/delete/",
DriverDeleteView.as_view(),
name="driver-delete"
),
path(
"drivers/<int:pk>/update/",
DriverUpdateView.as_view(),
name="driver-update"
)
]

app_name = "taxi"
38 changes: 35 additions & 3 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.urls import reverse_lazy
from django.shortcuts import render, redirect
from django.urls import reverse_lazy, reverse
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

from .forms import DriverCreationForm, DriverLicenseUpdateForm, CarCreateForm
from .models import Driver, Car, Manufacturer


Expand Down Expand Up @@ -61,10 +62,24 @@ class CarListView(LoginRequiredMixin, generic.ListView):
class CarDetailView(LoginRequiredMixin, generic.DetailView):
model = Car

def post(self, request, *args, **kwargs):
self.object = self.get_object()
user = request.user
print(user)
if "add_driver" in request.POST:
self.object.drivers.add(user)

if "remove_driver" in request.POST:
self.object.drivers.remove(user)

return redirect(reverse(
"taxi:car-detail", kwargs={"pk": self.object.pk}
))


class CarCreateView(LoginRequiredMixin, generic.CreateView):
model = Car
fields = "__all__"
form_class = CarCreateForm
success_url = reverse_lazy("taxi:car-list")


Expand All @@ -87,3 +102,20 @@ class DriverListView(LoginRequiredMixin, generic.ListView):
class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")


class DriverCreateView(LoginRequiredMixin, generic.CreateView):
model = Driver
form_class = DriverCreationForm
success_url = reverse_lazy("taxi:driver-list")


class DriverDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Driver
success_url = reverse_lazy("taxi:driver-list")


class DriverUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = DriverLicenseUpdateForm
success_url = reverse_lazy("taxi:driver-list")
31 changes: 23 additions & 8 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@
{% block content %}
<h1>
{{ car.model }}
<a href="{% url 'taxi:car-delete' pk=car.id %}" class="btn btn-danger link-to-page">
Delete
</a>

<a href="{% url 'taxi:car-update' pk=car.id %}" class="btn btn-secondary link-to-page">
Update
</a>
</h1>
<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<h1>
Drivers
{% if user.is_authenticated %}
<form style="display: inline" action="" method="post">
{% csrf_token %}
{% if user in car.drivers.all %}
<button class="btn btn-danger link-to-page" type="submit" name="remove_driver" value="{{ user.id }}">
Delete me from this car
</button>
{% else %}
<button class="btn btn-success link-to-page" type="submit" name="add_driver" value="{{ user.id }}">
Assign me to this car
</button>
{% endif %}
</form>
{% endif %}
</h1>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>
<a href="{% url 'taxi:car-update' pk=car.id %}" class="btn btn-secondary">
Update
</a>
<a href="{% url 'taxi:car-delete' pk=car.id %}" class="btn btn-danger">
Delete
</a>
{% endblock %}
10 changes: 10 additions & 0 deletions templates/taxi/driver_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "base.html" %}

{% block content %}
<h1>Delete driver?</h1>
<form action="" method="post">
{% csrf_token %}

<input type="submit" value="Yes" class="btn btn-danger">
</form>
{% endblock %}
6 changes: 6 additions & 0 deletions templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ <h4>Cars</h4>
<p>No cars!</p>
{% endfor %}
</div>
<a href="{% url 'taxi:driver-update' pk=driver.id %}" class="btn btn-secondary">
Update
</a>
<a href="{% url 'taxi:driver-delete' pk=driver.id %}" class="btn btn-danger">
Delete
</a>
{% endblock %}
12 changes: 12 additions & 0 deletions templates/taxi/driver_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% load crispy_forms_filters %}

{% block content %}
<h1>Create driver:</h1>
<form action="" method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}

<input type="submit" value="Submit" class="btn btn-primary">
</form>
{% endblock %}
6 changes: 5 additions & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{% extends "base.html" %}

{% block content %}
<h1>Driver List
<h1>
Driver List
<a href="{% url 'taxi:driver-create' %}" class="btn btn-primary link-to-page">
Create
</a>
</h1>

{% if driver_list %}
Expand Down

0 comments on commit 8d33709

Please sign in to comment.