Skip to content

Commit

Permalink
feat: add nested join for Taxref table
Browse files Browse the repository at this point in the history
if fields query params is equal to "medias.types" or
"attributs.bib_attributs" then join is loaded.

Reviewed-by: andriacap
  • Loading branch information
andriacap committed Nov 20, 2024
1 parent dc6a1e8 commit 7ca8758
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,35 @@ def joined_load(cls, fields=None, *, query, **kwargs):
for f in fields:
if f in Taxref.__mapper__.relationships:
query_option.append(joinedload(getattr(Taxref, f)))
elif "." in f:
parts = f.split(".")
base_relation = parts[0]
sub_relation = ".".join(parts[1:])

Check warning on line 210 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L208-L210

Added lines #L208 - L210 were not covered by tests

if base_relation in Taxref.__mapper__.relationships:
rel = getattr(Taxref, base_relation)
sub_query = joinedload(rel)

Check warning on line 214 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L212-L214

Added lines #L212 - L214 were not covered by tests

for sub in sub_relation.split("."):
if sub in rel.mapper.relationships:
sub_query = sub_query.joinedload(getattr(rel.mapper.class_, sub))

Check warning on line 218 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L216-L218

Added lines #L216 - L218 were not covered by tests
else:
# Cas spécifique pour gérer les relations via CorTaxonAttribut
if base_relation == "attributs":

Check warning on line 221 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L221

Added line #L221 was not covered by tests
# Ici on rejoint la table CorTaxonAttribut
sub_query = joinedload(

Check warning on line 223 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L223

Added line #L223 was not covered by tests
Taxref.attributs
) # Charger la relation attributs
for sub in sub_relation.split("."):
if (

Check warning on line 227 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L226-L227

Added lines #L226 - L227 were not covered by tests
sub == "bib_attribut"
): # Si on cherche à aller dans BibAttributs
sub_query = sub_query.joinedload(

Check warning on line 230 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L230

Added line #L230 was not covered by tests
CorTaxonAttribut.bib_attribut
)
query_option.append(sub_query)

Check warning on line 233 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L233

Added line #L233 was not covered by tests

query_option.append(sub_query)

Check warning on line 235 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L235

Added line #L235 was not covered by tests
query = query.options(*tuple(query_option))

return query
Expand Down

0 comments on commit 7ca8758

Please sign in to comment.