Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Let filter for empty choices
Browse files Browse the repository at this point in the history
This adds an additional `empty` choice to some ChoiceFields - the used
filtermethod than uses this keyword to check if the database field
contains an empty string (i.e. the field is not set).
  • Loading branch information
b1rger committed Sep 28, 2023
1 parent c32206a commit 2eb8f45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions apis_ontology/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ def name_alternative_name_filter(queryset, name, value):
return queryset.filter(
Q(name__icontains=value) |
Q(alternative_label__icontains=value))

def filter_empty_string(queryset, name, value):
if value == "empty":
value = ""
lookup = f"{name}__exact"
return queryset.filter(**{lookup: value})
23 changes: 21 additions & 2 deletions apis_ontology/settings/server_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,27 @@

APIS_RELATIONS_FILTER_EXCLUDE += ["annotation", "annotation_set_relation"]

from apis_ontology.filters import name_first_name_alternative_name_filter, name_alternative_name_filter
from apis_ontology.filters import name_first_name_alternative_name_filter, name_alternative_name_filter, filter_empty_string
#INSTALLED_APPS.append("apis_highlighter")
def salarychoices():
from apis_ontology.models import Salary
return Salary.TYP_CHOICES + (("empty", "Nicht gesetzt"),)

def placechoices():
from apis_ontology.models import Place
return Place.TYPE_CHOICES + (("empty", "Nicht gesetzt"),)

def genderchoices():
from apis_ontology.models import Person
return Person.GENDER_CHOICES + (("empty", "Nicht gesetzt"),)

APIS_ENTITIES = {
"Salary": {
"relations_per_page": 100,
"search": ["name"]
"search": ["name"],
"list_filters": {
"typ": {"method": filter_empty_string, "extra": {"choices": salarychoices, "required": False }},
}
},
"Function": {
"relations_per_page": 100,
Expand All @@ -107,6 +122,9 @@
"form_order": ["name", "kind", "lat", "lng", "status", "collection"],
"table_fields": ["name"],
"additional_cols": ["id", "lat", "lng", "part_of"],
"list_filters": {
"type": {"method": filter_empty_string, "extra": {"choices": placechoices, "required": False }},
}
},
"Person": {
"relations_per_page": 100,
Expand All @@ -131,6 +149,7 @@
"additional_cols": ["id", "gender"],
"list_filters": {
"name": {"method": name_first_name_alternative_name_filter, "label": "Name or first name or alternative name"},
"gender": {"method": filter_empty_string, "extra": {"choices": genderchoices, "required": False}},
},
},
"Institution": {
Expand Down

0 comments on commit 2eb8f45

Please sign in to comment.