Skip to content

Commit

Permalink
refactor: use utils.settings.get_entity_settings_by_modelname
Browse files Browse the repository at this point in the history
This replaces every occurence of direct access to settings.APIS_ENTITIES
with the helper function `get_entity_settings_by_modelname`
  • Loading branch information
b1rger committed Oct 5, 2023
1 parent a0c9c1d commit f679ec1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
5 changes: 3 additions & 2 deletions apis_core/apis_entities/autocomplete3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from apis_core.apis_vocabularies.models import VocabsBaseClass
from apis_core.utils import caching
from apis_core.utils.caching import get_autocomplete_property_choices
from apis_core.utils.settings import get_entity_settings_by_modelname

path_ac_settings = getattr(settings, "APIS_AUTOCOMPLETE_SETTINGS", False)
if path_ac_settings:
Expand Down Expand Up @@ -160,7 +161,7 @@ def get(self, request, *args, **kwargs):
q = q.strip()
arg_list = [
Q(**{x + search_type: q})
for x in settings.APIS_ENTITIES[ent_model_name]["search"]
for x in get_entity_settings_by_modelname(ent_model_name).get("search", [])
]
res = ent_model.objects.filter(reduce(operator.or_, arg_list)).distinct()
if q3:
Expand Down Expand Up @@ -446,7 +447,7 @@ def get(self, request, *args, **kwargs):
try:
arg_list = [
Q(**{x + "__icontains": q})
for x in settings.APIS_ENTITIES[entity.title()]["search"]
for x in get_entity_settings_by_modelname(entity.title()).get("search", [])
]
except KeyError:
arg_list = [Q(**{x + "__icontains": q}) for x in ["name"]]
Expand Down
9 changes: 4 additions & 5 deletions apis_core/apis_entities/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from apis_core.apis_metainfo.models import Collection
from apis_core.apis_entities.models import TempEntityClass
from apis_core.utils import caching
from apis_core.utils.settings import get_entity_settings_by_modelname

# The following classes define the filter sets respective to their models.
# Also by what was enabled in the global settings file (or disabled by not explicitley enabling it).
Expand Down Expand Up @@ -103,11 +104,9 @@ def set_and_sort_filters(self, filters):
"status",
"references",
]
list_filters = []
if settings.APIS_ENTITIES:
entity_settings = settings.APIS_ENTITIES.get(self.entity_class.__name__, {})
list_filters = entity_settings.get("list_filters", {})
list_filters_exclude.extend(entity_settings.get("list_filters_exclude", []))
entity_settings = get_entity_settings_by_modelname(self.entity_class.__name__)
list_filters = entity_settings.get("list_filters", {})
list_filters_exclude.extend(entity_settings.get("list_filters_exclude", []))

# The `list_filters` setting of an entity defines both the order of filters and it allows to define additional filters
# A filter is defined by a name and a dict describing its attributes
Expand Down
16 changes: 6 additions & 10 deletions apis_core/apis_entities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from apis_core.apis_metainfo.models import Uri, Collection
from apis_core.apis_vocabularies.models import TextType
from apis_core.utils import DateParser, caching, settings as apis_settings
from apis_core.utils.settings import get_entity_settings_by_modelname
from .fields import ListSelect2, Select2Multiple

if "apis_highlighter" in settings.INSTALLED_APPS:
Expand Down Expand Up @@ -159,17 +160,12 @@ def sort_fields_list(field_names, entity_name):
:param entity_name: string representation of an entity name
:return: a list of strings
"""
entity_settings = getattr(settings, "APIS_ENTITIES", None)
entity_settings = get_entity_settings_by_modelname(entity_name)
form_order = entity_settings.get("form_order", [])

if entity_settings is not None:
try:
form_order = entity_settings[entity_name].get("form_order", [])
except KeyError as e:
form_order = []

if len(form_order) > 0:
remaining = set(field_names) - set(form_order)
field_names = form_order + list(remaining)
if len(form_order) > 0:
remaining = set(field_names) - set(form_order)
field_names = form_order + list(remaining)

return field_names

Expand Down
11 changes: 5 additions & 6 deletions apis_core/apis_entities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
access_for_all_function,
ENTITIES_DEFAULT_COLS,
)
from apis_core.utils.settings import get_entity_settings_by_modelname
from .filters import get_list_filter_of_entity
from .forms import (
GenericFilterFormHelper,
Expand Down Expand Up @@ -136,9 +137,8 @@ def get_table(self, **kwargs):
"columns"
) # populates "Select additional columns" dropdown
default_cols = [] # get set to "name" in get_entities_table when empty
if hasattr(settings, "APIS_ENTITIES"):
class_settings = settings.APIS_ENTITIES.get(class_name, {})
default_cols = class_settings.get("table_fields", [])
entity_settings = get_entity_settings_by_modelname(class_name)
default_cols = entity_settings.get("table_fields", [])
default_cols = default_cols + selected_cols

self.table_class = get_entities_table(
Expand Down Expand Up @@ -229,9 +229,8 @@ def get_context_data(self, **kwargs):
context = dict(context, **chartdata)

toggleable_cols = []
if hasattr(settings, "APIS_ENTITIES"):
entity_settings = settings.APIS_ENTITIES.get(class_name, {})
toggleable_cols = entity_settings.get("additional_cols", [])
entity_settings = get_entity_settings_by_modelname(class_name)
toggleable_cols = entity_settings.get("additional_cols", [])

# TODO kk spelling of this dict key should get fixed throughout
# (togglable_colums -> toggleable_columns)
Expand Down

0 comments on commit f679ec1

Please sign in to comment.