Skip to content

Commit

Permalink
fix: sink only object tag id
Browse files Browse the repository at this point in the history
fix: dump tags ids

fix: add use set to store tags

chore: quality fixes
  • Loading branch information
Ian2012 committed Aug 30, 2024
1 parent cc90b17 commit e3fea7c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
16 changes: 7 additions & 9 deletions platform_plugin_aspects/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from django.db import transaction
from django.db.models.signals import post_save, post_delete
from django.db.models.signals import post_delete, post_save
from django.dispatch import Signal, receiver

from platform_plugin_aspects.sinks import (
Expand Down Expand Up @@ -200,16 +200,13 @@ def on_taxonomy_saved( # pylint: disable=unused-argument # pragma: no cover
post_save.connect(on_taxonomy_saved, sender=_taxonomy) # pragma: no cover


def on_object_tag_saved( # pylint: disable=unused-argument # pragma: no cover
sender, instance, **kwargs
):
def on_object_tag_saved(sender, instance, **kwargs): # pragma: no cover
"""
Receives post save signal and queues the dump job.
"""
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
from platform_plugin_aspects.tasks import ( # pylint: disable=import-outside-toplevel
dump_data_to_clickhouse,
dump_course_to_clickhouse
)

sink = ObjectTagSink(None, None)
Expand All @@ -230,20 +227,21 @@ def on_object_tag_deleted( # pylint: disable=unused-argument # pragma: no cove
"""
# import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
from platform_plugin_aspects.tasks import ( # pylint: disable=import-outside-toplevel
dump_course_to_clickhouse
dump_course_to_clickhouse,
)

CourseOverview = get_model("course_overviews")
if CourseOverview:
try:
CourseOverview.objects.get(id=instance.object_id)
dump_course_to_clickhouse.delay(instance.object_id)
except CourseOverview.DoesNotExist as exc:
except CourseOverview.DoesNotExist:
pass


# Connect the ExternalId.post_save signal handler only if we have a model to attach to.
# (prevents celery errors during tests)
_object_tag = get_model("object_tag")
if _object_tag:
post_save.connect(on_object_tag_saved, sender=_object_tag) # pragma: no cover
if _object_tag: # pragma: no cover
post_save.connect(on_object_tag_saved, sender=_object_tag)
post_delete.connect(on_object_tag_deleted, sender=_object_tag)
23 changes: 13 additions & 10 deletions platform_plugin_aspects/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,27 +244,30 @@ def test_get_tags_for_block(self, mock_get_object_tags):
mock_taxonomy2 = Mock()
mock_taxonomy2.name = "Taxonomy Two"

def mock_tag(taxonomy, value, parent=None):
def mock_tag(taxonomy, value, i, parent=None):
"""
Returns a mock ObjectTag.
"""
mock_tag = Mock()
mock_tag.id = i
mock_tag.taxonomy = taxonomy
mock_tag.value = value
mock_tag.tag = mock_tag
mock_tag.tag.parent = parent
return mock_tag

mock_tag11 = mock_tag(mock_taxonomy1, "tag1.1")
mock_tag12 = mock_tag(mock_taxonomy1, "tag1.2", mock_tag11.tag)
mock_tag13 = mock_tag(mock_taxonomy1, "tag1.3", mock_tag12.tag)
mock_tag21 = mock_tag(mock_taxonomy2, "tag2.1")
mock_tag22 = mock_tag(mock_taxonomy2, "tag2.2")
i = 1
mock_tag11 = mock_tag(mock_taxonomy1, "tag1.1", i)
i += 1
mock_tag12 = mock_tag(mock_taxonomy1, "tag1.2", i, mock_tag11.tag)
i += 1
mock_tag13 = mock_tag(mock_taxonomy1, "tag1.3", i, mock_tag12.tag)
i += 1
mock_tag21 = mock_tag(mock_taxonomy2, "tag2.1", i)
i += 1
mock_tag22 = mock_tag(mock_taxonomy2, "tag2.2", i)
mock_get_object_tags.return_value = [mock_tag13, mock_tag21, mock_tag22]

course_tags = get_tags_for_block(course.location)
assert course_tags == {
"Taxonomy One": ["tag1.3", "tag1.2", "tag1.1"],
"Taxonomy Two": ["tag2.1", "tag2.2"],
}
assert course_tags == [1, 2, 3, 4, 5]
mock_get_object_tags.assert_called_once_with(course.location)
35 changes: 11 additions & 24 deletions platform_plugin_aspects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,18 @@ def _get_object_tags(usage_key): # pragma: no cover
return {}


def get_tags_for_block(usage_key) -> dict:
def get_tags_for_block(usage_key) -> set:
"""
Return all the tags (and their parent tags) applied to the given block.
Returns a dict of [taxonomy]: [tag, tag, tag]
Returns a set of tag id: [1, 2, 3]
"""
tags = _get_object_tags(usage_key)
serialized_tags = {}

for explicit_tag in tags:
_add_tag(explicit_tag, serialized_tags)
implicit_tag = explicit_tag.tag.parent

while implicit_tag:
_add_tag(implicit_tag, serialized_tags)
implicit_tag = implicit_tag.parent

return serialized_tags


def _add_tag(tag, serialized_tags):
"""
Add a tag to our serialized list of tags.
"""
if tag.taxonomy.name not in serialized_tags:
serialized_tags[tag.taxonomy.name] = [tag.value]
else:
serialized_tags[tag.taxonomy.name].append(tag.value)
object_tags = _get_object_tags(usage_key)
serialized_tags = set()
for object_tag in object_tags:
tag = object_tag.tag
while tag:
serialized_tags.add(tag.id)
tag = tag.parent

return list(serialized_tags)

0 comments on commit e3fea7c

Please sign in to comment.