From 9d7edd63b219a9abc22e720c49f150de068e505d Mon Sep 17 00:00:00 2001 From: raiot Date: Tue, 19 Mar 2024 20:21:58 +0800 Subject: [PATCH] sync --- apps/courses/auto_importer.py | 12 ++++++++++++ apps/courses/views.py | 2 ++ 2 files changed, 14 insertions(+) diff --git a/apps/courses/auto_importer.py b/apps/courses/auto_importer.py index 6dd489f..a83c4ef 100644 --- a/apps/courses/auto_importer.py +++ b/apps/courses/auto_importer.py @@ -35,6 +35,17 @@ def create_lesson_cover(media_path): def add_lesson(course): + """ + This function is used to add lessons to a given course. It searches for media files in the course directory and + creates a new lesson for each media file found. If a lesson with the same media file already exists in the database, + it skips the creation of that lesson. + + Parameters: + course (Course): The course object to which the lessons will be added. + + Returns: + None + """ file_types = ('*.mp4', '*.avi', '*.mkv', '*.flv') media_files = [] for ext in file_types: @@ -61,6 +72,7 @@ def get_course_media(): # compare the existing courses with the content, if the course is not in the database, add it for course_path in content: if course_path not in existing_courses_path: + course_title = Path(course_path).name Course.objects.create(title=course_title, description=course_title, instructor=course_title, course_path=course_path) diff --git a/apps/courses/views.py b/apps/courses/views.py index 1919019..4d56d7d 100644 --- a/apps/courses/views.py +++ b/apps/courses/views.py @@ -17,6 +17,8 @@ class IndexView(View): class CourseDetailView(View): def get(self, request, pk): course = Course.objects.get(pk=pk) + from .auto_importer import add_lesson + add_lesson(course) context = {'lesson_list': course.lesson_set.all(), 'course': course} return render(request, 'courses/course_detail.html', context)