django-orchestra/orchestra/contrib/orders/helpers.py
Santiago L 97f1c7ef2b Replace field.rel.to with field.remote_field.model
Field.rel and Field.remote_field.to are removed in Django 2.x
2021-04-22 14:44:47 +02:00

40 lines
1.3 KiB
Python

from django.core.exceptions import ObjectDoesNotExist
from orchestra.core import services
def get_related_object(origin, max_depth=2):
"""
Introspects origin object and return the first related service object
WARNING this is NOT an exhaustive search but a compromise between cost and
flexibility. A more comprehensive approach may be considered if
a use-case calls for it.
"""
def related_iterator(node):
for field in node._meta.private_fields:
if hasattr(field, 'ct_field'):
yield getattr(node, field.name)
for field in node._meta.fields:
if field.remote_field:
try:
yield getattr(node, field.name)
except ObjectDoesNotExist:
pass
# BFS model relation transversal
queue = [[origin]]
while queue:
models = queue.pop(0)
if len(models) > max_depth:
return None
node = models[-1]
if len(models) > 1:
if type(node) in services:
return node
for related in related_iterator(node):
if related and related not in models:
new_models = list(models)
new_models.append(related)
queue.append(new_models)