added the color update

This commit is contained in:
Faheedkhan 2025-06-02 17:26:42 +03:00
parent f79a45b470
commit a430a7ffd9

View File

@ -945,12 +945,16 @@ class CarColorCreate(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
context["car"] = get_object_or_404(models.Car, slug=self.kwargs["slug"])
return context
class CarColorsUpdateView( LoginRequiredMixin, PermissionRequiredMixin, SuccessMessageMixin, UpdateView):
model = models.CarColors
form_class = forms.CarColorsForm
template_name = "inventory/add_colors.html"
success_message = _("Car finance details updated successfully")
permission_required = ["inventory.change_carfinance"]
success_message = _("Car Colors details updated successfully")
permission_required = ["inventory.change_car"]
def get_object(self, queryset=None):
"""
Retrieves the CarColors instance associated with the Car slug from the URL.
@ -969,29 +973,22 @@ class CarColorsUpdateView( LoginRequiredMixin, PermissionRequiredMixin, SuccessM
return get_object_or_404(models.CarColors, car__slug=slug)
def get_success_url(self):
"""
Redirects to the car's detail page using its slug after a successful update.
"""
# self.object refers to the CarColors instance that was just updated.
# self.object.car then refers to the associated Car instance.
return reverse("car_detail", kwargs={"slug": self.object.car.slug})
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["instance"] = self.get_object()
return kwargs
def get_initial(self):
initial = super().get_initial()
instance = self.get_object()
dealer = get_user_type(self.request)
selected_items = instance.additional_services.filter(dealer=dealer)
initial["additional_finances"] = selected_items
return initial
def get_form(self, form_class=None):
form = super().get_form(form_class)
dealer = get_user_type(self.request)
form.fields[
"additional_finances"
].queryset = models.AdditionalServices.objects.filter(dealer=dealer)
return form
def get_context_data(self, **kwargs):
"""
Adds the related Car object to the template context.
"""
context = super().get_context_data(**kwargs)
# self.object is already available here from get_object()
context['car'] = self.object.car
context['page_title'] = _("Update Colors for %(car_name)s") % {'car_name': context['car']}
return context
class CarListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
"""