Titivillus/create_quest/views.py

37 lines
982 B
Python

#!/usr/bin/env python3
"""
/create_quest app views.
"""
from django.contrib import messages
from django.shortcuts import redirect, render
from .forms import QuestForm, PostForm
from quest.models import Quest, Post, PageTitle
def index(request):
"""
The index page for creating new quests.
"""
if request.method == 'POST':
# TODO: clean the post body
quest = Quest(owner=request.user)
quest_form = QuestForm(request.POST, instance=quest)
post = Post(post_type='text', page_num=1)
post_form = PostForm(request.POST, instance=post)
if all((quest_form.is_valid(), post_form.is_valid())):
quest.save()
post.quest = quest
post.save()
page = PageTitle(
quest=quest,
page_num=1,
title="Page 1"
)
page.save()
return redirect('quest:quest', quest_id=quest.id)
else:
quest_form = QuestForm()
post_form = PostForm()
context = {'quest_form': quest_form, 'post_form': post_form}
return render(request, 'create_quest/index.html', context)