Skip to content

Commit

Permalink
'Solution_taxi-service-user-form-and-volidation'
Browse files Browse the repository at this point in the history
  • Loading branch information
vvg04021968 committed Dec 16, 2024
1 parent 028a41f commit adffb8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions taxi/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CarForm(forms.ModelForm):
drivers = forms.ModelMultipleChoiceField(
queryset=get_user_model().objects.all(),
widget=forms.CheckboxSelectMultiple,
required=False
)

class Meta:
Expand Down
7 changes: 7 additions & 0 deletions taxi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DriverCreateView,
DriverDeleteView,
DriverLicenseUpdateView,
toggle_assign_to_car,
)

urlpatterns = [
Expand Down Expand Up @@ -64,6 +65,12 @@
"cars/<int:pk>/delete/",
CarDeleteView.as_view(), name="car-delete"
),
path(
"cars/<int:pk>/toggle-assign/",
toggle_assign_to_car,
name="toggle-car-assign",
),

path(
"drivers/",
DriverListView.as_view(),
Expand Down
13 changes: 13 additions & 0 deletions taxi/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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
Expand Down Expand Up @@ -105,3 +106,15 @@ class DriverLicenseUpdateView(LoginRequiredMixin, generic.UpdateView):
class DriverDeleteView(LoginRequiredMixin, generic.DeleteView):
model = Driver
success_url = reverse_lazy("taxi:driver-list")


@login_required
def toggle_assign_to_car(request, pk):
driver = Driver.objects.get(id=request.user.id)
if (
Car.objects.get(id=pk) in driver.cars.all()
): # probably could check if car exists
driver.cars.remove(pk)
else:
driver.cars.add(pk)
return HttpResponseRedirect(reverse_lazy("taxi:car-detail", args=[pk]))

0 comments on commit adffb8d

Please sign in to comment.