diff --git a/horilla_views/admin.py b/horilla_views/admin.py index 03cb2a23a..f5afe88c7 100644 --- a/horilla_views/admin.py +++ b/horilla_views/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from horilla_views.models import ActiveGroup, ActiveTab, ToggleColumn +from horilla_views.models import ActiveGroup, ActiveTab, SavedFilter, ToggleColumn admin.site.register([ToggleColumn, ActiveTab, ActiveGroup]) diff --git a/horilla_views/cbv_methods.py b/horilla_views/cbv_methods.py index 2fb4714d1..ad40be24b 100644 --- a/horilla_views/cbv_methods.py +++ b/horilla_views/cbv_methods.py @@ -30,7 +30,6 @@ from django.utils.html import format_html from django.utils.safestring import SafeString from django.utils.translation import gettext_lazy as _trans -from base.methods import eval_validate from horilla import settings from horilla.horilla_middlewares import _thread_locals from horilla_views.templatetags.generic_template_filters import getattribute @@ -473,6 +472,8 @@ def value_to_field(field: object, value: list) -> Any: """ return value according to the format of the field """ + from base.methods import eval_validate + if isinstance(field, models.ManyToManyField): return [int(val) for val in value] elif isinstance( diff --git a/horilla_views/generic/cbv/views.py b/horilla_views/generic/cbv/views.py index 5de78923c..e68aad9f8 100644 --- a/horilla_views/generic/cbv/views.py +++ b/horilla_views/generic/cbv/views.py @@ -294,13 +294,14 @@ class HorillaListView(ListView): keys_to_remove = [ key for key, value in data_dict.items() - if value[0] in ["unknown", "on"] + self.filter_keys_to_remove + if key in ["filter_applied", "nav_url"] + self.filter_keys_to_remove ] for key in keys_to_remove + ["referrer"]: if key in data_dict.keys(): data_dict.pop(key) context["filter_dict"] = data_dict + context["keys_to_remove"] = keys_to_remove request = self.request ordered_ids = list(queryset.values_list("id", flat=True)) @@ -813,6 +814,8 @@ class HorillaFormView(FormView): # NOTE: Dynamic create view's forms save method will be overwritten is_dynamic_create_view: bool = False + # [(field_name,DynamicFormView,[other_field1,...])] # other_fields + # can be mentioned like this to pass the field selected dynamic_create_fields: list = [] def __init__(self, **kwargs: Any) -> None: @@ -903,7 +906,10 @@ class HorillaFormView(FormView): files = self.request.FILES form = self.init_form(data=data, files=files, instance=instance) if self.is_dynamic_create_view: - setattr(type(form), "save", save) + # setattr(type(form), "save", save) + from types import MethodType + + form.save = MethodType(save, form) if self.request.method == "GET": [ @@ -916,6 +922,9 @@ class HorillaFormView(FormView): view = dynamic_tuple[1] view.display_title = "Dynamic create" field = dynamic_tuple[0] + additional_data_fields = [] + if len(dynamic_tuple) == 3: + additional_data_fields = dynamic_tuple[2] key = self.request.session.session_key + "cbv" + field field_instance = form.instance._meta.get_field(field) value = form.initial.get(field, []) @@ -931,7 +940,6 @@ class HorillaFormView(FormView): ) else: value = getattr(getattribute(form.instance, field), "pk", value) - CACHE.set( key, { @@ -957,6 +965,22 @@ class HorillaFormView(FormView): choices.insert(0, ("", "Select option")) choices.append(("dynamic_create", "Dynamic create")) attrs = form.fields[field].widget.attrs + for data_field in additional_data_fields: + + data_field_attr = form.fields[data_field].widget.attrs + if ( + f"$('#modalButton{field}Form [name={data_field}]').val(this.value);" + not in data_field_attr.get("onchange", "") + ): + data_field_attr["onchange"] = ( + data_field_attr.get("onchange", "") + + f""" + if(this.value != 'dynamic_create'){{ + $('#modalButton{field}Form [name={data_field}]').val(this.value); + }} + """ + ) + form.fields[field] = form_field( choices=choices, label=form.fields[field].label, @@ -967,6 +991,18 @@ class HorillaFormView(FormView): ) form.fields[field].widget.attrs = attrs form.initial[field] = value + for dynamic_tuple in self.dynamic_create_fields: + field = dynamic_tuple[0] + onchange = form.fields[field].widget.attrs.get("onchange", "") + if onchange: + CACHE.set( + self.request.session.session_key + + "cbv" + + field + + "onchange", + onchange, + ) + if pk: form.instance = instance title = str(instance) @@ -1004,6 +1040,7 @@ class HorillaNavView(TemplateView): filter_instance: FilterSet = None filter_instance_context_name: str = "" filter_body_template: str = "" + empty_inputs: list = [] view_types: list = [] create_attrs: str = """""" @@ -1027,8 +1064,12 @@ class HorillaNavView(TemplateView): context["search_in"] = self.search_in context["filter_instance_context_name"] = self.filter_instance last_filter = CACHE.get( - self.request.session.session_key + "last-applied-filter", {} + self.request.session.session_key + + "last-applied-filter" + + self.request.path, + {}, ) + context["empty_inputs"] = self.empty_inputs + ["nav_url"] context["last_filter"] = dict(last_filter) if self.filter_instance: context[self.filter_form_context_name] = self.filter_instance.form diff --git a/horilla_views/templates/generic/filter_tags.html b/horilla_views/templates/generic/filter_tags.html index 2dc041474..7bcbef620 100644 --- a/horilla_views/templates/generic/filter_tags.html +++ b/horilla_views/templates/generic/filter_tags.html @@ -14,6 +14,9 @@
{{filter_dict}}
diff --git a/horilla_views/templates/generic/horilla_form.html b/horilla_views/templates/generic/horilla_form.html index 3399c6e25..1c921f97f 100644 --- a/horilla_views/templates/generic/horilla_form.html +++ b/horilla_views/templates/generic/horilla_form.html @@ -1,3 +1,4 @@ +{% load generic_template_filters %}
{% for field_tuple in dynamic_create_fields %}
+ {% endfor %}
{{form.structured}}
{% for field_tuple in dynamic_create_fields %} +
+ + - +
{% endfor %} diff --git a/horilla_views/templates/generic/horilla_list_table.html b/horilla_views/templates/generic/horilla_list_table.html index 9db721cb3..6009fa946 100644 --- a/horilla_views/templates/generic/horilla_list_table.html +++ b/horilla_views/templates/generic/horilla_list_table.html @@ -335,6 +335,9 @@ if (widths.length) { thWidth = widths[0]; } + if (!thWidth) { + thWidth = 180 + } $('#{{view_id}} .lastTh').css("width", `${thWidth}px`); } }); diff --git a/horilla_views/templates/generic/horilla_nav.html b/horilla_views/templates/generic/horilla_nav.html index 54e525116..8501ea862 100644 --- a/horilla_views/templates/generic/horilla_nav.html +++ b/horilla_views/templates/generic/horilla_nav.html @@ -24,6 +24,7 @@ hx-on:submit="htmxLoadIndicator(this);" class="oh-main__titlebar oh-main__titlebar--right" > +
{ + $(".oh-tabs__tab--active").click(); + }, 100); } {% endif %} diff --git a/horilla_views/templates/generic/reload_select_field.html b/horilla_views/templates/generic/reload_select_field.html index 8487c1a1a..b86e46839 100644 --- a/horilla_views/templates/generic/reload_select_field.html +++ b/horilla_views/templates/generic/reload_select_field.html @@ -1,23 +1,31 @@
{{field}} - - +