2017-11-11 12:57:52 +00:00
|
|
|
from django.template import RequestContext
|
2017-12-15 11:33:01 +00:00
|
|
|
from django.shortcuts import render, get_list_or_404, get_object_or_404
|
2017-11-10 13:55:39 +00:00
|
|
|
from django.views import View
|
|
|
|
from django.views.generic.base import TemplateView
|
2017-11-13 17:42:10 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2017-11-10 13:55:39 +00:00
|
|
|
from django.conf import settings
|
2017-11-11 08:03:06 +00:00
|
|
|
from django.http import Http404
|
2017-11-10 13:55:39 +00:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.http import HttpResponseRedirect
|
2017-11-11 08:03:06 +00:00
|
|
|
from django.db.models import Case, When, Value, IntegerField, Count, F
|
2017-11-13 17:42:10 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2017-11-10 13:55:39 +00:00
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
from guardian.decorators import permission_required_or_403
|
|
|
|
|
|
|
|
from LandingPage.models import User, Show, Season, Episode, Submission, SubmissionVote
|
2017-08-25 18:03:37 +00:00
|
|
|
|
2017-11-11 12:57:52 +00:00
|
|
|
from . import forms
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
2017-08-25 18:03:37 +00:00
|
|
|
# Create your views here.
|
2017-11-10 13:55:39 +00:00
|
|
|
|
2017-11-11 08:03:06 +00:00
|
|
|
# Index page of a show
|
2017-11-10 15:21:21 +00:00
|
|
|
class IndexView(TemplateView):
|
2017-11-10 13:55:39 +00:00
|
|
|
template_name = "show.html"
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
def get_context_data(self, abbr, **kwargs):
|
2017-11-10 13:55:39 +00:00
|
|
|
ctx = super().get_context_data()
|
2017-11-11 08:03:06 +00:00
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
# Get show by abbr, add episode count to the show and return only the first object
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
2017-11-11 08:03:06 +00:00
|
|
|
|
|
|
|
# Get all seasons of the show and annotate episode counts onto them
|
2017-11-11 08:12:39 +00:00
|
|
|
seasons = show.seasons.all()
|
2017-11-11 08:03:06 +00:00
|
|
|
|
|
|
|
# Add fields to context
|
|
|
|
ctx['show'] = show
|
|
|
|
ctx['seasons'] = seasons
|
|
|
|
|
2017-11-10 15:21:21 +00:00
|
|
|
return ctx
|
|
|
|
|
2017-11-11 08:03:06 +00:00
|
|
|
# Episodes page of a show
|
2017-11-10 15:21:21 +00:00
|
|
|
class EpisodeView(TemplateView):
|
|
|
|
template_name = "episode.html"
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
def get_context_data(self, abbr, season, episode, **kwargs):
|
2017-11-10 15:21:21 +00:00
|
|
|
ctx = super().get_context_data()
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
# Get show by abbr
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
|
|
|
episode = get_object_or_404(Episode, show=show,season__number=season,episode=episode)
|
2017-11-10 15:21:21 +00:00
|
|
|
|
2017-11-11 13:33:27 +00:00
|
|
|
# I acknowledge that this is a mess. A functional mess. But a mess nonetheless. Hey, that rhymed!
|
2017-11-11 08:03:06 +00:00
|
|
|
submissions = episode.submissions.annotate(
|
|
|
|
positives=Count(
|
|
|
|
Case(
|
|
|
|
When(
|
|
|
|
votes__positive=True,
|
|
|
|
then=Value(1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
negatives=Count('votes') - F('positives'),
|
|
|
|
score=F('positives') - F('negatives')
|
|
|
|
).order_by('-score')
|
2017-11-10 15:21:21 +00:00
|
|
|
|
2017-11-11 08:03:06 +00:00
|
|
|
# Add fields to context
|
|
|
|
ctx['show'] = show
|
|
|
|
ctx['episode'] = episode
|
|
|
|
ctx['submissions'] = submissions
|
2017-11-10 15:21:21 +00:00
|
|
|
|
2017-11-10 13:55:39 +00:00
|
|
|
return ctx
|
2017-11-11 09:24:39 +00:00
|
|
|
|
2017-11-11 12:57:52 +00:00
|
|
|
# Submission form GET and POST
|
2017-11-13 17:42:10 +00:00
|
|
|
@login_required
|
2017-12-15 11:33:01 +00:00
|
|
|
def SubmissionForm(req, abbr, season, episode):
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
|
|
|
episode = get_object_or_404(Episode, show=show,season__number=season,episode=episode)
|
2017-11-13 17:42:10 +00:00
|
|
|
user = req.user
|
2017-11-11 12:57:52 +00:00
|
|
|
|
|
|
|
form = forms.SubmissionForm()
|
|
|
|
|
|
|
|
# Request context
|
|
|
|
ctx = {
|
|
|
|
'form': form,
|
|
|
|
'show': show,
|
|
|
|
'episode': episode
|
|
|
|
}
|
|
|
|
|
|
|
|
# Handle POST
|
|
|
|
if req.method == 'POST':
|
|
|
|
form = forms.SubmissionForm(req.POST)
|
|
|
|
ctx['form'] = form
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
|
|
|
|
# Check if the URL has already been submitted
|
|
|
|
if Submission.objects.filter(episode=episode,url=form_data['url']).count() > 0:
|
|
|
|
ctx['error'] = 'This URL has already been submitted!'
|
|
|
|
return render(req, "submit.html", ctx)
|
|
|
|
|
2017-12-26 17:34:24 +00:00
|
|
|
if not user.has_perm('LandingPage.change_show'):
|
2017-12-15 11:33:01 +00:00
|
|
|
# Check if there has been a submission by this user for this episode within the last 24 hours
|
|
|
|
if Submission.objects.filter(user=user,episode=episode,timestamp__gte=datetime.datetime.now() - datetime.timedelta(hours=24)).count() > 0:
|
|
|
|
ctx['error'] = 'You can only submit one link for an episode in 24 hours!'
|
|
|
|
return render(req, "submit.html", ctx)
|
2017-11-11 12:57:52 +00:00
|
|
|
|
|
|
|
new_submission = form.save(commit=False)
|
|
|
|
new_submission.user = user
|
|
|
|
new_submission.episode = episode
|
|
|
|
new_submission.save()
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbr, episode.season.number, episode.episode))
|
2017-11-11 12:57:52 +00:00
|
|
|
else:
|
|
|
|
ctx['error'] = 'Invalid fields!'
|
|
|
|
|
|
|
|
return render(req, "submit.html", ctx)
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
# Edit a submission - for moderators
|
2017-12-26 17:34:24 +00:00
|
|
|
@permission_required_or_403('LandingPage.change_show', (Show, 'abbr', 'abbr'), accept_global_perms=True)
|
2017-12-15 11:33:01 +00:00
|
|
|
def SubmissionModForm(req, abbr, submission):
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
|
|
|
submission = get_object_or_404(Submission, pk=submission)
|
|
|
|
episode = submission.episode
|
|
|
|
user = req.user
|
|
|
|
|
|
|
|
form = forms.SubmissionForm(instance=submission)
|
|
|
|
|
|
|
|
# Request context
|
|
|
|
ctx = {
|
|
|
|
'form': form,
|
|
|
|
'show': show,
|
|
|
|
'episode': episode
|
|
|
|
}
|
|
|
|
|
|
|
|
# Handle POST
|
|
|
|
if req.method == 'POST':
|
|
|
|
|
|
|
|
if 'delete' in req.POST:
|
|
|
|
submission.delete()
|
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbr, episode.season.number, episode.episode))
|
|
|
|
|
|
|
|
if 'delete_ban' in req.POST:
|
|
|
|
submission.delete()
|
|
|
|
return HttpResponseRedirect('/ban?user=%d'%(submission.user.pk))
|
|
|
|
|
|
|
|
form = forms.SubmissionForm(req.POST, instance=submission)
|
|
|
|
ctx['form'] = form
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbr, episode.season.number, episode.episode))
|
|
|
|
else:
|
|
|
|
ctx['error'] = 'Invalid fields!'
|
|
|
|
|
|
|
|
return render(req, "submit_mod.html", ctx)
|
|
|
|
|
|
|
|
# Season form GET and POST
|
2017-12-26 17:34:24 +00:00
|
|
|
@permission_required_or_403('LandingPage.change_show', (Show, 'abbr', 'abbr'), accept_global_perms=True)
|
2017-12-15 11:33:01 +00:00
|
|
|
def SeasonSubmitForm(req, abbr):
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
|
|
|
user = req.user
|
|
|
|
|
|
|
|
form = forms.SeasonForm()
|
|
|
|
|
|
|
|
# Request context
|
|
|
|
ctx = {
|
|
|
|
'form': form,
|
|
|
|
'show': show
|
|
|
|
}
|
|
|
|
|
|
|
|
# Handle POST
|
|
|
|
if req.method == 'POST':
|
|
|
|
form = forms.SeasonForm(req.POST)
|
|
|
|
ctx['form'] = form
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
|
|
|
|
# Check if the URL has already been submitted
|
|
|
|
if Season.objects.filter(show=show,number=form_data['number']).count() > 0:
|
|
|
|
ctx['error'] = 'This season has already been submitted!'
|
|
|
|
return render(req, "season_add.html", ctx)
|
|
|
|
|
|
|
|
new_season = form.save(commit=False)
|
|
|
|
new_season.show = show
|
|
|
|
new_season.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/show/%s'%(abbr))
|
|
|
|
else:
|
|
|
|
ctx['error'] = 'Invalid fields!'
|
|
|
|
|
|
|
|
return render(req, "season_add.html", ctx)
|
|
|
|
|
|
|
|
# Episode form GET and POST
|
2017-12-26 17:34:24 +00:00
|
|
|
@permission_required_or_403('LandingPage.change_show', (Show, 'abbr', 'abbr'), accept_global_perms=True)
|
2017-12-15 11:33:01 +00:00
|
|
|
def EpisodeSubmitForm(req, abbr, season):
|
|
|
|
show = get_object_or_404(Show, abbr=abbr)
|
|
|
|
season = get_object_or_404(Season, show=show,number=season)
|
|
|
|
user = req.user
|
|
|
|
|
|
|
|
form = forms.EpisodeForm()
|
|
|
|
|
|
|
|
# Request context
|
|
|
|
ctx = {
|
|
|
|
'form': form,
|
|
|
|
'season': season,
|
|
|
|
'show': show
|
|
|
|
}
|
|
|
|
|
|
|
|
# Handle POST
|
|
|
|
if req.method == 'POST':
|
|
|
|
form = forms.EpisodeForm(req.POST)
|
|
|
|
ctx['form'] = form
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
form_data = form.cleaned_data
|
|
|
|
|
|
|
|
# Check if the URL has already been submitted
|
|
|
|
if Episode.objects.filter(show=show,episode=form_data['episode'],season=season).count() > 0:
|
|
|
|
ctx['error'] = 'This episode has already been submitted!'
|
|
|
|
return render(req, "episode_add.html", ctx)
|
|
|
|
|
|
|
|
new_episode = form.save(commit=False)
|
|
|
|
new_episode.show = show
|
|
|
|
new_episode.season = season
|
|
|
|
new_episode.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/show/%s'%(abbr))
|
|
|
|
else:
|
|
|
|
ctx['error'] = 'Invalid fields!'
|
|
|
|
|
|
|
|
return render(req, "episode_add.html", ctx)
|
|
|
|
|
2017-11-11 09:24:39 +00:00
|
|
|
# Vote request
|
|
|
|
# /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}}
|
2017-11-13 17:42:10 +00:00
|
|
|
class SubmissionVoteSubmit(LoginRequiredMixin, View):
|
2017-12-15 11:33:01 +00:00
|
|
|
def post (self, req, abbr, subid, positive):
|
2017-11-11 09:26:26 +00:00
|
|
|
# Convert positive parameter into a boolean
|
|
|
|
pos_bool = int(positive) == 1
|
|
|
|
|
2017-11-13 17:42:10 +00:00
|
|
|
user = req.user
|
2017-11-11 09:24:39 +00:00
|
|
|
|
|
|
|
# Get the submission from the database
|
2017-12-15 11:33:01 +00:00
|
|
|
submission = get_object_or_404(Submission, id=subid)
|
2017-11-11 09:24:39 +00:00
|
|
|
|
|
|
|
# Prevent voting for own submission
|
|
|
|
if submission.user == user:
|
2017-11-11 10:41:41 +00:00
|
|
|
return HttpResponse('<h1>Error</h1><p>You cannot vote for your own submission.</p>', status=400)
|
2017-11-11 09:24:39 +00:00
|
|
|
|
|
|
|
# Allow changing a vote from positive to negative or vice-versa. Delete vote if its a re-vote
|
|
|
|
vote = submission.votes.filter(user=user,submission__id=submission.id).first()
|
|
|
|
if vote:
|
2017-11-11 09:26:26 +00:00
|
|
|
if not vote.positive == pos_bool:
|
|
|
|
vote.positive = pos_bool
|
2017-11-11 09:24:39 +00:00
|
|
|
vote.save()
|
|
|
|
else:
|
|
|
|
vote.delete()
|
|
|
|
else:
|
|
|
|
new_vote = SubmissionVote(
|
|
|
|
user=user,
|
|
|
|
submission=submission,
|
2017-11-11 09:26:26 +00:00
|
|
|
positive=pos_bool
|
2017-11-11 09:24:39 +00:00
|
|
|
)
|
|
|
|
new_vote.save()
|
|
|
|
|
2017-12-15 11:33:01 +00:00
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbr, submission.episode.season.number, submission.episode.episode))
|
2017-11-11 09:24:39 +00:00
|
|
|
|