Skip to content

Commit

Permalink
Add Language filter to term listing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzohrab committed Nov 21, 2023
1 parent 85cc79a commit 6a64ef0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
22 changes: 18 additions & 4 deletions lute/templates/term/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
</p>
<div id="filterControls" style="display: none; margin-left: 20px;">
<table>
<tr>
<td>Language</td>
<td>
<select id="filtLanguage">
{% for langopt in language_options %}
<option value={{ langopt[0] }}>{{ langopt[1] }}</option>
{% endfor %}
</select>
</td>
<tr>
<td>Parent terms only</td>
<td>
Expand Down Expand Up @@ -163,6 +172,7 @@
// data sent to the controller, and are used by the
// TermRepository.
data: {
filtLanguage: () => $('#filtLanguage').val(),
filtParentsOnly: () => $('#filtParentsOnly').prop('checked'),
filtAgeMin: () => $('#filtAgeMin').val(),
filtAgeMax: () => $('#filtAgeMax').val(),
Expand Down Expand Up @@ -283,6 +293,7 @@

let handle_filter_update = function() {
const store = {
filtLanguage: $('#filtLanguage').val(),
filtDisplay: $('#filterControls').css('display'),
filtParentsOnly: $('#filtParentsOnly').prop('checked'),
filtAgeMin: $('#filtAgeMin').val(),
Expand All @@ -309,10 +320,11 @@
return;

$('#showHideFilters').prop('src', "{{ url_for('static', filename='icn/minus-button.png') }}");
$('#filtParentsOnly').prop('checked', store.filtParentsOnly);
if (store.filtAgeMin != '')
$('#filtLanguage').val(parseInt(store.filtLanguage ?? '0'));
$('#filtParentsOnly').prop('checked', store.filtParentsOnly ?? false);
if ((store.filtAgeMin ?? '') != '')
$('#filtAgeMin').val(parseInt(store.filtAgeMin));
if (store.filtAgeMax != '')
if ((store.filtAgeMax ?? '') != '')
$('#filtAgeMax').val(parseInt(store.filtAgeMax));
$('#filtStatusMin').val(parseInt(store.filtStatusMin));
$('#filtStatusMax').val(parseInt(store.filtStatusMax));
Expand All @@ -334,6 +346,7 @@
};

let handle_clear_filters = function() {
$('#filtLanguage').val(0);
$('#filtParentsOnly').prop('checked', false);
$('#filtAgeMin').val('');
$('#filtAgeMax').val('');
Expand All @@ -348,7 +361,8 @@
set_up_parent_autocomplete();
$('#btnSetParent').click(handle_set_parent_click);
$('#showHideFilters').click(handle_show_hide_filter_click);


$('#filtLanguage').change(handle_filter_update);
$('#filtParentsOnly').change(handle_filter_update);
$('#filtAgeMin').keyup(handle_filter_update);
$('#filtAgeMax').keyup(handle_filter_update);
Expand Down
69 changes: 38 additions & 31 deletions lute/term/datatables.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,50 @@ def get_data_tables_list(parameters):
LEFT OUTER JOIN wordimages wi on wi.WiWoID = w.WoID
"""

filt_parents_only = parameters["filtParentsOnly"]
filt_age_min = parameters["filtAgeMin"].strip()
filt_age_max = parameters["filtAgeMax"].strip()
filt_status_min = int(parameters["filtStatusMin"])
filt_status_max = int(parameters["filtStatusMax"])
filt_include_ignored = parameters["filtIncludeIgnored"]

typecrit = supported_parser_type_criteria()
wheres = [f"L.LgParserType in ({typecrit})"]
if filt_parents_only == "true":

# Add "where" criteria for all the filters.

# Have to check for 'null' for language filter.
# A new user may filter the language when the demo data is loaded,
# but on "wipe database" the filtLanguage value stored in localdata
# may be invalid, resulting in the filtLanguage form control actually
# sending the **string value** "null" here.
# The other filter values don't change with the data,
# so we don't need to check for null.
# Tricky tricky.
language_id = parameters["filtLanguage"]
if language_id == "null" or language_id is None:
language_id = "0"
language_id = int(language_id)
if language_id != 0:
wheres.append(f"L.LgID == {language_id}")

if parameters["filtParentsOnly"] == "true":
wheres.append("parents.parentlist IS NULL")
if filt_age_min:
filt_age_min = int(filt_age_min)
wheres.append(
f"cast(julianday('now') - julianday(w.wocreated) as int) >= {filt_age_min}"
)
if filt_age_max:
filt_age_max = int(filt_age_max)
wheres.append(
f"cast(julianday('now') - julianday(w.wocreated) as int) <= {filt_age_max}"
)

status_wheres = ["StID <> 98"]
if filt_status_min > 0:
status_wheres.append(f"StID >= {filt_status_min}")
if filt_status_max > 0:
status_wheres.append(f"StID <= {filt_status_max}")
sql_age_calc = "cast(julianday('now') - julianday(w.wocreated) as int)"
age_min = parameters["filtAgeMin"].strip()
if age_min:
wheres.append(f"{sql_age_calc} >= {int(age_min)}")
age_max = parameters["filtAgeMax"].strip()
if age_max:
wheres.append(f"{sql_age_calc} <= {int(age_max)}")

status_wheres = ["StID <> 98"]
status_min = int(parameters["filtStatusMin"])
status_max = int(parameters["filtStatusMax"])
if status_min > 0:
status_wheres.append(f"StID >= {status_min}")
if status_max > 0:
status_wheres.append(f"StID <= {status_max}")
status_wheres = " AND ".join(status_wheres)
if filt_include_ignored == "true":
if parameters["filtIncludeIgnored"] == "true":
status_wheres = f"({status_wheres} OR StID = 98)"
wheres.append(status_wheres)

where = " AND ".join(wheres)
full_base_sql = base_sql + " WHERE " + where

session = db.session
connection = session.connection()

return DataTablesSqliteQuery.get_data(full_base_sql, parameters, connection)
# Phew.
return DataTablesSqliteQuery.get_data(
base_sql + " WHERE " + " AND ".join(wheres), parameters, db.session.connection()
)
8 changes: 7 additions & 1 deletion lute/term/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
@bp.route("/index/<search>", methods=["GET"])
def index(search):
"Index page."
return render_template("term/index.html", initial_search=search)
languages = db.session.query(Language).order_by(Language.name).all()
langopts = [(lang.id, lang.name) for lang in languages]
langopts = [(0, "(all)")] + langopts
return render_template(
"term/index.html", initial_search=search, language_options=langopts
)


@bp.route("/datatables", methods=["POST"])
Expand All @@ -29,6 +34,7 @@ def datatables_active_source():
# The DataTablesFlaskParamParser doesn't know about term-specific filters,
# add those manually.
filter_param_names = [
"filtLanguage",
"filtParentsOnly",
"filtAgeMin",
"filtAgeMax",
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/term/test_datatables.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def fixture_dt_params():
# Filters - set "manually" in the route.
# Cheating here ... had to look at the request payload
# in devtools to see what was being sent.
"filtLanguage": "null", # Ha!
"filtParentsOnly": "false",
"filtAgeMin": "",
"filtAgeMax": "",
Expand All @@ -45,6 +46,7 @@ def test_smoke_term_datatables_query_runs(app_context, _dt_params):

def test_smoke_query_with_filter_params_runs(app_context, _dt_params):
"Smoke test with filters set."
_dt_params["filtLanguage"] = "44"
_dt_params["filtParentsOnly"] = "true"
_dt_params["filtAgeMin"] = "1"
_dt_params["filtAgeMax"] = "10"
Expand Down

0 comments on commit 6a64ef0

Please sign in to comment.