Titivillus/create_quest/views.py

38 lines
980 B
Python
Raw Normal View History

2018-08-10 08:39:51 -04:00
#!/usr/bin/env python3
"""
/create_quest app views.
"""
from django.contrib import messages
from django.shortcuts import redirect, render
from .forms import QuestForm, PostForm
2018-09-25 11:58:07 -04:00
from quest.models import Quest, Post, Page
2018-08-10 08:39:51 -04:00
def index(request):
"""
The index page for creating new quests.
"""
if request.method == 'POST':
2018-09-05 14:13:47 -04:00
# TODO: clean the post body
quest = Quest(owner=request.user)
quest_form = QuestForm(request.POST, instance=quest)
2018-09-25 11:58:07 -04:00
post = Post(post_type='text')
post_form = PostForm(request.POST, instance=post)
if all((quest_form.is_valid(), post_form.is_valid())):
quest.save()
2018-09-25 11:58:07 -04:00
page = Page(
quest=quest,
page_num=1,
title="Page 1"
)
page.save()
2018-09-25 11:58:07 -04:00
post.quest = quest
post.page = page
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}
2018-08-10 08:39:51 -04:00
return render(request, 'create_quest/index.html', context)