2017-11-11 12:57:52 +00:00
|
|
|
from django.template import RequestContext
|
2017-11-10 13:55:39 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
|
|
|
from django.views.generic.base import TemplateView
|
|
|
|
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-10 13:55:39 +00:00
|
|
|
|
2017-11-11 09:24:39 +00:00
|
|
|
from LandingPage.models import User
|
2017-11-10 13:55:39 +00:00
|
|
|
from LandingPage.models import Show
|
|
|
|
from LandingPage.models import Season
|
|
|
|
from LandingPage.models import Episode
|
2017-11-11 09:24:39 +00:00
|
|
|
from LandingPage.models import 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"
|
|
|
|
|
|
|
|
def get_context_data(self, abbreviation, **kwargs):
|
|
|
|
ctx = super().get_context_data()
|
2017-11-11 08:03:06 +00:00
|
|
|
|
|
|
|
# Get show by abbreviation, add episode count to the show and return only the first object
|
2017-11-11 08:12:39 +00:00
|
|
|
show = Show.objects.filter(abbr=abbreviation).first()
|
2017-11-11 08:03:06 +00:00
|
|
|
|
|
|
|
# 404
|
|
|
|
if not show:
|
|
|
|
raise Http404("Show does not exist")
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
|
|
|
def get_context_data(self, abbreviation, season, episode, **kwargs):
|
|
|
|
ctx = super().get_context_data()
|
|
|
|
|
2017-11-11 08:03:06 +00:00
|
|
|
# Get show by abbreviation
|
|
|
|
show = Show.objects.filter(abbr=abbreviation).first()
|
|
|
|
|
|
|
|
# Get episode by season and episode number
|
2017-11-11 12:57:52 +00:00
|
|
|
episode = Episode.objects.filter(show=show,season__number=season,episode=episode).first()
|
2017-11-11 08:03:06 +00:00
|
|
|
|
|
|
|
# 404's
|
|
|
|
if not show:
|
|
|
|
raise Http404("Show does not exist")
|
|
|
|
|
|
|
|
if not episode:
|
|
|
|
raise Http404("Episode does not exist")
|
2017-11-10 15:21:21 +00:00
|
|
|
|
2017-11-11 08:03:06 +00:00
|
|
|
# I aknowledge that this is a mess. A functional mess. But a mess nonetheless. Hey, that rhymed!
|
|
|
|
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
|
|
|
|
def SubmissionForm(req, abbreviation, season, episode):
|
|
|
|
show = Show.objects.get(abbr=abbreviation)
|
|
|
|
episode = Episode.objects.filter(show=show,season__number=season,episode=episode).first()
|
|
|
|
|
|
|
|
# Check for login status
|
|
|
|
if not 'user_id' in req.session:
|
|
|
|
return HttpResponse('<h1>Error</h1><p>You need to be logged in to submit. Please <a href=/login>log in</a></p>', status=400)
|
|
|
|
|
|
|
|
user = User.objects.get(user_id=req.session['user_id'])
|
|
|
|
|
|
|
|
# 404's
|
|
|
|
if not show:
|
|
|
|
raise Http404("Show does not exist")
|
|
|
|
|
|
|
|
if not episode:
|
|
|
|
raise Http404("Episode does not exist")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# 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.date.today() - 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)
|
|
|
|
|
|
|
|
# Have to do this because you can't add fields to a form
|
|
|
|
# If you know a better way of doing this, be my guest
|
|
|
|
new_submission = form.save(commit=False)
|
|
|
|
new_submission.user = user
|
|
|
|
new_submission.episode = episode
|
|
|
|
new_submission.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbreviation, episode.season.number, episode.episode))
|
|
|
|
else:
|
|
|
|
ctx['error'] = 'Invalid fields!'
|
|
|
|
|
|
|
|
return render(req, "submit.html", ctx)
|
|
|
|
|
2017-11-11 09:24:39 +00:00
|
|
|
# Vote request
|
|
|
|
# /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}}
|
|
|
|
class SubmissionVoteSubmit(View):
|
|
|
|
def get (self, req, abbreviation, subid, positive):
|
2017-11-11 09:26:26 +00:00
|
|
|
# Convert positive parameter into a boolean
|
|
|
|
pos_bool = int(positive) == 1
|
|
|
|
|
2017-11-11 09:24:39 +00:00
|
|
|
# Check for login status
|
2017-11-11 10:41:41 +00:00
|
|
|
if not 'user_id' in req.session:
|
|
|
|
return HttpResponse('<h1>Error</h1><p>You need to be logged in to vote. Please <a href=/login>log in</a></p>', status=400)
|
2017-11-11 09:24:39 +00:00
|
|
|
|
|
|
|
user = User.objects.get(user_id=req.session['user_id'])
|
|
|
|
|
|
|
|
# Get the submission from the database
|
|
|
|
submission = Submission.objects.filter(id=subid).first()
|
|
|
|
|
|
|
|
# 404
|
|
|
|
if not submission:
|
|
|
|
raise Http404("Submission does not exist")
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbreviation, submission.episode.season.number, submission.episode.episode))
|
|
|
|
|