/create_quest works, added quest models

This commit is contained in:
iou1name 2018-08-13 07:02:14 -04:00
parent aeaaa1df2b
commit 1f0e122646
7 changed files with 181 additions and 6 deletions

24
create_quest/forms.py Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
"""
Form(s) for the create_quest page.
"""
from django import forms
from quest.models import Quest, Post, PageTitle
class QuestForm(forms.ModelForm):
"""
The main create_quest form.
"""
class Meta:
model = Quest
fields = ('title',)
class PostForm(forms.ModelForm):
"""
The form for beginning the first post of the quest.
"""
class Meta:
model = Post
fields = ('post_text',)

View File

@ -2,9 +2,14 @@
{% block title %}Start a new quest{% endblock %}
{% block content %}
<h1>New Quest</h1>
<form method="post" action="{# url_for('.create_quest') #}">
<form method="post" action="{{ url('create_quest:index') }}">
{{ csrf_input }}
{#
<input type="text" placeholder="Quest Title" name="quest_title" maxlength="300" required/><br/>
<textarea id="create_textarea" name="quest_body"></textarea>
#}
{{ quest_form.as_p() }}
{{ post_form.as_p() }}
<input type="submit" name="submit" value="Submit"/>
</form>
{% endblock %}

View File

@ -2,13 +2,28 @@
"""
/create_quest app views.
"""
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import redirect, render
from .forms import QuestForm, PostForm
from quest.models import Quest, Post
@login_required(login_url='/login/')
def index(request):
"""
The index page for creating new quests.
"""
context = {}
if request.method == 'POST':
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()
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)

View File

@ -0,0 +1,54 @@
# Generated by Django 2.1 on 2018-08-12 20:09
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='PageTitle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('page_num', models.IntegerField()),
('title', models.CharField(max_length=200)),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('page_num', models.IntegerField(default=1)),
('post_type', models.CharField(choices=[('text', 'Text'), ('dice', 'Dice'), ('poll', 'Poll')], max_length=4)),
('post_text', models.TextField()),
('timestamp', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='Quest',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quest_name', models.CharField(max_length=200)),
('open_post_id', models.IntegerField(null=True)),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='post',
name='quest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Quest'),
),
migrations.AddField(
model_name='pagetitle',
name='quest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Quest'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1 on 2018-08-12 20:12
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('quest', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='quest',
old_name='quest_name',
new_name='title',
),
]

View File

@ -1,3 +1,42 @@
#!/usr/bin/env python3
"""
The main quest models.
"""
from django.db import models
from django.conf import settings
# Create your models here.
class Quest(models.Model):
"""
The meta quest object. Contains general information about the quest.
"""
title = models.CharField(max_length=200)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
open_post_id = models.IntegerField(null=True)
class Post(models.Model):
"""
An object for arbituary posts. Contains all post data, type, etc.
"""
quest = models.ForeignKey(Quest, on_delete=models.CASCADE)
page_num = models.IntegerField(default=1)
POST_TYPES = (
('text', 'Text'),
('dice', 'Dice'),
('poll', 'Poll'))
post_type = models.CharField(max_length=4, choices=POST_TYPES)
post_text = models.TextField()
timestamp = models.DateTimeField(auto_now=True)
class PageTitle(models.Model):
"""
Represents the title of a quest page.
"""
quest = models.ForeignKey(Quest, on_delete=models.CASCADE)
page_num = models.IntegerField()
title = models.CharField(max_length=200)
def __str__(self):
return self.title

View File

@ -0,0 +1,20 @@
# Generated by Django 2.1 on 2018-08-12 20:09
import django.core.validators
from django.db import migrations, models
import users.validators
class Migration(migrations.Migration):
dependencies = [
('users', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Must be between 3 and 20 characters. Letters and digits only.', max_length=20, unique=True, validators=[users.validators.CharValidator('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'Username must only contain alphanumeric characters.'), django.core.validators.MinLengthValidator(3, 'Username must contain more than 3 characters.'), django.core.validators.MaxLengthValidator(20, 'Username must contain less than 20 characters.')], verbose_name='username'),
),
]