Titivillus/quest/views.py

81 lines
2.4 KiB
Python
Raw Normal View History

2018-08-10 08:39:51 -04:00
#!/usr/bin/env python3
"""
Quest and quest accessory views.
"""
2018-09-30 15:20:42 -04:00
from datetime import timedelta, datetime, timezone
2018-09-25 11:58:07 -04:00
from django.contrib import messages
2018-08-10 08:39:51 -04:00
from django.http import HttpResponse
2018-09-25 11:58:07 -04:00
from django.shortcuts import render, redirect
2018-08-10 08:39:51 -04:00
2018-09-25 11:58:07 -04:00
from .models import Quest, DiceRoll, PollOption, PollVote, Page
2018-09-24 08:22:01 -04:00
from .forms import EditQuestForm
2018-08-13 13:17:51 -04:00
2018-08-10 08:39:51 -04:00
def index(request):
"""
/quest page index. Possibly not needed.
"""
return HttpResponse("Hello, world. You're at the quest index.")
2018-09-25 11:58:07 -04:00
def quest(request, quest_id, page_num='1'):
2018-08-10 08:39:51 -04:00
"""
Arbituary quest page view.
"""
2018-09-25 11:58:07 -04:00
# TODO: 404 quest not found
2018-08-13 13:17:51 -04:00
quest = Quest.objects.get(id=quest_id)
2018-09-25 11:58:07 -04:00
pages = Page.objects.filter(
quest=quest, appendix=False).order_by('page_num')
appendices = Page.objects.filter(
quest=quest, appendix=True).order_by('title')
chat_messages = quest.message_set.all()
try:
page = Page.objects.get(quest=quest, page_num=page_num)
except Page.DoesNotExist:
messages.error(request, "Page not found, redirecting you.")
return redirect('quest:quest', quest_id=quest.id, page_num='1')
posts = quest.post_set.filter(page=page)
2018-09-24 13:46:46 -04:00
# TODO: filter by page_num as well
dice_rolls = DiceRoll.objects.filter(dicecall__post__quest=quest)
poll_options = PollOption.objects.filter(poll__post__quest=quest)
poll_votes = PollVote.objects.filter(option__poll__post__quest=quest)
2018-09-03 17:59:41 -04:00
ip_address = request.META['REMOTE_ADDR']
2018-08-24 21:51:03 -04:00
context = locals()
2018-08-13 13:17:51 -04:00
return render(request, 'quest/quest.html', context)
2018-09-05 12:48:24 -04:00
def edit_quest(request, quest_id, page_num=1):
"""
Edit quest page. Only available to the QM.
"""
quest = Quest.objects.get(id=quest_id)
if quest.owner != request.user:
return redirect('quest:quest', quest_id=quest_id, page_num=page_num)
if request.method == 'POST':
2018-09-24 08:22:01 -04:00
form = EditQuestForm(request.POST)
2018-09-21 14:18:54 -04:00
if form.is_valid():
quest.anon_name = form.cleaned_data['anon_name']
2018-09-30 15:20:42 -04:00
quest.live = form.cleaned_data['live']
live_date = form.cleaned_data['live_date']
live_time = form.cleaned_data['live_time']
if live_date and live_time:
live_datetime = datetime.combine(
live_date,
live_time,
timezone.utc
)
tz_delta = timedelta(minutes=form.cleaned_data['timezone'])
live_datetime = live_datetime + tz_delta
quest.live_time = live_datetime
else:
quest.live_time = None
2018-09-21 14:18:54 -04:00
quest.save()
return redirect('quest:quest',quest_id=quest.id, page_num=page_num)
else:
messages.error(request, "Error")
2018-09-05 12:48:24 -04:00
else:
pass
context = locals()
return render(request, 'quest/edit_quest.html', context)