diff --git a/taxi/forms.py b/taxi/forms.py index 0c1373a0..282617ee 100644 --- a/taxi/forms.py +++ b/taxi/forms.py @@ -10,6 +10,7 @@ class CarForm(forms.ModelForm): drivers = forms.ModelMultipleChoiceField( queryset=get_user_model().objects.all(), widget=forms.CheckboxSelectMultiple, + required=False ) class Meta: diff --git a/taxi/urls.py b/taxi/urls.py index bae906a4..cd389eda 100644 --- a/taxi/urls.py +++ b/taxi/urls.py @@ -16,6 +16,7 @@ DriverCreateView, DriverDeleteView, DriverLicenseUpdateView, + toggle_assign_to_car, ) urlpatterns = [ @@ -64,6 +65,12 @@ "cars//delete/", CarDeleteView.as_view(), name="car-delete" ), + path( + "cars//toggle-assign/", + toggle_assign_to_car, + name="toggle-car-assign", + ), + path( "drivers/", DriverListView.as_view(), diff --git a/taxi/views.py b/taxi/views.py index 15c74c7d..f1d9ec23 100644 --- a/taxi/views.py +++ b/taxi/views.py @@ -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 @@ -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]))