2017-08-25 18:03:37 +00:00
|
|
|
from django.shortcuts import render
|
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
|
|
|
|
|
|
|
from LandingPage.models import Show
|
|
|
|
from LandingPage.models import Season
|
|
|
|
from LandingPage.models import Episode
|
2017-11-10 15:21:21 +00:00
|
|
|
from LandingPage.models import Submission
|
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
|
|
|
|
show = Show.objects.filter(abbr=abbreviation).annotate(episode_count=Count('episodes')).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().annotate(episode_count=Count('episodes'))
|
|
|
|
|
|
|
|
# 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
|
|
|
|
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")
|
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
|