-
Notifications
You must be signed in to change notification settings - Fork 804
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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): | ||
class Meta: | ||
model = Driver | ||
fields = ["username", "first_name", "last_name", "license_number"] | ||
|
||
|
||
class DriverLicenseUpdateForm(forms.ModelForm): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
class Meta: | ||
model = Driver | ||
fields = ["license_number"] | ||
|
||
|
||
class CarCreationForm(forms.ModelForm): | ||
class Meta: | ||
model = Car | ||
fields = ["model", "manufacturer", "drivers"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ def get_absolute_url(self): | |
|
||
|
||
class Car(models.Model): | ||
objects = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assignment |
||
model = models.CharField(max_length=255) | ||
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE) | ||
drivers = models.ManyToManyField(Driver, related_name="cars") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,16 @@ | |
CarUpdateView, | ||
CarDeleteView, | ||
DriverListView, | ||
DriverCreateView, | ||
DriverLicenseUpdateView, | ||
DriverDeleteView, | ||
DriverDetailView, | ||
ManufacturerListView, | ||
ManufacturerCreateView, | ||
ManufacturerUpdateView, | ||
ManufacturerDeleteView, | ||
assign_me_car, | ||
delete_me_car, | ||
) | ||
|
||
urlpatterns = [ | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming the path for |
||
), | ||
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" |
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 | ||
|
||
|
||
|
@@ -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") | ||
|
||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure that the |
||
|
||
|
||
@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) |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
"django.contrib.staticfiles", | ||
"debug_toolbar", | ||
"crispy_forms", | ||
"crispy_bootstrap4", | ||
"taxi", | ||
] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ | |
{% load crispy_forms_filters %} | ||
|
||
{% block content %} | ||
<h1>{{ object|yesno:"Update,Create" }} car</h1> | ||
<h1>Create driver</h1> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
DriverCreationForm
should extendUserCreationForm
instead offorms.ModelForm
. This is important to ensure consistency with Django's user creation standards and is a requirement in the task checklist .