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

fix: improve get_member_for_entity helper function #417

Merged
merged 1 commit into from
Nov 14, 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
2 changes: 1 addition & 1 deletion apis_core/apis_entities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_table(self, **kwargs):
default_cols = entity_settings.get("table_fields", [])
default_cols = default_cols + selected_cols

self.table_class = get_member_for_entity(self.get_model(), suffix="table")
self.table_class = get_member_for_entity(self.get_model(), suffix="Table")
if self.table_class is None:
self.table_class = get_entities_table(class_name, default_cols=default_cols)
table = super(GenericListViewNew, self).get_table()
Expand Down
10 changes: 3 additions & 7 deletions apis_core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,19 @@ def get_member_for_entity(
module = ct.app_label
model = ct.model.capitalize()
if suffix:
model += suffix.capitalize()
model += suffix
if path:
module += f".{path}"
else:
if suffix:
module += f".{suffix.lower()}s"
logging.debug("Checking if %s.%s exists", module, model)
try:
members = inspect.getmembers(importlib.import_module(module))
members = list(filter(lambda c: c[0] == model, members))
except ModuleNotFoundError:
members = []
if members:
logging.debug(
"Based on path %s and suffix %s using class %s",
path,
suffix,
model,
)
logging.debug("Found %s.%s", module, model)
return members[0][1]
return None