Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
timovlad committed Jan 3, 2025
1 parent 7c20474 commit c115ea4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
23 changes: 11 additions & 12 deletions taxi/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@ class Meta:
model = Car
fields = "__all__"
widgets = {
'drivers': forms.CheckboxSelectMultiple,
"drivers": forms.CheckboxSelectMultiple,
}


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

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

if len(license_number) != 8:
raise forms.ValidationError("Consist only of 8 characters")

# Проверка первых 3 символов
if not license_number[:3].isalpha() or not license_number[:3].isupper():
raise forms.ValidationError("First 3 characters are uppercase letters")
if (not license_number[:3].isalpha()
or not license_number[:3].isupper()):
raise forms.ValidationError(
"First 3 characters are uppercase letters")

# Проверка последних 5 символов
if not license_number[3:].isdigit():
raise forms.ValidationError("Last 5 characters are digits")

return license_number



class DriverUpdateView(ModelForm):
class DriverLicenseUpdateForm(ModelForm):
class Meta:
model = Driver
fields = ("license_number",)
Expand All @@ -47,11 +46,11 @@ def clean_license_number(self):
if len(license_number) != 8:
raise forms.ValidationError("Consist only of 8 characters")

# Проверка первых 3 символов
if not license_number[:3].isalpha() or not license_number[:3].isupper():
raise forms.ValidationError("First 3 characters are uppercase letters")
if (not license_number[:3].isalpha()
or not license_number[:3].isupper()):
raise forms.ValidationError(
"First 3 characters are uppercase letters")

# Проверка последних 5 символов
if not license_number[3:].isdigit():
raise forms.ValidationError("Last 5 characters are digits")

Expand Down
16 changes: 13 additions & 3 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ManufacturerCreateView,
ManufacturerUpdateView,
ManufacturerDeleteView,
assign_driver, remove_driver
)

urlpatterns = [
Expand Down Expand Up @@ -46,10 +47,19 @@
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(
"cars/<int:car_id>/assign_driver/",
assign_driver, name="assign-driver"),
path(
"cars/<int:car_id>/remove_driver/",
remove_driver, name="remove-driver"),
path("drivers/", DriverListView.as_view(), name="driver-list"),
path("drivers/create/", DriverCreateView.as_view(), name="driver-create"), # add new
path("drivers/<int:pk>/update/", DriverUpdateView.as_view(), name="driver-update"), #update
path("drivers/<int:pk>/delete/", DriverDeleteView.as_view(), name="driver-delete"), # delete
path("drivers/create/",
DriverCreateView.as_view(), name="driver-create"),
path("drivers/<int:pk>/update/",
DriverUpdateView.as_view(), name="driver-update"),
path("drivers/<int:pk>/delete/",
DriverDeleteView.as_view(), name="driver-delete"),
path(
"drivers/<int:pk>/", DriverDetailView.as_view(), name="driver-detail"
),
Expand Down
25 changes: 18 additions & 7 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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

from taxi.forms import DriverCreationForm, DriverUpdateView, CarForm
from taxi.forms import DriverCreationForm, DriverLicenseUpdateForm, CarForm
from .models import Driver, Car, Manufacturer
from django.shortcuts import render, get_object_or_404, redirect


@login_required
Expand Down Expand Up @@ -69,8 +68,6 @@ class CarCreateView(LoginRequiredMixin, generic.CreateView):
form_class = CarForm




class CarUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Car
fields = "__all__"
Expand All @@ -91,6 +88,7 @@ class DriverDetailView(LoginRequiredMixin, generic.DetailView):
model = Driver
queryset = Driver.objects.all().prefetch_related("cars__manufacturer")


class DriverCreateView(LoginRequiredMixin, generic.CreateView):
model = Driver
success_url = reverse_lazy("taxi:driver-list")
Expand All @@ -100,10 +98,23 @@ class DriverCreateView(LoginRequiredMixin, generic.CreateView):
class DriverUpdateView(LoginRequiredMixin, generic.UpdateView):
model = Driver
success_url = reverse_lazy("taxi:driver-list")
form_class = DriverUpdateView

form_class = DriverLicenseUpdateForm


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


@login_required
def assign_driver(request, car_id):
car = get_object_or_404(Car, pk=car_id)
car.drivers.add(request.user)
return redirect("taxi:car-detail", pk=car_id)


@login_required
def remove_driver(request, car_id):
car = get_object_or_404(Car, pk=car_id)
car.drivers.remove(request.user)
return redirect("taxi:car-detail", pk=car_id)

0 comments on commit c115ea4

Please sign in to comment.