Add comments handler

remotes/origin/hasaccess
Ivlev Denis 8 years ago
parent 5e7057252d
commit e7962bb00d
  1. 57
      apps/course/views.py
  2. 3
      project/urls.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'

@ -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/<int:pk>/', CourseView.as_view(), name='course'),
path('course/<int:course_id>/like', likes, name='likes'),
path('course/<int:course_id>/comment', coursecomment, name='coursecomment'),
path('lesson/<int:pk>/', LessonView.as_view(), name='lesson'),
path('', TemplateView.as_view(template_name="templates/lilcity/main.html"), name='index'),
]

Loading…
Cancel
Save