Skip to content

Commit f260ddd

Browse files
committed
feat: adds "index" option to register_object_tag_class
to allow subclasses to be registered wherever they want to be.
1 parent 3e53687 commit f260ddd

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

openedx_tagging/core/tagging/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,6 @@ def get_tag_options(self) -> Iterator[TagResult]:
593593
break
594594

595595

596-
# Register the object tag classes in reverse order for how we want them considered
597-
register_object_tag_class(OpenObjectTag)
596+
# We'd rather match closed taxonomies than open ones when casting ObjectTags, so we register ClosedObjectTag first.
598597
register_object_tag_class(ClosedObjectTag)
598+
register_object_tag_class(OpenObjectTag)

openedx_tagging/core/tagging/registry.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
_OBJECT_TAG_CLASS_REGISTRY = []
1111

1212

13-
def register_object_tag_class(cls):
13+
def register_object_tag_class(cls, index=0):
1414
"""
1515
Register a given class as a candidate object tag class.
1616
17+
By default, inserts the given class at the beginning of the list, so that it will be considered before all
18+
previously-registered classes. Adjust ``index`` to change where the class will be considered.
19+
1720
The class must have a `valid_for` class method.
1821
"""
1922
assert hasattr(cls, "valid_for")
20-
_OBJECT_TAG_CLASS_REGISTRY.append(cls)
23+
_OBJECT_TAG_CLASS_REGISTRY.insert(index, cls)
2124

2225

2326
def get_object_tag_class(
@@ -44,8 +47,8 @@ def get_object_tag_class(
4447
# Log error and continue
4548
log.exception(f"Unable to import custom object_tag_class for {taxonomy}")
4649

47-
# Return the most recently-registered, appropriate object tag class
48-
for ObjectTagClass in reversed(_OBJECT_TAG_CLASS_REGISTRY):
50+
# Return the first appropriate object tag class
51+
for ObjectTagClass in _OBJECT_TAG_CLASS_REGISTRY:
4952
if ObjectTagClass.valid_for(
5053
taxonomy=taxonomy,
5154
object_type=object_type,

0 commit comments

Comments
 (0)