Skip to content

Commit

Permalink
Support Is /not Null filter for value is None on webui (apache#26584)
Browse files Browse the repository at this point in the history
* Support Not/Equal To filter for value is None on webui

closes: apache#26427

* Support Is /not Null filter for value is None on webui

closes: apache#26427

* Update airflow/www/static/js/main.js

Co-authored-by: Brent Bovenzi <[email protected]>

Co-authored-by: Brent Bovenzi <[email protected]>
  • Loading branch information
chenglongyan and bbovenzi authored Oct 11, 2022
1 parent 6462292 commit 8e2e80a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
33 changes: 33 additions & 0 deletions airflow/www/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ function initializeUITimezone() {
});
}

function filterOpSelected(ele) {
const op = $(ele);
const filterVal = $('.filter_val.form-control', op.parents('tr'));

if (op.text() === 'Is Null' || op.text() === 'Is not Null') {
if (filterVal.attr('required') !== undefined) {
filterVal.removeAttr('required');
filterVal.attr('airflow-required', true);
}

if (filterVal.parent('.datetime').length === 1) {
filterVal.parent('.datetime').hide();
} else {
filterVal.hide();
}
} else {
if (filterVal.attr('airflow-required') === 'true') {
filterVal.attr('required', true);
filterVal.removeAttr('airflow-required');
}

if (filterVal.parent('.datetime').length === 1) {
filterVal.parent('.datetime').show();
} else {
filterVal.show();
}
}
}

$(document).ready(() => {
initializeUITimezone();

Expand All @@ -226,10 +255,14 @@ $(document).ready(() => {
$.fn.datetimepicker.defaults.sideBySide = true;
$('.datetimepicker').datetimepicker();

$('.filters .select2-chosen').each((idx, elem) => { filterOpSelected(elem); });
$('.filters .select2-chosen').on('DOMNodeInserted', (e) => { filterOpSelected(e.target); });

// Fix up filter fields from FAB adds to the page. This event is fired after
// the FAB registered one which adds the new control
$('#filter_form a.filter').click(() => {
$('.datetimepicker').datetimepicker();
$('.filters .select2-chosen').on('DOMNodeInserted', (e) => { filterOpSelected(e.target); });
});

// Global Tooltip selector
Expand Down
38 changes: 37 additions & 1 deletion airflow/www/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,38 @@ class UtcAwareFilterMixin:

def apply(self, query, value):
"""Apply the filter."""
value = timezone.parse(value, timezone=timezone.utc)
if isinstance(value, str) and not value.strip():
value = None
else:
value = timezone.parse(value, timezone=timezone.utc)

return super().apply(query, value)


class FilterIsNull(BaseFilter):
"""Is null filter."""

name = lazy_gettext("Is Null")
arg_name = "emp"

def apply(self, query, value):
query, field = get_field_setup_query(query, self.model, self.column_name)
value = set_value_to_type(self.datamodel, self.column_name, None)
return query.filter(field == value)


class FilterIsNotNull(BaseFilter):
"""Is not null filter."""

name = lazy_gettext("Is not Null")
arg_name = "nemp"

def apply(self, query, value):
query, field = get_field_setup_query(query, self.model, self.column_name)
value = set_value_to_type(self.datamodel, self.column_name, None)
return query.filter(field != value)


class FilterGreaterOrEqual(BaseFilter):
"""Greater than or Equal filter."""

Expand Down Expand Up @@ -660,6 +687,15 @@ class AirflowFilterConverter(fab_sqlafilters.SQLAFilterConverter):
),
) + fab_sqlafilters.SQLAFilterConverter.conversion_table

def __init__(self, datamodel):
super().__init__(datamodel)

for (method, filters) in self.conversion_table:
if FilterIsNull not in filters:
filters.append(FilterIsNull)
if FilterIsNotNull not in filters:
filters.append(FilterIsNotNull)


class CustomSQLAInterface(SQLAInterface):
"""
Expand Down

0 comments on commit 8e2e80a

Please sign in to comment.