Skip to content

Commit

Permalink
feat(relations): allow more generic relation tables
Browse files Browse the repository at this point in the history
This extends the list of possible relation table classes by also looking
at the MRO of the target of the relation. This way it possible to make
one table for a relation between two parent classes that is used for
displaying relations between two child classes of those parent classes.
  • Loading branch information
b1rger committed Oct 7, 2024
1 parent db2b008 commit 6142910
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions apis_core/relations/templatetags/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Case, Q, Value, When

from apis_core.generic.helpers import first_member_match, module_paths
from apis_core.generic.helpers import first_member_match, module_paths, mro_paths
from apis_core.relations.models import Relation
from apis_core.relations.tables import RelationsListTable
from apis_core.relations.utils import relation_content_types, relation_match_target
Expand Down Expand Up @@ -58,11 +58,16 @@ def relations_from(from_obj, relation_type: ContentType = None):

@register.simple_tag(takes_context=True)
def relations_list_table(context, relations, target=None):
table_modules = module_paths(
type(context["object"]),
path="tables",
suffix=f"{target.name.capitalize()}RelationsTable",
)
suffixes = ["RelationsTable"]
if target:
suffixes.extend(
f"{module[-1]}RelationsTable" for module in mro_paths(target.model_class())
)
table_modules = ()
for suffix in suffixes:
table_modules += module_paths(
type(context["object"]), path="tables", suffix=suffix
)
table_class = first_member_match(table_modules, RelationsListTable)
if target:
relations = [
Expand Down

0 comments on commit 6142910

Please sign in to comment.