Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use utils.settings.get_entity_settings_by_modelname #322

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 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 @@ -130,6 +131,11 @@ def get(self, request, *args, **kwargs):
ent_model = caching.get_ontology_class_of_name(ac_type)
ent_model_name = ent_model.__name__

model_fields = ent_model._meta.get_fields()
default_search_fields = [
field.name for field in model_fields if not field.is_relation
]

# inspect user search query
q3 = None
if self.q.startswith("http"):
Expand Down Expand Up @@ -160,7 +166,9 @@ 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", default_search_fields
)
]
res = ent_model.objects.filter(reduce(operator.or_, arg_list)).distinct()
if q3:
Expand Down Expand Up @@ -446,7 +454,9 @@ 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