njpma/app/text/views.py
2021-11-22 13:59:43 +01:00

62 lines
2 KiB
Python

import random
from django.shortcuts import render, redirect, get_object_or_404
from django.conf import settings
from . import models
from ..video.views import get_stream_prefix
def fallback(request, slug=''):
if not slug:
return redirect('index')
context = {}
return render(request, 'fallback.html', context)
def index(request):
from ..video.models import Film
from ..text.models import Text
context = {}
context['films'] = Film.objects.filter(public=True).count()
context['texts'] = Text.objects.filter(public=True).count()
featured = Film.objects.filter(featured=True)
featured_count = featured.count()
if featured_count:
featured = featured[random.randint(0, featured_count - 1)]
try:
featured = featured.data['items'][0]['id']
except:
featured = None
context['featured'] = featured
return render(request, 'index.html', context)
def page(request, slug=''):
context = {}
if request.user.is_staff:
page = models.Page.objects.filter(slug=slug).first()
else:
page = models.Page.objects.filter(slug=slug, public=True).first()
if page:
context['page'] = page
return render(request, 'page.html', context)
else:
return render(request, 'fallback.html', context)
def about(request):
context = {}
return render(request, 'about.html', context)
def texts(request):
context = {}
all_texts = models.Text.objects.filter(public=True).order_by('position', 'created')
context['texts'] = all_texts.filter()
return render(request, 'texts.html', context)
def text(request, slug):
context = {}
if request.user.is_staff:
context['text'] = get_object_or_404(models.Text, slug=slug)
else:
context['text'] = get_object_or_404(models.Text, slug=slug, public=True)
context['stream_prefix'] = get_stream_prefix(request)
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'text.html', context)