From f0be7241b51ae0ca935483d936915246afda5172 Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Wed, 31 Jan 2018 17:15:53 +0300 Subject: [PATCH] LIL-120. Add lesson comments view --- apps/course/views.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ project/urls.py | 2 ++ 2 files changed, 51 insertions(+) diff --git a/apps/course/views.py b/apps/course/views.py index 38d3f921..8f5c7d14 100644 --- a/apps/course/views.py +++ b/apps/course/views.py @@ -87,6 +87,55 @@ def coursecomment(request, course_id): }) +@login_required +@csrf_exempt +@require_http_methods(['POST']) +def lessoncomment(request, Lesson): + try: + lesson = Lesson.objects.get(id=lesson_id) + except Lesson.DoesNotExist: + return JsonResponse({ + 'success': False, + 'errors': ['Lesson with id f{lesson_id} not found'] + }, status=400) + else: + reply_to = request.POST.get('reply_id', 0) + comment = request.POST.get('comment', '') + if not comment: + return JsonResponse({ + 'success': False, + 'errors': ['Comment can not be empty'] + }, status=400) + + if not int(reply_to): + lessoncomment = LessonComment.objects.create( + author=request.user, + content=comment, + course=course, + ) + else: + try: + _lessoncomment = LessonComment.objects.get(id=reply_to) + except LessonComment.DoesNotExist: + return JsonResponse({ + 'success': False, + 'errors': ['CourseComment with id f{reply_to} not found'] + }, status=400) + else: + lessoncomment = LessonComment.objects.create( + author=request.user, + content=comment, + course=course, + parent=_lessoncomment, + ) + ctx = {'node': lessoncomment, 'user': request.user} + html = loader.render_to_string('course/blocks/comment.html', ctx) + return JsonResponse({ + 'success': True, + 'comment': html, + }) + + class CourseView(DetailView): model = Course context_object_name = 'course' diff --git a/project/urls.py b/project/urls.py index 701135a6..30854945 100644 --- a/project/urls.py +++ b/project/urls.py @@ -21,6 +21,7 @@ from django.conf import settings from apps.course.views import ( CoursesView, likes, coursecomment, CourseView, LessonView, SearchView, + lessoncomment, ) from apps.user.views import UserView @@ -32,6 +33,7 @@ urlpatterns = [ path('course//like', likes, name='likes'), path('course//comment', coursecomment, name='coursecomment'), path('lesson//', LessonView.as_view(), name='lesson'), + path('lesson//comment', lessoncomment, name='lessoncomment'), path('search/', SearchView.as_view(), name='search'), path('user//', UserView.as_view(), name='user'), path('', TemplateView.as_view(template_name="templates/lilcity/main.html"), name='index'),