Skip to content

Commit

Permalink
comment
Browse files Browse the repository at this point in the history
  • Loading branch information
awais786 committed Dec 19, 2024
1 parent d9d44ff commit dc76fe2
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/openedx-filters.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions openedx_filters/learning/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

18 changes: 17 additions & 1 deletion openedx_filters/learning/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
StudentRegistrationRequested,
VerticalBlockChildRenderStarted,
VerticalBlockRenderCompleted,
CourseTemplateRequested
)

import requests

@ddt
class TestCertificateFilters(TestCase):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit dc76fe2

Please sign in to comment.