#!/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, Page 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') post_form = PostForm(request.POST, instance=post) if all((quest_form.is_valid(), post_form.is_valid())): quest.save() page0 = Page( quest=quest, page_num=0, title="Homepage" ) page0.save() page1 = Page( quest=quest, page_num=1, title="Page 1" ) page1.save() post.quest = quest post.page = page1 post.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)