-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Course templates api #36021
Closed
Closed
Course templates api #36021
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
cms/djangoapps/contentstore/rest_api/v1/serializers/course_templates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from rest_framework import serializers | ||
|
||
class CourseMetadataSerializer(serializers.Serializer): | ||
course_id = serializers.CharField() | ||
title = serializers.CharField() | ||
description = serializers.CharField() | ||
thumbnail = serializers.URLField() | ||
active = serializers.BooleanField() | ||
|
||
class CourseSerializer(serializers.Serializer): | ||
courses_name = serializers.CharField() | ||
zip_url = serializers.URLField() | ||
metadata = CourseMetadataSerializer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
cms/djangoapps/contentstore/rest_api/v1/views/course_templates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework.exceptions import NotFound | ||
from opaque_keys.edx.keys import CourseKey # Import CourseKey if using edX | ||
from django.conf import settings | ||
import requests | ||
from openedx.core.lib.api.view_utils import ( | ||
DeveloperErrorViewMixin, | ||
view_auth_classes, | ||
) | ||
from rest_framework.request import Request | ||
from ..serializers import CourseSerializer, CourseMetadataSerializer | ||
|
||
|
||
# @view_auth_classes(is_authenticated=True) | ||
class CourseTemplatesListView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
API endpoint to fetch and return course data from a GitHub repository. | ||
|
||
This view dynamically fetches course data from a specified GitHub repository | ||
and returns it in a structured JSON format. It processes directories and files | ||
in the repository to extract course names, ZIP URLs, and metadata files. | ||
|
||
Example URL: | ||
/api/courses/<course_key_string>/ | ||
|
||
Query Parameters: | ||
course_key_string (str): The course key in the format `org+course+run`. | ||
|
||
Example Response: | ||
[ | ||
{ | ||
"courses_name": "AI Courses", | ||
"zip_url": "https://raw.githubusercontent.com/awais786/courses/main/edly/AI%20Courses/course._Rnm_t%20(1).tar.gz", | ||
"metadata": { | ||
"course_id": "course-v1:edX+DemoX+T2024", | ||
"title": "Introduction to Open edX", | ||
"description": "Learn the fundamentals of the Open edX platform, including how to create and manage courses.", | ||
"thumbnail": "https://discover.ilmx.org/wp-content/uploads/2024/01/Course-image-2.webp", | ||
"active": true | ||
} | ||
} | ||
] | ||
|
||
Raises: | ||
NotFound: If there is an error fetching data from the repository. | ||
|
||
""" | ||
def get(self, request: Request, course_id: str): | ||
""" | ||
Handle GET requests to fetch course data. | ||
|
||
Args: | ||
request: The HTTP request object. | ||
course_id (str): The course id. | ||
|
||
Returns: | ||
Response: A structured JSON response containing course data. | ||
|
||
""" | ||
try: | ||
# Extract organization from course key | ||
course_key = CourseKey.from_string(course_id) | ||
organization = course_key.org | ||
|
||
# GitHub repository details. It should come from settings. | ||
templates_repo_url = f"https://api.github.com/repos/awais786/courses/contents/{organization}" | ||
|
||
# Fetch data from GitHub | ||
data = fetch_contents(templates_repo_url) | ||
courses = [] | ||
for directory in data: | ||
course_data = {'courses_name': directory["name"]} | ||
contents = fetch_contents(directory["url"]) # Assume directory contains URL to course contents | ||
|
||
for item in contents: | ||
if item['name'].endswith('.tar.gz'): # Check if file is a ZIP file | ||
course_data['zip_url'] = item['download_url'] | ||
elif item['name'].endswith('.json'): # Check if file is a JSON metadata file | ||
course_data['metadata'] = fetch_contents(item['download_url']) | ||
|
||
courses.append(course_data) | ||
|
||
# Serialize and return the data | ||
serializer = CourseSerializer(courses, many=True) | ||
return Response(serializer.data) | ||
|
||
except Exception as err: | ||
raise NotFound(f"Error fetching course data: {str(err)}") | ||
|
||
|
||
def fetch_contents(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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>{{ organization }} Templates List</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 20px; | ||
} | ||
|
||
.courses-container { | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 16px; | ||
justify-content: center; | ||
} | ||
|
||
.course-card { | ||
border: 1px solid #ddd; | ||
border-radius: 8px; | ||
padding: 16px; | ||
max-width: 300px; | ||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); | ||
text-align: center; | ||
background-color: #fff; | ||
} | ||
|
||
.course-thumbnail img { | ||
max-width: 100%; | ||
height: auto; | ||
border-radius: 8px; | ||
} | ||
|
||
.course-info { | ||
margin-top: 12px; | ||
} | ||
|
||
.course-name { | ||
font-size: 1.2em; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.course-id { | ||
color: #555; | ||
font-size: 0.9em; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.course-title { | ||
font-size: 1.1em; | ||
font-weight: bold; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.course-description { | ||
font-size: 0.95em; | ||
color: #666; | ||
} | ||
|
||
.page-header { | ||
text-align: center; /* Center the text horizontally */ | ||
margin: 20px 0; /* Add some spacing above and below the header */ | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div class="page-header"> | ||
<h2>{{ organization }} Templates List</h2> | ||
</div> | ||
|
||
<div class="courses-container"> | ||
{% for course in courses %} | ||
<div class="course-card"> | ||
<div class="course-thumbnail"> | ||
<img src="{{ course.metadata.thumbnail }}" alt="{{ course.metadata.title }}" width="354" height="218" /> | ||
</div> | ||
<div class="course-info"> | ||
<h3 class="course-name">{{ course.courses_name }}</h3> | ||
<p class="course-id">Course ID: {{ course.metadata.course_id }}</p> | ||
<h4 class="course-title">{{ course.metadata.title }}</h4> | ||
<p class="course-description">{{ course.metadata.description }}</p> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will come from settings