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

Open
wants to merge 1 commit 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
52 changes: 52 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.forms import ValidationError
from taxi.models import Driver, Car


class CarForm(forms.ModelForm):
drivers = forms.ModelMultipleChoiceField(
queryset=get_user_model().objects.all(),
Comment on lines +9 to +10

Choose a reason for hiding this comment

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

The drivers field in CarForm uses get_user_model().objects.all() as its queryset. Ensure that this is correct, as it might be more appropriate to use Driver.objects.all() if Driver is a separate model from the user model. This aligns with the checklist point to use get_user_model() only when appropriate.

widget=forms.CheckboxSelectMultiple,
required=False,
)

class Meta:
model = Car
fields = "__all__"


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

def clean_license_number(self):
return validate_license_number(self.cleaned_data["license_number"])


class DriverLicenseUpdateForm(forms.ModelForm):
class Meta:
model = Driver
fields = ("license_number",)

def clean_license_number(self):
return validate_license_number(self.cleaned_data["license_number"])


def validate_license_number(license_number: str) -> str:

if len(license_number) != 8:
raise ValidationError("License must consist only of 8 characters")
elif not license_number[:3].isalpha() or not license_number[:3].isupper():
raise ValidationError(
"First 3 characters of license are not uppercase or letters"
)
elif not license_number[3:].isdigit():
raise ValidationError("Last 5 characters are not digits")
return license_number
15 changes: 15 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,
DriverLicenseUpdateView, toggle_car
)

urlpatterns = [
Expand Down Expand Up @@ -43,9 +46,21 @@
path("cars/<int:pk>/update/", CarUpdateView.as_view(), name="car-update"),
path("cars/<int:pk>/delete/", CarDeleteView.as_view(), name="car-delete"),
path("drivers/", DriverListView.as_view(), name="driver-list"),
path("drivers/create/", DriverCreateView.as_view(), name="driver-create"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
path(
"drivers/<int:pk>/delete/",
DriverDeleteView.as_view(),
name="driver-delete",
),
path("cars/<int:pk>/toggle-car/", toggle_car, name="toggle-car"),
path(
"drivers/<int:pk>/update/",
DriverLicenseUpdateView.as_view(),
name="driver-update",
),
]

app_name = "taxi"
35 changes: 33 additions & 2 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin
from taxi.forms import CarForm, DriverCreationForm, DriverLicenseUpdateForm


from .forms import DriverCreationForm

Choose a reason for hiding this comment

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

The import of DriverCreationForm on this line is redundant since it is already imported on line 7. Consider removing this duplicate import to clean up the code.

from .models import Driver, Car, Manufacturer


Expand Down Expand Up @@ -64,13 +68,13 @@ class CarDetailView(LoginRequiredMixin, generic.DetailView):

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


class CarUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Car
fields = "__all__"
form_class = CarForm
success_url = reverse_lazy("taxi:car-list")


Expand All @@ -87,3 +91,30 @@ 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 DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = DriverLicenseUpdateForm
success_url = reverse_lazy("taxi:driver-list")


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


@login_required
def toggle_car(request, pk):
driver = Driver.objects.get(id=request.user.id)
if Car.objects.get(id=pk) in driver.cars.all():
driver.cars.remove(pk)
else:
driver.cars.add(pk)
return HttpResponseRedirect(reverse_lazy("taxi:car-detail", args=[pk]))
27 changes: 19 additions & 8 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@
{% 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 car in user.cars.all %}
<a href="{% url 'taxi:toggle-car' pk=car.id %}" class="btn btn-danger link-to-page">
Delete me from this car
</a>
{%else%}
<a href="{% url 'taxi:toggle-car' pk=car.id %}" class="btn btn-success link-to-page">
Assign me to this car
</a>
{% 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>{{ object|yesno:"Update,Create" }} driver</h1>
<form action="" method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}

<input type="submit" value="Submit" class="btn btn-primary">
</form>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% 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
Loading