from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404 from django.utils.decorators import method_decorator from django.views.generic import TemplateView, DetailView from apps.content.models import Contest, ContestWork @method_decorator(login_required, name='dispatch') class ContestEditView(TemplateView): template_name = 'content/contest_edit.html' def get(self, request, pk=None, lesson=None): if pk: self.object = get_object_or_404(Contest, pk=pk) 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 return context