-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from PnX-SI/admin_filters
filters from database list
- Loading branch information
Showing
5 changed files
with
151 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
*~ | ||
|
||
static/medias/* | ||
|
||
node_modules | ||
#taxhub app | ||
settings.ini | ||
venv | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from functools import partial | ||
|
||
from flask import has_app_context | ||
from flask_admin.model.filters import BaseFilter | ||
|
||
from flask_admin.contrib.sqla.filters import FilterEqual | ||
from flask_admin.babel import lazy_gettext | ||
|
||
|
||
from apptax.taxonomie.models import ( | ||
Taxref, | ||
BibAttributs, | ||
CorTaxonAttribut, | ||
BibListes, | ||
CorNomListe, | ||
TMedias, | ||
) | ||
|
||
|
||
# https://github.com/flask-admin/flask-admin/issues/1807 | ||
# https://stackoverflow.com/questions/54638047/correct-way-to-register-flask-admin-views-with-application-factory | ||
class ReloadingIterator: | ||
def __init__(self, iterator_factory): | ||
self.iterator_factory = iterator_factory | ||
|
||
def __iter__(self): | ||
return self.iterator_factory() | ||
|
||
|
||
class DynamicOptionsMixin: | ||
def get_dynamic_options(self, view): | ||
raise NotImplementedError | ||
|
||
def get_options(self, view): | ||
return ReloadingIterator(partial(self.get_dynamic_options, view)) | ||
|
||
|
||
class TaxrefDistinctFilter(DynamicOptionsMixin, FilterEqual): | ||
def get_dynamic_options(self, view): | ||
if has_app_context(): | ||
yield from [ | ||
(getattr(row, self.column.key), getattr(row, self.column.key)) | ||
for row in Taxref.query.distinct(self.column).order_by(self.column).all() | ||
] | ||
|
||
|
||
class FilterTaxrefAttr(DynamicOptionsMixin, FilterEqual): | ||
def apply(self, query, value, alias=None): | ||
return query.join(CorTaxonAttribut).filter(CorTaxonAttribut.id_attribut == value) | ||
|
||
def get_dynamic_options(self, view): | ||
if has_app_context(): | ||
yield from [ | ||
(attr.id_attribut, attr.label_attribut) for attr in BibAttributs.query.all() | ||
] | ||
|
||
|
||
class FilterBiblist(DynamicOptionsMixin, FilterEqual): | ||
def apply(self, query, value, alias=None): | ||
return query.join(CorNomListe).filter(CorNomListe.id_liste == value) | ||
|
||
def get_dynamic_options(self, view): | ||
if has_app_context(): | ||
yield from [(list.id_liste, list.nom_liste) for list in BibListes.query.all()] | ||
|
||
|
||
class FilterIsValidName(BaseFilter): | ||
def apply(self, query, value, alias=None): | ||
if int(value) == 1: | ||
return query.filter(Taxref.cd_nom == Taxref.cd_ref) | ||
else: | ||
return query.filter(Taxref.cd_nom != Taxref.cd_ref) | ||
|
||
def operation(self): | ||
return lazy_gettext("equal") | ||
|
||
|
||
class FilterMedia(BaseFilter): | ||
def apply(self, query, value, alias=None): | ||
medias_filter = Taxref.medias.any() | ||
if int(value) == 1: | ||
return query.filter(medias_filter) | ||
else: | ||
return query.filter(~medias_filter) | ||
|
||
def operation(self): | ||
return lazy_gettext("equal") | ||
|
||
|
||
class FilterAttributes(BaseFilter): | ||
def apply(self, query, value, alias=None): | ||
attr_filter = Taxref.attributs.any() | ||
if int(value) == 1: | ||
return query.filter(attr_filter) | ||
else: | ||
return query.filter(~attr_filter) | ||
|
||
def operation(self): | ||
return lazy_gettext("equal") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
$(document).ready(function() { | ||
let search = document.getElementById("search_value").value; | ||
let url = (APPLICATION_ROOT + "/api/taxref/search/lb_nom/").replace("//", "/"); | ||
$(document).ready(function () { | ||
let search = document.getElementById("search_value").value; | ||
let url = (APPLICATION_ROOT + "api/taxref/allnamebylist").replace("//", "/"); | ||
$(".taxref-autocomplete").select2({ | ||
ajax: { | ||
url: function (params) { | ||
console.log(params) | ||
if (search !==undefined && params == "") { | ||
if (search !== undefined && params == "") { | ||
params = search; | ||
} | ||
return url + params | ||
return url + "?search_name=" + params; | ||
}, | ||
results: function (data, page) { | ||
var data = $.map(data, function (obj) { | ||
obj.id = obj.id || obj.lb_nom; | ||
obj.text = obj.text || obj.lb_nom; | ||
var search_name = obj.search_name.replace("<i>", ""); | ||
search_name = search_name.replace("</i>", ""); | ||
obj.id = obj.cd_nom; | ||
obj.text = search_name; | ||
return obj; | ||
}); | ||
return {results : data} | ||
return { results: data }; | ||
}, | ||
dataType: 'json', | ||
delay: 250, | ||
placeholder: "Saisir les trois premières lettres du nom d'un taxon", | ||
minimumInputLength: 3 | ||
}, | ||
dataType: "json", | ||
delay: 250, | ||
placeholder: "Nom latin, nom vernaculaire, cd_nom", | ||
minimumInputLength: 3, | ||
}); | ||
|
||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters