From dc76fe24cf420510d080f26a47e41995aa874d7a Mon Sep 17 00:00:00 2001 From: awais qureshi Date: Thu, 19 Dec 2024 14:18:16 +0500 Subject: [PATCH] comment --- .idea/.gitignore | 3 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/modules.xml | 8 +++ .idea/openedx-filters.iml | 12 +++++ .idea/vcs.xml | 6 +++ openedx_filters/learning/filters.py | 50 +++++++++++++++++++ .../learning/tests/test_filters.py | 18 ++++++- 7 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/openedx-filters.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..47ede84 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/openedx-filters.iml b/.idea/openedx-filters.iml new file mode 100644 index 0000000..8b8c395 --- /dev/null +++ b/.idea/openedx-filters.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/openedx_filters/learning/filters.py b/openedx_filters/learning/filters.py index 9b4b79c..4d2b58c 100644 --- a/openedx_filters/learning/filters.py +++ b/openedx_filters/learning/filters.py @@ -850,3 +850,53 @@ def run_filter(cls, schedules: QuerySet) -> QuerySet: """ data = super().run_pipeline(schedules=schedules) return data.get("schedules") + + +import requests + +class CourseTemplateRequested(OpenEdxPublicFilter): + """ + Custom class for fetching templates from dynamic sources. + """ + filter_type = "org.openedx.templates.fetch.requested.v1" + + @classmethod + def run_filter(cls, source_type, source_config, organization): + """ + Fetch templates from a specified source. + + Arguments: + source_type (str): The type of source ('github' or 's3'). + source_config (dict): Configuration for the source (e.g., URL for GitHub, bucket/key for S3). + + Returns: + dict: Templates fetched from the source. + + Raises: + TemplateFetchException: If fetching templates fails. + """ + data = super().run_pipeline(source_type=source_type, source_config=source_config, organization=organization ) + # Extract the updated values after pipeline execution + source_type = data.get("source_type") + source_config = data.get("source_config") + organization = data.get("organization") + # Route to specific fetching logic based on the source type + if source_type == "github": + data = cls.fetch_from_github(organization, source_config) + else: + raise cls.TemplateFetchException(f"Unknown source type: {source_type}") + + + @classmethod + def fetch_from_github(cls, organization, source_type, source_url): + headers = { + "Authorization": f"token {settings.GITHUB_TOKEN_COURSE_TEMPLATES}", + "Accept": "application/vnd.github.v3+json", + } + try: + response = requests.get(url, headers=headers) + response.raise_for_status() # Raise error for 4xx/5xx responses + return response.json() + except Exception as err: + return JsonResponseBadRequest({"error": err.message}) + diff --git a/openedx_filters/learning/tests/test_filters.py b/openedx_filters/learning/tests/test_filters.py index a84e31d..e3f2818 100644 --- a/openedx_filters/learning/tests/test_filters.py +++ b/openedx_filters/learning/tests/test_filters.py @@ -30,8 +30,10 @@ StudentRegistrationRequested, VerticalBlockChildRenderStarted, VerticalBlockRenderCompleted, + CourseTemplateRequested ) +import requests @ddt class TestCertificateFilters(TestCase): @@ -106,7 +108,6 @@ def test_halt_certificate_process(self, CertificateException, attributes): """ exception = CertificateException(message="You can't generate certificate", **attributes) - self.assertDictContainsSubset(attributes, exception.__dict__) @ddt @@ -800,3 +801,18 @@ def test_schedule_requested(self): result = ScheduleQuerySetRequested.run_filter(schedules) self.assertEqual(schedules, result) + + +@ddt +class TestTemplateFetchRequested(TestCase): + """ + Test class to verify the behavior of the TemplateFetchRequested filter. + """ + def test_template_fetch_requested(self): + + breakpoint() + source_type = "github" + source_config = "https://github.com/awais786/courses" + organization = 'edly' + result = CourseTemplateRequested.run_filter(organization, source_type, source_config) + self.assertEqual(result, expected_result)