2023-09-20 12:32:44 +05:30
|
|
|
"""
|
|
|
|
|
select_widgets.py
|
|
|
|
|
|
|
|
|
|
This module is used to write horilla form select widgets
|
|
|
|
|
"""
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-31 11:50:53 +05:30
|
|
|
import datetime
|
2023-09-20 12:32:44 +05:30
|
|
|
from django import forms
|
|
|
|
|
|
2024-01-31 11:50:53 +05:30
|
|
|
from base import thread_local_middleware
|
2024-03-10 19:37:46 +05:30
|
|
|
|
2024-01-31 11:50:53 +05:30
|
|
|
ALL_INSTANCES = {}
|
2023-09-20 12:32:44 +05:30
|
|
|
|
2024-03-10 19:37:46 +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,
|
2023-11-24 17:00:10 +05:30
|
|
|
required=False,
|
2023-09-20 12:32:44 +05:30
|
|
|
**kwargs
|
|
|
|
|
) -> None:
|
|
|
|
|
self.filter_route_name = filter_route_name
|
2023-11-24 17:00:10 +05:30
|
|
|
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
|
2023-11-24 17:00:10 +05:30
|
|
|
context["required"] = self.required
|
2024-01-31 11:50:53 +05:30
|
|
|
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
|
2024-01-31 11:50:53 +05:30
|
|
|
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
|