You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
5.2 KiB
148 lines
5.2 KiB
import os
|
|
|
|
from django.conf import settings
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import JsonResponse, HttpResponse
|
|
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, FAQ
|
|
from apps.course.models import ContestWorkComment
|
|
from apps.user.models import Child
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class ContestEditView(TemplateView):
|
|
template_name = 'content/contest_edit.html'
|
|
query_pk_and_slug = True
|
|
|
|
def get(self, request, slug=None, lesson=None):
|
|
if slug:
|
|
self.object = get_object_or_404(Contest, slug=slug)
|
|
else:
|
|
self.object = Contest()
|
|
|
|
return super().get(request)
|
|
|
|
def get_context_data(self):
|
|
context = super().get_context_data()
|
|
context['object'] = self.object
|
|
return context
|
|
|
|
|
|
class ContestView(DetailView):
|
|
model = Contest
|
|
context_object_name = 'contest'
|
|
template_name = 'content/contest.html'
|
|
query_pk_and_slug = True
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
context = super().get_context_data()
|
|
if self.request.user.is_authenticated:
|
|
context['contest_work_uploaded'] = ContestWork.objects.filter(user=self.request.user).exists()
|
|
return context
|
|
|
|
|
|
class ContestWorkView(DetailView):
|
|
model = ContestWork
|
|
context_object_name = 'contest_work'
|
|
template_name = 'content/contest_work.html'
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
context = super().get_context_data()
|
|
prev_contest_work = ContestWork.objects.filter(created_at__gt=self.object.created_at)[:1]
|
|
if prev_contest_work:
|
|
context['prev_contest_work'] = prev_contest_work[0]
|
|
next_contest_work = ContestWork.objects.filter(created_at__lt=self.object.created_at)[:1]
|
|
if next_contest_work:
|
|
context['next_contest_work'] = next_contest_work[0]
|
|
|
|
context['user_liked'] = self.object.likes.filter(user=self.request.user).exists() \
|
|
if self.request.user.is_authenticated else False
|
|
context['likes_count'] = self.object.likes.filter(user__is_active=True).count()
|
|
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,
|
|
})
|
|
|
|
|
|
class FAQView(TemplateView):
|
|
template_name = 'content/faq.html'
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**kwargs)
|
|
context['faqs'] = [{
|
|
'question': f.question,
|
|
'answer': f.answer,
|
|
'opened': 0,
|
|
} for f in FAQ.objects.all()]
|
|
return self.render_to_response(context)
|
|
|
|
from apps.notification.tasks import draw_cert
|
|
|
|
@csrf_exempt
|
|
def get_certificate(request, cert):
|
|
signed_path_pattern = 'signed-user-certificates/%d.jpg'
|
|
fn = signed_path_pattern % cert
|
|
path = os.path.join(settings.RESOURCES_ROOT, fn)
|
|
child_id = request.GET.get('child')
|
|
if child_id:
|
|
child = get_object_or_404(Child, pk=child_id)
|
|
path = draw_cert(path, child.user.email, child.first_name, child.last_name)
|
|
f = open(path, 'rb')
|
|
response = HttpResponse(f, content_type='image/jpeg')
|
|
response['Content-Disposition'] = 'attachment; filename=%s' % fn
|
|
f.close()
|
|
if child_id:
|
|
os.remove(path)
|
|
return response
|
|
|