Skip to content

Commit

Permalink
fix: use django's inline js implementation
Browse files Browse the repository at this point in the history
add an empty form, only if not submitting and extras required
  • Loading branch information
asfaltboy committed Oct 9, 2022
1 parent fc01abc commit 90decee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 291 deletions.
30 changes: 18 additions & 12 deletions advanced_filters/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.db.models.fields import DateField
from django.forms.formsets import formset_factory, BaseFormSet
from functools import reduce
from django.utils.functional import cached_property
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -199,6 +200,14 @@ def get_form_kwargs(self, index):
kwargs['model_fields'] = self.model_fields
return kwargs

@cached_property
def forms(self):
"""Instantiate forms at first property access."""
forms = super().forms
if self.extra > 0 and not self.data:
forms += [self.empty_form]
return forms


AFQFormSet = formset_factory(
AdvancedFilterQueryForm, formset=AdvancedFilterFormSet,
Expand All @@ -221,6 +230,7 @@ class Media:
'advanced-filters/jquery_adder.js',
SELECT2_JS,
'magnific-popup/jquery.magnific-popup.js',
'admin/js/inlines.js',
'advanced-filters/advanced-filters.js',
]
js = required_js
Expand Down Expand Up @@ -289,23 +299,18 @@ def __init__(self, *args, **kwargs):

def clean(self):
cleaned_data = super().clean()
if not self.fields_formset.is_valid():
logger.debug(
"Errors validating advanced query filters: %s",
pformat([(f.errors, f.non_field_errors())
for f in self.fields_formset.forms]))
raise forms.ValidationError("Error validating filter forms")
cleaned_data['model'] = "{}.{}".format(self._model._meta.app_label,
self._model._meta.object_name)
return cleaned_data

@property
def _non_deleted_forms(self):
forms = []
for form in self.fields_formset.forms:
if form in self.fields_formset.deleted_forms:
continue # skip deleted forms when generating query
forms.append(form)
if self.fields_formset.is_valid():
for form in self.fields_formset.forms:
if form in self.fields_formset.deleted_forms:
continue # skip deleted forms when generating query
forms.append(form)
return forms

def generate_query(self):
Expand Down Expand Up @@ -345,6 +350,7 @@ def initialize_form(self, instance, model, data=None, extra=None):
)

def save(self, commit=True):
self.instance.query = self.generate_query()
self.instance.model = self.cleaned_data.get('model')
if self.is_valid():
self.instance.query = self.generate_query()
self.instance.model = self.cleaned_data.get('model')
return super().save(commit)
278 changes: 0 additions & 278 deletions advanced_filters/static/orig_inlines.js

This file was deleted.

1 change: 0 additions & 1 deletion advanced_filters/static/orig_inlines.min.js

This file was deleted.

0 comments on commit 90decee

Please sign in to comment.