From fa25ea63e750d8a78cd1629d7084c15d50c72bb2 Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Wed, 11 Sep 2024 15:33:16 -0500 Subject: [PATCH] feat: add xblock aside for in context analytics --- platform_plugin_aspects/apps.py | 3 + platform_plugin_aspects/signals.py | 4 +- .../static/html/example.html | 3 + platform_plugin_aspects/xblock_aside.py | 82 +++++++++++++++++++ setup.py | 3 + 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 platform_plugin_aspects/static/html/example.html create mode 100644 platform_plugin_aspects/xblock_aside.py diff --git a/platform_plugin_aspects/apps.py b/platform_plugin_aspects/apps.py index 5f506ff..8c65218 100644 --- a/platform_plugin_aspects/apps.py +++ b/platform_plugin_aspects/apps.py @@ -75,3 +75,6 @@ def ready(self): from platform_plugin_aspects.extensions import ( # pylint: disable=unused-import, import-outside-toplevel filters, ) + from platform_plugin_aspects.xblock_aside import ( # pylint: disable=unused-import, import-outside-toplevel + AspectsAside + ) diff --git a/platform_plugin_aspects/signals.py b/platform_plugin_aspects/signals.py index fc630f8..6eda30b 100644 --- a/platform_plugin_aspects/signals.py +++ b/platform_plugin_aspects/signals.py @@ -17,6 +17,8 @@ ) from platform_plugin_aspects.utils import get_model +from opaque_keys import InvalidKeyError + try: from openedx.core.djangoapps.user_api.accounts.signals import USER_RETIRE_LMS_MISC except ImportError: @@ -235,7 +237,7 @@ def on_object_tag_deleted( # pylint: disable=unused-argument # pragma: no cove try: CourseOverview.objects.get(id=instance.object_id) dump_course_to_clickhouse.delay(instance.object_id) - except CourseOverview.DoesNotExist: + except (CourseOverview.DoesNotExist, InvalidKeyError): pass diff --git a/platform_plugin_aspects/static/html/example.html b/platform_plugin_aspects/static/html/example.html new file mode 100644 index 0000000..dff5f98 --- /dev/null +++ b/platform_plugin_aspects/static/html/example.html @@ -0,0 +1,3 @@ +

+Hola desde aside {{xblock_id}} +

\ No newline at end of file diff --git a/platform_plugin_aspects/xblock_aside.py b/platform_plugin_aspects/xblock_aside.py new file mode 100644 index 0000000..6b274cf --- /dev/null +++ b/platform_plugin_aspects/xblock_aside.py @@ -0,0 +1,82 @@ +# pyright: reportMissingImports=false + +"""Xblock aside enabling OpenAI driven summaries.""" + +import logging + +import pkg_resources +from django.template import Context, Template +from django.utils import translation +from web_fragments.fragment import Fragment +from common.djangoapps.edxmako.shortcuts import render_to_string +from xblock.core import XBlock, XBlockAside +from xblock.fields import Scope, String +from xblock.utils.resources import ResourceLoader + +logger = logging.getLogger(__name__) +loader = ResourceLoader(__name__) + +from platform_plugin_aspects.xblock import ResourceLoader + +@XBlock.needs("user") +@XBlock.needs("i18n") +class AspectsAside(XBlockAside): + """ + XBlock aside that injects a superset dashboard for instructors. + """ + + def _get_block(self): + """ + Get the block wrapped by this aside. + """ + from xmodule.modulestore.django import modulestore # pylint: disable=import-error, import-outside-toplevel + + return modulestore().get_item(self.scope_ids.usage_id.usage_key) + + @XBlockAside.aside_for("studio_view") + @XBlockAside.aside_for("author_view") + @XBlockAside.aside_for("student_view") + def student_view_aside(self, block, context): # pylint: disable=unused-argument + """ + Display the tag selector with specific categories and allowed values, + depending on the context. + """ + logger.info(f"Class name {block.__class__.__name__}") + if block.__class__.__name__.replace("WithMixins", "") in ['ProblemBlock']: + frag = Fragment() + context.update({ + "xblock_id": self.scope_ids.usage_id.usage_key + }) + frag.add_content(self.render_template("static/html/example.html", context)) + # frag.add_javascript_url(self._get_studio_resource_url('/js/xblock_asides/structured_tags.js')) + # frag.initialize_js('StructuredTagsInit') + return frag + return Fragment() + + @classmethod + def should_apply_to_block(cls, block): + """ + Override base XBlockAside implementation. + + Indicates whether this aside should apply to a given block type, course, and user. + """ + # logger.info(block.__dict__) + return True + + + def render_template(self, template_path, context=None) -> str: + """ + Render a template with the given context. + + The template is translatedaccording to the user's language. + + args: + template_path: The path to the template + context: The context to render in the template + + returns: + The rendered template + """ + return loader.render_django_template( + template_path, context, i18n_service=self.runtime.service(self, "i18n") + ) \ No newline at end of file diff --git a/setup.py b/setup.py index db62a07..9ea47c6 100755 --- a/setup.py +++ b/setup.py @@ -185,5 +185,8 @@ def is_requirement(line): "xblock.v1": [ "superset = platform_plugin_aspects.xblock:SupersetXBlock", ], + "xblock_asides.v1": [ + "aspects_aside = platform_plugin_aspects.xblock_aside:AspectsAside", + ], }, )