This repository has been archived on 2022-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
Episodes.Community/Show/views.py

126 lines
4.1 KiB
Python

from django.shortcuts import render
from django.shortcuts import render
from django.views import View
from django.views.generic.base import TemplateView
from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.db.models import Case, When, Value, IntegerField, Count, F
from LandingPage.models import User
from LandingPage.models import Show
from LandingPage.models import Season
from LandingPage.models import Episode
from LandingPage.models import Submission, SubmissionVote
# Create your views here.
# Index page of a show
class IndexView(TemplateView):
template_name = "show.html"
def get_context_data(self, abbreviation, **kwargs):
ctx = super().get_context_data()
# Get show by abbreviation, add episode count to the show and return only the first object
show = Show.objects.filter(abbr=abbreviation).first()
# 404
if not show:
raise Http404("Show does not exist")
# Get all seasons of the show and annotate episode counts onto them
seasons = show.seasons.all()
# Add fields to context
ctx['show'] = show
ctx['seasons'] = seasons
return ctx
# Episodes page of a show
class EpisodeView(TemplateView):
template_name = "episode.html"
def get_context_data(self, abbreviation, season, episode, **kwargs):
ctx = super().get_context_data()
# Get show by abbreviation
show = Show.objects.filter(abbr=abbreviation).first()
# Get episode by season and episode number
episode = Episode.objects.filter(show=show,episode=episode).first()
# 404's
if not show:
raise Http404("Show does not exist")
if not episode:
raise Http404("Episode does not exist")
# 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')
# Add fields to context
ctx['show'] = show
ctx['episode'] = episode
ctx['submissions'] = submissions
return ctx
# Vote request
# /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}}
class SubmissionVoteSubmit(View):
def get (self, req, abbreviation, subid, positive):
# Check for login status
if not req.session['user_id']:
r = HttpResponse('<h1>Error</h1><p>You need to be logged in to vote. Please <a href=/login>log in</a></p>')
r.status = 400
return r
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:
r = HttpResponse('<h1>Error</h1><p>You cannot vote for your own submission.</p>')
r.status = 400
return r
# 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:
if not vote.positive == (int(positive) == 1):
vote.positive = int(positive) == 1
vote.save()
else:
vote.delete()
else:
new_vote = SubmissionVote(
user=user,
submission=submission,
positive=int(positive) == 1
)
new_vote.save()
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbreviation, submission.episode.season.number, submission.episode.episode))