diff --git a/apps/course/templates/course/blocks/comment.html b/apps/course/templates/course/blocks/comment.html new file mode 100644 index 00000000..cbb199f4 --- /dev/null +++ b/apps/course/templates/course/blocks/comment.html @@ -0,0 +1,25 @@ +{% load static %} + +
+ {% if node.author.photo %} +
+ +
+ {% else %} +
+ +
+ {% endif %} +
+
+
+ {{ node.author.get_full_name }} + {{ node.created_at_humanize }} +
+
{{ node.content }}
+
+
+ +
+
+
\ No newline at end of file diff --git a/apps/course/templates/course/blocks/comments.html b/apps/course/templates/course/blocks/comments.html index 55e7f615..c762ebc5 100644 --- a/apps/course/templates/course/blocks/comments.html +++ b/apps/course/templates/course/blocks/comments.html @@ -1,30 +1,5 @@ -{% load static %} {% load mptt_tags %} {% recursetree object.comments.all %} - -
- {% if node.author.photo %} -
- -
- {% else %} -
- -
- {% endif %} -
-
-
- {{ node.author.get_full_name }} - {{ node.created_at_humanize }} -
-
{{ node.content }}
-
-
- -
-
-
- +{% include './comment.html' %} {{ children }} {% endrecursetree %} \ No newline at end of file diff --git a/apps/course/views.py b/apps/course/views.py index 466f9b54..6521837f 100644 --- a/apps/course/views.py +++ b/apps/course/views.py @@ -1,10 +1,10 @@ from django.contrib.auth.decorators import login_required from django.http import JsonResponse -from django.template import loader -from django.views.generic import View, DetailView, ListView +from django.template import loader, Context, Template +from django.views.generic import View, CreateView, DetailView, ListView from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_http_methods -from .models import Course, Like, Lesson +from .models import Course, Like, Lesson, CourseComment, LessonComment from .filters import CourseFilter @@ -18,7 +18,7 @@ def likes(request, course_id): return JsonResponse({ 'success': False, 'errors': ['Course with id f{cource_id} not found'] - }) + }, status=400) else: course_user_likes = course.likes.filter(user=request.user) if course_user_likes.exists(): @@ -37,6 +37,55 @@ def likes(request, course_id): }) +@login_required +@csrf_exempt +@require_http_methods(['POST']) +def coursecomment(request, course_id): + try: + course = Course.objects.get(id=course_id) + except Course.DoesNotExist: + return JsonResponse({ + 'success': False, + 'errors': ['Course with id f{cource_id} not found'] + }, status=400) + else: + reply_to = request.POST.get('reply_id', None) + comment = request.POST.get('comment', '') + if not comment: + return JsonResponse({ + 'success': False, + 'errors': ['Comment can not be empty'] + }, status=400) + + if not reply_to: + coursecomment = CourseComment.objects.create( + author=request.user, + content=comment, + course=course, + ) + else: + try: + _coursecomment = CourseComment.objects.get(id=reply_to) + except CourseComment.DoesNotExist: + return JsonResponse({ + 'success': False, + 'errors': ['CourseComment with id f{reply_to} not found'] + }, status=400) + else: + coursecomment = CourseComment.objects.create( + author=request.user, + content=comment, + course=course, + parrent=_coursecomment, + ) + ctx = {'node': coursecomment} + 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 495632dd..0822252b 100644 --- a/project/urls.py +++ b/project/urls.py @@ -18,7 +18,7 @@ from django.urls import path, include from django.views.generic import TemplateView from django.conf import settings -from apps.course.views import CoursesView, likes, CourseView, LessonView +from apps.course.views import CoursesView, likes, coursecomment, CourseView, LessonView urlpatterns = [ path('admin/', admin.site.urls), @@ -26,6 +26,7 @@ urlpatterns = [ path('courses/', CoursesView.as_view(), name='courses'), path('course//', CourseView.as_view(), name='course'), path('course//like', likes, name='likes'), + path('course//comment', coursecomment, name='coursecomment'), path('lesson//', LessonView.as_view(), name='lesson'), path('', TemplateView.as_view(template_name="templates/lilcity/main.html"), name='index'), ]