Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes #97

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions advanced_filters/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 @@ -134,12 +134,8 @@ 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'),
list) and query_data['operator'] == 'range':
date_from = date_to_string(query_data.get('value_from'))
Expand Down Expand Up @@ -168,6 +164,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
56 changes: 37 additions & 19 deletions advanced_filters/static/advanced-filters/advanced-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,65 @@ var OperatorHandlers = function($) {
}
self.val_input.css({display: 'none'});

$(".hasDatepicker").datepicker("destroy");
try {$(".hasDatepicker").datepicker("destroy");} catch(e) {}
$from.addClass('vDateField');
$to.addClass('vDateField');
grappelli.initDateAndTimePicker();
try {grappelli.initDateAndTimePicker();} catch(e) {}
};

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) {}
});
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();
} else {
self.remove_datepickers();
}

var input = $(elm).parents('tr').find('input.query-value');
if (list.includes(self.value)) {
input.prop('readonly', true);
} else {
input.prop('readonly', false);
}
};

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 ||
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 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) {
Expand Down Expand Up @@ -108,15 +126,16 @@ var OperatorHandlers = function($) {
// if only 1 form and it's empty, add first extra formset
$('[data-rules-formset] .add-row a').click();
}

$('.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 @@ -127,18 +146,17 @@ 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<form novalidate {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %}
{% with adminform.form.fields_formset as formset %}
<div id="advanced_filters">
<h1>{% trans "Change advanced filter" %}:</h1>
{% csrf_token %}
{{ formset.management_form }}
<!-- Fieldsets -->
Expand Down
2 changes: 1 addition & 1 deletion advanced_filters/templates/admin/common_js_init.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
}
});
})(django.jQuery);
</script>
</script>