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

48 lines
1.7 KiB
Python
Raw Normal View History

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
from django.http import HttpResponse
from django.http import HttpResponseRedirect
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-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()
ctx['show'] = Show.objects.filter(abbr=abbreviation).first()
ctx['seasons'] = Season.objects.filter(show=ctx['show'])
2017-11-10 15:21:21 +00:00
ctx['episodes'] = Episode.objects.filter(show=ctx['show']).count()
return ctx
class EpisodeView(TemplateView):
template_name = "episode.html"
def get_context_data(self, abbreviation, season, episode, **kwargs):
ctx = super().get_context_data()
ctx['show'] = Show.objects.filter(abbr=abbreviation).first()
ctx['episode'] = Episode.objects.filter(show=ctx['show'],episode=episode).first()
if not ctx['episode']:
return ctx
# If you're smarter than me, maybe you can compress the next 8 lines into fewer ones by some django magic I'm not aware of
submissions = ctx['episode'].submissions.all()
for sbm in submissions:
sbm.positives = sbm.votes.filter(positive=True).count()
sbm.negatives = sbm.votes.count() - sbm.positives
# sorts by "score", TODO: less bullshit
ctx['submissions'] = reversed(sorted(submissions, key=lambda x: x.positives - x.negatives))
2017-11-10 13:55:39 +00:00
return ctx