Files
ihrm/horilla_widgets/widgets/select_widgets.py

62 lines
1.8 KiB
Python
Raw Normal View History

2023-09-20 12:32:44 +05:30
"""
select_widgets.py
This module is used to write horilla form select widgets
"""
import datetime
2023-09-20 12:32:44 +05:30
from django import forms
from base import thread_local_middleware
ALL_INSTANCES = {}
2023-09-20 12:32:44 +05:30
2023-09-20 12:32:44 +05:30
class HorillaMultiSelectWidget(forms.Widget):
"""
HorillaMultiSelectWidget
"""
def __init__(
self,
*args,
filter_route_name,
filter_class=None,
filter_instance_contex_name=None,
filter_template_path=None,
instance=None,
required=False,
2023-09-20 12:32:44 +05:30
**kwargs
) -> None:
self.filter_route_name = filter_route_name
self.required = required
2023-09-20 12:32:44 +05:30
self.filter_class = filter_class
self.filter_instance_contex_name = filter_instance_contex_name
self.filter_template_path = filter_template_path
self.instance = instance
super().__init__()
template_name = "horilla_widgets/horilla_multiselect_widget.html"
def get_context(self, name, value, attrs):
# Get the default context from the parent class
context = super().get_context(name, value, attrs)
# Add your custom data to the context
queryset = self.choices.queryset
field = self.choices.field
context["queryset"] = queryset
context["field_name"] = name
context["field"] = field
context["self"] = self
context["filter_template_path"] = self.filter_template_path
context["filter_route_name"] = self.filter_route_name
context["required"] = self.required
self.attrs["id"] = (
("id_" + name) if self.attrs.get("id") is None else self.attrs.get("id")
)
2023-09-20 12:32:44 +05:30
context[self.filter_instance_contex_name] = self.filter_class
request = getattr(thread_local_middleware._thread_locals, "request", None)
ALL_INSTANCES[str(request.user.id)] = self
2023-09-20 12:32:44 +05:30
return context