Skip to content

Commit

Permalink
some fix from modlinltd#97
Browse files Browse the repository at this point in the history
  • Loading branch information
aatrubilin committed Apr 20, 2021
1 parent db80f1f commit 3f222cb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
16 changes: 11 additions & 5 deletions advanced_filters/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AdvancedFilterQueryForm(CleanWhiteSpacesMixin, forms.Form):
label=_('Operator'),
required=True, choices=OPERATORS, initial="iexact",
widget=forms.Select(attrs={'class': 'query-operator'}))
value = VaryingTypeCharField(required=True, widget=forms.TextInput(
value = VaryingTypeCharField(required=False, widget=forms.TextInput(
attrs={'class': 'query-value'}), label=_('Value'))
value_from = forms.DateTimeField(widget=forms.HiddenInput(
attrs={'class': 'query-dt-from'}), required=False)
Expand Down Expand Up @@ -139,10 +139,7 @@ def _parse_query_dict(query_data, model):
elif query_data['value'] is False:
query_data['operator'] = "isfalse"
else:
if isinstance(mfield, DateField):
# this is a date/datetime field
query_data['operator'] = "range" # default
else:
if not query_data.get('operator') == "range":
query_data['operator'] = operator # default

if isinstance(query_data.get('value'),
Expand Down Expand Up @@ -173,6 +170,15 @@ def clean(self):
self.set_range_value(cleaned_data)
return cleaned_data

def clean_value(self):
value = self.cleaned_data['value']
op = self.cleaned_data.get('operator', '')
list = ['istrue', 'isfalse', 'isnull']
if op not in list:
self.fields['value'].required = True
return self.fields['value'].clean(value)
return value

def make_query(self, *args, **kwargs):
""" Returns a Q object from the submitted form """
query = Q() # initial is an empty query
Expand Down
92 changes: 71 additions & 21 deletions advanced_filters/static/advanced-filters/advanced-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,45 @@ var OperatorHandlers = function($) {
}
self.val_input.css({display: 'none'});

$(".hasDatepicker").datepicker("destroy");
try {
$(".hasDatepicker").datepicker("destroy");
} catch(e) {
console.warn("Can't destroy datepicker");
}
$from.addClass('vDateField');
$to.addClass('vDateField');
grappelli.initDateAndTimePicker();
try {
grappelli.initDateAndTimePicker();
} catch(e) {
console.warn("Can't ini datepicker with grappelli.initDateAndTimePicker");
}
};

self.remove_datepickers = function() {
self.val_input.css({display: 'block'});
if (self.val_input.parent().find('input.vDateField').length > 0) {
var datefields = self.val_input.parent().find('input.vDateField');
datefields.each(function() {
$(this).datepicker("destroy");
try {
$(this).datepicker("destroy");
} catch(e) {
console.warn("Can't destroy datepicker");
}
});
datefields.remove();
}
};

self.modify_widget = function(elm) {
// pick a widget for the value field according to operator

list = ['istrue', 'isfalse', 'isnull'];

self.value = $(elm).val();
self.val_input = $(elm).parents('tr').find('.query-value');
console.log("selected operator: " + self.value);
var field = $(elm).parents('tr').find('.query-field');
self.initialize_select2(field);

if (self.value == "range") {
self.add_datepickers();
Expand All @@ -69,27 +86,60 @@ var OperatorHandlers = function($) {
self.remove_datepickers();
}

if ($.inArray(self.value, ["istrue","isnull","isfalse"]) > -1) {
self.val_input.hide();
var input = $(elm).parents('tr').find('input.query-value');
if (list.includes(self.value)) {
input.prop('readonly', true);
input.css('pointer-events', 'none');
input.css('color', '#ccc');
} else {
self.val_input.show();
input.prop('readonly', false);
input.css('pointer-events', 'all');
input.css('color', 'black');
}
};

self.initialize_select2 = function(elm) {
// initialize select2 widget and populate field choices
var field = $(elm).val();
var choices_url = ADVANCED_FILTER_CHOICES_LOOKUP_URL + (FORM_MODEL ||
var op = $(elm).parents('tr').find('.query-operator').val();

if ($.inArray(op, ["iexact", "icontains", "iregex"]) > -1) {
var choices_url = ADVANCED_FILTER_CHOICES_LOOKUP_URL + (FORM_MODEL ||
MODEL_LABEL) + '/' + field;
var input = $(elm).parents('tr').find('input.query-value');
input.select2("destroy");
$.get(choices_url, function(data) {
input.select2({'data': data, 'createSearchChoice': function(term) {
return { 'id': term, 'text': term };
}});
});
var input = $(elm).parents('tr').find('input.query-value');
input.select2("destroy");
$.get(choices_url, function(data) {
input.select2({'data': data, 'multiple': op == "iregex", 'createSearchChoice': function(term) {
return { 'id': term, 'text': term };
}});
});
} else {
var input = $(elm).parents('tr').find('input.query-value');
input.select2("destroy");
}
};

// self.initialize_select2 = function(elm) {
// // initialize select2 widget and populate field choices
// var field = $(elm).val();
// var op = $(elm).parents('tr').find('.query-operator');
// if (field.includes('__') && op.val() == 'iexact') {
// var choices_url = ADVANCED_FILTER_CHOICES_LOOKUP_URL + (FORM_MODEL ||
// MODEL_LABEL) + '/' + field;
// var input = $(elm).parents('tr').find('input.query-value');
// input.select2("destroy");
// $.get(choices_url, function(data) {
// input.select2({'data': data, 'createSearchChoice': function(term) {
// return { 'id': term, 'text': term };
// }});
// });
// }
// else {
// var input = $(elm).parents('tr').find('input.query-value');
// input.select2("destroy");
// }
// };

self.field_selected = function(elm) {
self.selected_field_elm = elm;
var row = $(elm).parents('tr');
Expand Down Expand Up @@ -123,13 +173,13 @@ var OperatorHandlers = function($) {
}
$('.form-row select.query-operator').each(function() {
$(this).off("change");
$(this).data('pre_change', $(this).val());
// $(this).data('pre_change', $(this).val());
$(this).on("change", function() {
var before_change = $(this).data('pre_change');
if ($(this).val() != before_change) self.modify_widget(this);
$(this).data('pre_change', $(this).val());
}).change();
self.modify_widget(this);
// self.modify_widget(this);
});
$('.form-row select.query-field').each(function() {
$(this).off("change");
Expand All @@ -140,18 +190,18 @@ var OperatorHandlers = function($) {
$(this).data('pre_change', $(this).val());
}).change();
});
self.field_selected($('.form-row select.query-field').first());
// self.field_selected($('.form-row select.query-field').first());

};

self.destroy = function() {
$('.form-row select.query-operator').each(function() {
$('.form-row select.query-operator:last').each(function() {
$(this).off("change");
});
$('.form-row select.query-field').each(function() {
$('.form-row select.query-field:last').each(function() {
$(this).off("change");
});
$('.form-row input.query-value').each(function() {
$('.form-row input.query-value:last').each(function() {
$(this).select2("destroy");
});
};
Expand All @@ -166,4 +216,4 @@ var OperatorHandlers = function($) {
_af_handlers.init();
}
});
})(window._jq || jQuery);
})(window._jq || jQuery);

0 comments on commit 3f222cb

Please sign in to comment.