phantasma/app/text/views.py
2023-05-17 18:42:16 +01:00

52 lines
1.6 KiB
Python

import random
from django.shortcuts import render, redirect, get_object_or_404
from django.conf import settings
from . import models
def fallback(request, slug=''):
if not slug:
return redirect('index')
context = {}
return render(request, 'fallback.html', context)
def index(request):
from ..text.models import Text
context = {}
context['texts'] = Text.objects.filter(public=True, listed=True).order_by('title')
return render(request, 'index.html', context)
def page(request, slug=''):
context = {}
if slug == "":
return index(request)
elif 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 text(request, slug)
def about(request):
context = {}
context['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'about.html', context)
def texts(request):
context = {}
all_texts = models.Text.objects.filter(public=True, listed=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['pandora_url'] = settings.DEFAULT_PANDORA_API.replace('/api/', '')
return render(request, 'text.html', context)