|
|
|
|
@ -1,9 +1,14 @@ |
|
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
from django.http import JsonResponse |
|
|
|
|
from django.shortcuts import get_object_or_404 |
|
|
|
|
from django.template import loader |
|
|
|
|
from django.utils.decorators import method_decorator |
|
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
|
|
|
from django.views.decorators.http import require_http_methods |
|
|
|
|
from django.views.generic import TemplateView, DetailView |
|
|
|
|
|
|
|
|
|
from apps.content.models import Contest, ContestWork |
|
|
|
|
from apps.course.models import ContestWorkComment |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch') |
|
|
|
|
@ -54,3 +59,52 @@ class ContestWorkView(DetailView): |
|
|
|
|
context['user_liked'] = self.object.likes.filter(user=self.request.user).exists() \ |
|
|
|
|
if self.request.user.is_authenticated else False |
|
|
|
|
return context |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required |
|
|
|
|
@csrf_exempt |
|
|
|
|
@require_http_methods(['POST']) |
|
|
|
|
def contest_work_comment(request, contest_work_id): |
|
|
|
|
try: |
|
|
|
|
contest_work = ContestWork.objects.get(id=contest_work_id) |
|
|
|
|
except ContestWork.DoesNotExist: |
|
|
|
|
return JsonResponse({ |
|
|
|
|
'success': False, |
|
|
|
|
'errors': ['Contest_work with id f{contest_work_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): |
|
|
|
|
contest_work_comment = ContestWorkComment.objects.create( |
|
|
|
|
author=request.user, |
|
|
|
|
content=comment, |
|
|
|
|
contest_work=contest_work, |
|
|
|
|
) |
|
|
|
|
else: |
|
|
|
|
try: |
|
|
|
|
_contest_work_comment = ContestWorkComment.objects.get(id=reply_to) |
|
|
|
|
except ContestWorkComment.DoesNotExist: |
|
|
|
|
return JsonResponse({ |
|
|
|
|
'success': False, |
|
|
|
|
'errors': ['LessonComment with id f{reply_to} not found'] |
|
|
|
|
}, status=400) |
|
|
|
|
else: |
|
|
|
|
contest_work_comment = ContestWorkComment.objects.create( |
|
|
|
|
author=request.user, |
|
|
|
|
content=comment, |
|
|
|
|
contest_work=contest_work, |
|
|
|
|
parent=_contest_work_comment, |
|
|
|
|
) |
|
|
|
|
ctx = {'node': contest_work_comment, 'user': request.user} |
|
|
|
|
html = loader.render_to_string('templates/blocks/comment.html', ctx) |
|
|
|
|
return JsonResponse({ |
|
|
|
|
'success': True, |
|
|
|
|
'comment': html, |
|
|
|
|
}) |
|
|
|
|
|