from django.utils.translation import get_language class AddClassMixin: """ Provides functionality for automatically adding CSS classes to form field widgets. This mixin is intended to be used in Django forms. It modifies the rendering of form fields by appending specific CSS classes to their widget attributes, enhancing their default styling. It distinguishes between fields with ``forms.Select`` widgets and other types of widgets to apply different CSS classes. """ def add_class_to_fields(self): """ Adds the class to the fields of the model. :return: class names form-control or form-select """ for field_name, field in self.fields.items(): # if isinstance(field.widget, forms.Select): # existing_classes = field.widget.attrs.get('class', '') # field.widget.attrs['class'] = f"{existing_classes} form-select form-select-sm".strip() # else: existing_classes = field.widget.attrs.get('class', '') field.widget.attrs['class'] = f"{existing_classes} form-control form-control-sm".strip() class LocalizedNameMixin: """ Provides functionality to retrieve localized names based on the current language. This mixin is intended to provide language-specific name access for objects that support localization. It checks the current language setting and retrieves the appropriate name attribute (`arabic_name` for Arabic or `name` for other languages). :ivar arabic_name: Localized name for the Arabic language. :type arabic_name: Optional[str] :ivar name: Default name used for non-Arabic languages. :type name: Optional[str] """ def get_local_name(self): """ Returns the localized name based on the current language. """ if get_language() == 'ar': return getattr(self, 'arabic_name', None) return getattr(self, 'name', None) # class AddDealerInstanceMixin: # def form_valid(self, form): # if form.is_valid(): # form.instance.dealer = self.request.user.dealer.get_root_dealer # form.save() # return super().form_valid(form) # else: # return form.errors