#!/usr/bin/env python3 """ Quest and quest accessory views. """ from django.contrib import messages from django.http import HttpResponse from django.shortcuts import render, redirect from .models import Quest, DiceRoll, PollOption, PollVote, Page from .forms import EditQuestForm def index(request): """ /quest page index. Possibly not needed. """ return HttpResponse("Hello, world. You're at the quest index.") def quest(request, quest_id, page_num='1'): """ Arbituary quest page view. """ # TODO: 404 quest not found quest = Quest.objects.get(id=quest_id) 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) # 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) ip_address = request.META['REMOTE_ADDR'] context = locals() return render(request, 'quest/quest.html', context) 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': form = EditQuestForm(request.POST) if form.is_valid(): quest.anon_name = form.cleaned_data['anon_name'] quest.save() return redirect('quest:quest',quest_id=quest.id, page_num=page_num) else: messages.error(request, "Error") else: pass context = locals() return render(request, 'quest/edit_quest.html', context)