Browse Source

admin debugs , lesson toggle fixed.

master
Mohsen Taba 5 months ago
parent
commit
813aee4c39
  1. 4
      apps/article/admin.py
  2. 2
      apps/course/urls.py
  3. 43
      apps/course/views/lesson.py

4
apps/article/admin.py

@ -183,10 +183,6 @@ class ArticleCategoryAdmin(ModelAdmin):
if form.base_fields.get('slug'): if form.base_fields.get('slug'):
form.base_fields['slug'].required = False form.base_fields['slug'].required = False
return form return form
@display(description=_("Category"), header=True)
def display_header(self, obj):
return list(obj.title)
class ArticleAdmin(ModelAdmin): class ArticleAdmin(ModelAdmin):

2
apps/course/urls.py

@ -9,7 +9,7 @@ urlpatterns = [
path('categories/', views.CourseCategoryAPIView.as_view(), name='course-categories'), path('categories/', views.CourseCategoryAPIView.as_view(), name='course-categories'),
path('', views.CourseListAPIView.as_view(), name='course-list'), path('', views.CourseListAPIView.as_view(), name='course-list'),
path('my-courses/', views.MyCourseListAPIView.as_view(), name='course-my-courses-list'), path('my-courses/', views.MyCourseListAPIView.as_view(), name='course-my-courses-list'),
path('lesson/completion/', views.LessonCompletionCreateAPIView.as_view(), name='lesson-completion'),
path('lesson/completion/', views.LessonCompletionToggleAPIView.as_view(), name='lesson-completion'),
path('professors/', views.ProfessorListAPIView.as_view(), name='course-professor-list'), path('professors/', views.ProfessorListAPIView.as_view(), name='course-professor-list'),
re_path(r'professors/(?P<slug>[\w-]+)/courses/$', views.ProfessorCourseListAPIView.as_view(), name='course-professor-course-list'), re_path(r'professors/(?P<slug>[\w-]+)/courses/$', views.ProfessorCourseListAPIView.as_view(), name='course-professor-course-list'),
re_path(r'professors/(?P<slug>[\w-]+)/$', views.ProfessorDetailAPIView.as_view(), name='course-professor-detail'), re_path(r'professors/(?P<slug>[\w-]+)/$', views.ProfessorDetailAPIView.as_view(), name='course-professor-detail'),

43
apps/course/views/lesson.py

@ -105,44 +105,55 @@ class LessonDetailView(RetrieveAPIView):
class LessonCompletionCreateAPIView(GenericAPIView):
class LessonCompletionToggleAPIView(GenericAPIView):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
@swagger_auto_schema( @swagger_auto_schema(
operation_description="Mark a lesson as completed",
operation_description="Toggle lesson completion status (Check/Uncheck)",
tags=["Imam-Javad - Course"], tags=["Imam-Javad - Course"],
request_body=openapi.Schema( request_body=openapi.Schema(
type=openapi.TYPE_OBJECT, type=openapi.TYPE_OBJECT,
required=['lesson_id'], required=['lesson_id'],
properties={ properties={
'lesson_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID of the lesson to be marked as completed'),
'lesson_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID of the lesson to toggle'),
}, },
), ),
responses={ responses={
201: 'Lesson completed successfully.',
200: 'Lesson already completed.',
201: 'Lesson marked as COMPLETED.',
200: 'Lesson marked as INCOMPLETE (Unchecked).',
400: 'Lesson ID is required.', 400: 'Lesson ID is required.',
404: 'Lesson not found.', 404: 'Lesson not found.',
} }
) )
def post(self, request): def post(self, request):
student = request.user # Assuming the user is the student
student = request.user
lesson_id = request.data.get('lesson_id') lesson_id = request.data.get('lesson_id')
if not lesson_id: if not lesson_id:
return Response({'error': 'Lesson ID is required.'}, status=status.HTTP_400_BAD_REQUEST) return Response({'error': 'Lesson ID is required.'}, status=status.HTTP_400_BAD_REQUEST)
try: try:
course_lesson = CourseLesson.objects.get(id=lesson_id) course_lesson = CourseLesson.objects.get(id=lesson_id)
except CourseLesson.DoesNotExist: except CourseLesson.DoesNotExist:
return Response({'error': 'Lesson not found.'}, status=status.HTTP_404_NOT_FOUND) return Response({'error': 'Lesson not found.'}, status=status.HTTP_404_NOT_FOUND)
# Check if the lesson is already completed by the student
if LessonCompletion.objects.filter(student=student, course_lesson=course_lesson).exists():
return Response({'message': 'Lesson already completed.'}, status=status.HTTP_200_OK)
# Create a new completion record
completion = LessonCompletion(student=student, course_lesson=course_lesson)
completion.save()
return Response({'message': 'Lesson completed successfully.'}, status=status.HTTP_201_CREATED)
# TOGGLE LOGIC
# Try to find an existing completion record
completion = LessonCompletion.objects.filter(student=student, course_lesson=course_lesson).first()
if completion:
# Scenario: The user clicked by mistake or wants to un-check
# Action: Delete the record
completion.delete()
return Response(
{'message': 'Lesson marked as incomplete.', 'is_completed': False},
status=status.HTTP_200_OK
)
else:
# Scenario: The lesson is not finished yet
# Action: Create the record
LessonCompletion.objects.create(student=student, course_lesson=course_lesson)
return Response(
{'message': 'Lesson completed successfully.', 'is_completed': True},
status=status.HTTP_201_CREATED
)
Loading…
Cancel
Save