Skip to content

Commit 4a9b9ed

Browse files
committed
Give CompoundMixin the ability to formulate rels
Previously I had moved some of the methods for formulating combined `select_` and `prefetch_related` lists for multiple children from `CompoundMixin` down into the `AttachedRecordExporter`, because I thought the latter class would be the only place where that was really useful. In working on blacklight exporters, I discovered this is not the case, so I've moved that functionality back up into `CompoundMixin`, but a little more generalized. The functionality that IS specific to `AttachedRecordExporter` has been moved into the associated `Child` class, for determining relationship strings for attached children.
1 parent 0c7e3b6 commit 4a9b9ed

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

django/sierra/export/exporter.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ def expclass(self):
485485
self._expclass = export_type.get_exporter_class()
486486
return self._expclass
487487

488+
def derive_rel_list(self, exporter, which_rel):
489+
return getattr(exporter, which_rel, [])
490+
488491
def get_patched_class_attrs(self):
489492
"""
490493
Implement this method in subclasses if additional attrs
@@ -521,6 +524,24 @@ def children(self):
521524
self._children = type(self).spawn_children(args)
522525
return self._children
523526

527+
@staticmethod
528+
def combine_lists(*lists):
529+
"""
530+
This is a helper method for combining and deduplicating entries
531+
from multiple lists, returning one sorted, flattened list.
532+
"""
533+
combined_dupes = sorted([item for l in lists for item in l])
534+
return OrderedDict.fromkeys(combined_dupes).keys()
535+
536+
def combine_rels_from_children(self, which_rel, which_children=None):
537+
"""
538+
This is a helper method for combining lists of relations (like
539+
select_related or prefetch_related) from 1+ children.
540+
"""
541+
children = which_children or self.children.values()
542+
rel_lists = [c._config.derive_rel_list(c, which_rel) for c in children]
543+
return self.combine_lists(*rel_lists)
544+
524545
def get_records_from_children(self, deletions=False, prefetch=True,
525546
which_children=None):
526547
"""
@@ -651,11 +672,17 @@ class AttachedRecordExporter(CompoundMixin, Exporter):
651672
relative to the parent.
652673
"""
653674
class Child(CompoundMixin.Child):
654-
rel_prefix = ''
675+
rel_prefix = None
655676

656677
def derive_records_from_parent(self, parent_record):
657678
return [parent_record]
658679

680+
def derive_rel_list(self, exporter, which_rel):
681+
base_list = getattr(exporter, which_rel, [])
682+
if self.rel_prefix:
683+
return ['{}__{}'.format(self.rel_prefix, r) for r in base_list]
684+
return base_list
685+
659686
children_config = tuple()
660687

661688
@property
@@ -666,29 +693,6 @@ def main_child(self):
666693
def attached_children(self):
667694
return [c[1] for c in self.children.items()[1:]]
668695

669-
@staticmethod
670-
def combine_lists(*lists):
671-
"""
672-
This is a helper method for combining and deduplicating entries
673-
from multiple lists, returning one sorted, flattened list.
674-
"""
675-
combined_dupes = sorted([item for l in lists for item in l])
676-
return OrderedDict.fromkeys(combined_dupes).keys()
677-
678-
def combine_rels_from_children(self, which_rel, which_children=None):
679-
"""
680-
This is a helper method for combining lists of relations (like
681-
select_related or prefetch_related) from 1+ children.
682-
"""
683-
rel_lists = []
684-
for child in which_children or self.children.values():
685-
rel_prefix = child._config.rel_prefix
686-
base_list = getattr(child, which_rel)
687-
if rel_prefix:
688-
base_list = ['{}__{}'.format(rel_prefix, r) for r in base_list]
689-
rel_lists.append(base_list)
690-
return self.combine_lists(*rel_lists)
691-
692696
@property
693697
def select_related(self):
694698
"""

0 commit comments

Comments
 (0)