haikal/inventory/mixins.py
2024-12-08 14:07:50 +03:00

35 lines
1.1 KiB
Python

from django import forms
from django.utils.translation import get_language
class AddClassMixin:
"""
Mixin for adding classes to a model.
"""
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:
"""
Mixin to provide a reusable get_localized_name method.
"""
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)