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

Taxi Service User Form and Validation #830

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django import forms
from .models import Driver, Car


class DriverCreationForm(forms.ModelForm):

Choose a reason for hiding this comment

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

The DriverCreationForm should extend UserCreationForm instead of forms.ModelForm. This is important to ensure consistency with Django's user creation standards and is a requirement in the task checklist .

class Meta:
model = Driver
fields = ["username", "first_name", "last_name", "license_number"]


class DriverLicenseUpdateForm(forms.ModelForm):

Choose a reason for hiding this comment

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

The DriverLicenseUpdateForm is missing validation for the license_number field. According to the task requirements, the license number should consist of 8 characters, with the first 3 being uppercase letters and the last 5 being digits .

class Meta:
model = Driver
fields = ["license_number"]


class CarCreationForm(forms.ModelForm):
class Meta:
model = Car
fields = ["model", "manufacturer", "drivers"]
1 change: 1 addition & 0 deletions taxi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def get_absolute_url(self):


class Car(models.Model):
objects = None

Choose a reason for hiding this comment

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

The assignment objects = None in the Car model is unnecessary and should be removed. Django automatically provides a default manager for models, and this line could lead to confusion or errors.

model = models.CharField(max_length=255)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
drivers = models.ManyToManyField(Driver, related_name="cars")
Expand Down
18 changes: 18 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
CarUpdateView,
CarDeleteView,
DriverListView,
DriverCreateView,
DriverLicenseUpdateView,
DriverDeleteView,
DriverDetailView,
ManufacturerListView,
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
assign_me_car,
delete_me_car,
)

urlpatterns = [
Expand Down Expand Up @@ -46,6 +51,19 @@
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>/driver-update/",
DriverLicenseUpdateView.as_view(),
name="driver-update"
Comment on lines +60 to +63

Choose a reason for hiding this comment

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

Consider renaming the path for DriverLicenseUpdateView to something more descriptive, such as driver-license-update, to clearly indicate that this view is specifically for updating the driver's license.

),
path("assign-me-car/<int:pk>/", assign_me_car, name="assign-me-car"),
path("delete-me-car/<int:pk>/", delete_me_car, name="delete-me-car"),
]

app_name = "taxi"
40 changes: 38 additions & 2 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.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.mixins import LoginRequiredMixin

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


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

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


Expand All @@ -87,3 +88,38 @@ 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
Comment on lines +93 to +95

Choose a reason for hiding this comment

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

Ensure that the DriverCreationForm used here extends UserCreationForm instead of forms.ModelForm. This is crucial for maintaining consistency with user creation standards in Django as per the task requirements.



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


class DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
form_class = DriverLicenseUpdateForm
Comment on lines +103 to +105

Choose a reason for hiding this comment

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

Make sure that the DriverLicenseUpdateForm includes validation for the license_number field. The license number should consist of 8 characters, with the first 3 being uppercase letters and the last 5 being digits, as specified in the task requirements.



@login_required
def assign_me_car(request, pk):
if request.method == "POST":
driver = get_object_or_404(Driver, pk=request.user.id)
car = get_object_or_404(Car, pk=pk)
car.drivers.add(driver)

return redirect("taxi:car-detail", pk=pk)


@login_required
def delete_me_car(request, pk):
if request.method == "POST":
driver = get_object_or_404(Driver, pk=request.user.id)
car = get_object_or_404(Car, pk=pk)
car.drivers.remove(driver)

return redirect("taxi:car-detail", pk=pk)
14 changes: 14 additions & 0 deletions taxi_service/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.core.exceptions import ValidationError
from django.core.validators import MinLengthValidator, MaxLengthValidator

from taxi.models import Driver, Car


class LicenseNumberValidatorMixin:
license_number = forms.CharField(
required=True,
validators=[MinLengthValidator(8), MaxLengthValidator(8)]
)
Comment on lines +10 to +14

Choose a reason for hiding this comment

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

The LicenseNumberValidatorMixin currently only checks the length of the license number. According to the task requirements, you need to validate that the license number consists of 8 characters, with the first 3 being uppercase letters and the last 5 being digits. Consider implementing a custom validator to enforce this rule.

1 change: 1 addition & 0 deletions taxi_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"django.contrib.staticfiles",
"debug_toolbar",
"crispy_forms",
"crispy_bootstrap4",
"taxi",
]

Expand Down
2 changes: 1 addition & 1 deletion templates/taxi/car_confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ <h1>Delete car?</h1>

<input type="submit" value="Yes" class="btn btn-danger">
</form>
{% endblock %}
{% endblock %}
16 changes: 14 additions & 2 deletions templates/taxi/car_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ <h1>
</a>
</h1>
<p>Manufacturer: ({{ car.manufacturer.name }}, {{ car.manufacturer.country }})</p>
<h1>Drivers</h1>
<h1>Drivers
{% if user in car.drivers.all %}
<form action="{% url 'taxi:delete-me-car' pk=car.id %}" method="post" class="link-to-page">
{% csrf_token %}
<input class="btn btn-danger" type="submit" value="Delete me from this car">
</form>
{% else %}
<form action="{% url 'taxi:assign-me-car' pk=car.id %}" method="post" class="link-to-page">
{% csrf_token %}
<input class="btn btn-success" type="submit" value="Assign me to this car btn">
</form>
{% endif %}
</h1>
<hr>
<ul>
{% for driver in car.drivers.all %}
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
<li>{{ driver.username }} ({{ driver.first_name }} {{ driver.last_name }})</li>
{% endfor %}
</ul>
{% endblock %}
4 changes: 2 additions & 2 deletions templates/taxi/car_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{% load crispy_forms_filters %}

{% block content %}
<h1>{{ object|yesno:"Update,Create" }} car</h1>
<h1>Create driver</h1>

Choose a reason for hiding this comment

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

The heading 'Create driver' appears to be incorrect for this template, which is intended for a car form. Consider changing it to 'Create Car' or a similar title that reflects the purpose of the form.

<form action="" method="post" novalidate>
{% csrf_token %}
{{ form|crispy }}

<input type="submit" value="Submit" class="btn btn-primary">
</form>
{% endblock %}
{% endblock %}
12 changes: 11 additions & 1 deletion templates/taxi/driver_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ <h1>

<p><strong>First name:</strong> {{ driver.first_name }}</p>
<p><strong>Last name:</strong> {{ driver.last_name }}</p>
<p><strong>License number:</strong> {{ driver.license_number }}</p>
<p>
<strong>License number:</strong> {{ driver.license_number }}
</p>
<p><strong>Is staff:</strong> {{ driver.is_staff }}</p>

<div class="ml-3">
Expand All @@ -23,4 +25,12 @@ <h4>Cars</h4>
<p>No cars!</p>
{% endfor %}
</div>
<div>
<a href="{% url 'taxi:driver-update' pk=driver.id%}" class="btn btn-primary">
Update license
</a>
<a href="{% url 'taxi:driver-delete' pk=driver.id%}" class="btn btn-danger">
Delete driver
</a>
</div>
{% endblock %}
6 changes: 4 additions & 2 deletions templates/taxi/driver_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

{% if driver_list %}
Expand All @@ -22,8 +25,7 @@ <h1>Driver List
<td>{{ driver.license_number }}</td>
</tr>
{% endfor %}

</table>
</table>
{% else %}
<p>There are no drivers in the service.</p>
{% endif %}
Expand Down
Loading