added appendices, refactored pages

This commit is contained in:
iou1name 2018-09-25 11:58:07 -04:00
parent cb487f231e
commit ba28cb94e1
15 changed files with 108 additions and 112 deletions

View File

@ -4,7 +4,7 @@ Form(s) for the create_quest page.
"""
from django import forms
from quest.models import Quest, Post, PageTitle
from quest.models import Quest, Post
class QuestForm(forms.ModelForm):
"""

View File

@ -6,7 +6,7 @@ from django.contrib import messages
from django.shortcuts import redirect, render
from .forms import QuestForm, PostForm
from quest.models import Quest, Post, PageTitle
from quest.models import Quest, Post, Page
def index(request):
"""
@ -16,18 +16,19 @@ def index(request):
# 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 = Post(post_type='text')
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(
page = Page(
quest=quest,
page_num=1,
title="Page 1"
)
page.save()
post.quest = quest
post.page = page
post.save()
return redirect('quest:quest', quest_id=quest.id)
else:
quest_form = QuestForm()

View File

@ -1,3 +1,4 @@
{% set vars = {} %}
<!DOCTYPE html>
<html>
<head>

View File

@ -154,9 +154,10 @@ def text_post(socket, data):
# handle image
quest = Quest.objects.get(id=socket.quest_id)
p = Post(
quest=Quest.objects.get(id=socket.quest_id),
page_num=page_num,
quest=quest,
page=Page.objects.get(quest=quest, page_num=page_num),
post_type='text',
post_text=post_text)
p.save()
@ -196,10 +197,11 @@ def dice_post(socket, data):
post_text = "Roll " + dice_roll
if form['diceChal']:
post_text += " vs DC" + str(form['diceChal'])
quest = Quest.objects.get(id=socket.quest_id)
p = Post(
quest=Quest.objects.get(id=socket.quest_id),
page_num=page_num,
quest=quest,
page=Page.objects.get(quest=quest, page_num=page_num),
post_type='dice',
post_text=post_text
)
@ -232,9 +234,10 @@ def poll_post(socket, data):
return # error message?
form = form.cleaned_data
quest=Quest.objects.get(id=socket.quest_id)
p = Post(
quest=Quest.objects.get(id=socket.quest_id),
page_num=page_num,
quest=quest,
page=Page.objects.get(quest=quest, page_num=page_num),
post_type='poll',
post_text="Poll"
)
@ -412,19 +415,39 @@ def new_page(socket, data):
Called when the QM creates a new page.
"""
title = data.get('page_title')
appendix = bool(data.get('appendix'))
quest = Quest.objects.get(id=socket.quest_id)
p = PageTitle(
if appendix:
page = Page.objects.filter(
quest=quest,
appendix=True
).order_by('page_num').last()
if page:
page_num = chr(ord(page.page_num)+1)
else:
page_num = 'a'
else:
page_num = Page.objects.filter(
quest=quest,
appendix=False
).order_by('page_num').last().page_num + 1
p = Page(
quest=quest,
page_num=PageTitle.objects.filter(quest=quest).count() + 1,
page_num=page_num,
title=title,
appendix=appendix,
)
p.save()
data = {}
data['page_num'] = p.page_num
data['title'] = title
data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num])
if appendix:
data['appendix'] = True
else:
data['appendix'] = False
data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num])
socket.send('new_page', data)

View File

@ -23,9 +23,18 @@
{% endif %}
<li>
<select onChange="window.location.href=this.value">
{% for page in pages %}
<option value="{{ url('quest:quest', args=[quest_id, page.page_num]) }}"{% if page.page_num == page_num %} selected="yes"{% endif %}>{{ page.title }}</option>
{% endfor %}
<optgroup label="Pages">
{% for page in pages %}
<option value="{{ url('quest:quest', args=[quest_id, page.page_num]) }}"{% if page.page_num == page_num %} selected="yes" {% if vars.update({'next_page': loop.nextitem}) %}{% endif %}{% endif %}>{{ page.title }}</option>
{% endfor %}
</optgroup>
{% if appendices %}
<optgroup label="Appendices">
{% for appendix in appendices %}
<option value="{{ url('quest:quest', args=[quest_id, appendix.page_num]) }}"{% if appendix.page_num == page_num %} selected="yes"{% endif %}>{{ appendix.title }}</option>
{% endfor %}
</optgroup>
{% endif %}
</select>
</li>
<li id="toggleChat"><a onclick="toggle_chat()" href="javascript:void(0);">{% if request.session.get("hide_chat") == True %}←{% else %}→{% endif %}</a></li>
@ -143,14 +152,15 @@
<div id="QMPage" class="QMPostTabContent" style="display:none;">
<form id="QMPageForm" action="javascript:void(0);" onsubmit="form_post('QMPageForm', 'new_page');">
New Page: <input type="text" name="page_title" maxlength="200" value="Page {{ pages.count() + 1 }}"><br>
Appendix: <input type="checkbox" name="appendix"><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
{% endif %}
{% if page_num != pages.reverse()[0].page_num %}
{% if vars['next_page'] %}
<div id="nextPageContainer">
<input type="button" id="nextPage" value="Next Page: {{ pages[page_num].title }}" onclick="window.location.href='{{ url('quest:quest', args=[quest_id, page_num+1]) }}'">
<input type="button" id="nextPage" value="Next Page: {{ vars['next_page'].title }}" onclick="window.location.href='{{ url('quest:quest', args=[quest_id, vars['next_page'].page_num]) }}'">
</div>
{% endif %}
</div>
@ -158,7 +168,7 @@
<h1>Chat</h1>
<div id="chatWindow">
{% autoescape false %}
{% for message in messages %}
{% for message in chat_messages %}
<div id="msg-{{ message.id }}" class="message">
<div class="messageHeader">
<span class="messageName">{{ message.user.username or quest.anon_name }}</span>

View File

@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2018-09-05 17:44
# Generated by Django 2.1.1 on 2018-09-25 13:36
import django.core.validators
from django.db import migrations, models
@ -22,11 +22,12 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='PageTitle',
name='Page',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('page_num', models.IntegerField()),
('page_num', models.CharField(max_length=4)),
('title', models.CharField(max_length=200)),
('appendix', models.BooleanField(default=False)),
],
),
migrations.CreateModel(
@ -47,7 +48,6 @@ class Migration(migrations.Migration):
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)),
@ -58,16 +58,17 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('anon_name', models.CharField(default='Anonymous', max_length=20)),
],
),
migrations.CreateModel(
name='DiceCall',
fields=[
('post', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quest.Post')),
('dice_roll', models.CharField(max_length=9)),
('dice_roll', models.CharField(max_length=16)),
('strict', models.BooleanField()),
('dice_challenge', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(999), django.core.validators.MinValueValidator(1)])),
('rolls_taken', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(99), django.core.validators.MinValueValidator(1)])),
('dice_challenge', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(65536)])),
('rolls_taken', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(256)])),
('open', models.BooleanField()),
],
),
@ -75,7 +76,7 @@ class Migration(migrations.Migration):
name='DiceRoll',
fields=[
('message', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quest.Message')),
('roll', models.CharField(max_length=9)),
('roll', models.CharField(max_length=16)),
('results', models.TextField()),
('total', models.IntegerField()),
],

View File

@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2018-09-05 17:44
# Generated by Django 2.1.1 on 2018-09-25 13:36
from django.conf import settings
from django.db import migrations, models
@ -10,8 +10,8 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('quest', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
@ -20,6 +20,11 @@ class Migration(migrations.Migration):
name='owner',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='post',
name='page',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Page'),
),
migrations.AddField(
model_name='post',
name='quest',
@ -36,7 +41,7 @@ class Migration(migrations.Migration):
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='pagetitle',
model_name='page',
name='quest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Quest'),
),

View File

@ -1,18 +0,0 @@
# Generated by Django 2.1.1 on 2018-09-21 16:50
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0002_auto_20180905_1344'),
]
operations = [
migrations.AddField(
model_name='quest',
name='anon_name',
field=models.CharField(default='Anonymous', max_length=20),
),
]

View File

@ -1,34 +0,0 @@
# Generated by Django 2.1.1 on 2018-09-24 13:31
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0003_quest_anon_name'),
]
operations = [
migrations.AlterField(
model_name='dicecall',
name='dice_challenge',
field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(65536)]),
),
migrations.AlterField(
model_name='dicecall',
name='dice_roll',
field=models.CharField(max_length=16),
),
migrations.AlterField(
model_name='dicecall',
name='rolls_taken',
field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(256)]),
),
migrations.AlterField(
model_name='diceroll',
name='roll',
field=models.CharField(max_length=16),
),
]

View File

@ -31,12 +31,25 @@ class Message(models.Model):
message = models.TextField(max_length=512)
class Page(models.Model):
"""
Represents the title of a quest page.
"""
quest = models.ForeignKey(Quest, on_delete=models.CASCADE)
page_num = models.CharField(max_length=4)
title = models.CharField(max_length=200)
appendix = models.BooleanField(default=False)
def __str__(self):
return self.title
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)
page = models.ForeignKey(Page, on_delete=models.CASCADE)
POST_TYPES = (
('text', 'Text'),
('dice', 'Dice'),
@ -129,15 +142,3 @@ class PollVote(models.Model):
class Meta:
unique_together = ('option', 'ip_address')
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

@ -156,7 +156,7 @@ socket.events['set_option_box'] = function(data) {
}
socket.events['new_page'] = function(data) {
if (page_num != data.page_num-1) { return; }
let html_str = '<div id="nextPageContainer"><input type="button" id="nextPage" value="Next Page: ' + data.title + '" onclick="window.location.href=\'' + SCRIPT_NAME + data.url + '\'"></div>';
let html_str = '<div id="nextPageContainer"><input type="button" id="nextPage" value="Next Page: ' + data.title + '" onclick="window.location.href=\'' + data.url + '\'"></div>';
document.getElementById('questPane').innerHTML = document.getElementById('questPane').innerHTML + html_str;
}

View File

@ -10,7 +10,7 @@ app_name = 'quest'
urlpatterns = [
path('', views.index, name='index'),
path('<int:quest_id>', views.quest, name='quest'),
path('<int:quest_id>/<int:page_num>', views.quest, name='quest'),
path('<int:quest_id>/<page_num>', views.quest, name='quest'),
path('<int:quest_id>/edit_quest', views.edit_quest, name='edit_quest'),
path('<int:quest_id>/<int:page_num>/edit_quest', views.edit_quest, name='edit_quest'),
path('<int:quest_id>/<page_num>/edit_quest', views.edit_quest, name='edit_quest'),
]

View File

@ -2,10 +2,11 @@
"""
Quest and quest accessory views.
"""
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import Quest, DiceRoll, PollOption, PollVote, PageTitle
from .models import Quest, DiceRoll, PollOption, PollVote, Page
from .forms import EditQuestForm
def index(request):
@ -15,17 +16,23 @@ def index(request):
return HttpResponse("Hello, world. You're at the quest index.")
def quest(request, quest_id, page_num=1):
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 = PageTitle.objects.filter(quest=quest).order_by('page_num')
if page_num > pages.reverse()[0].page_num:
page_num = pages.reverse()[0].page_num
return redirect('quest:quest', quest_id=quest.id, page_num=page_num)
messages = quest.message_set.all()
posts = quest.post_set.filter(page_num=page_num)
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)

1
todo
View File

@ -1,5 +1,4 @@
New Features:
Pages/appendixes
Live indicator/countdown
Notifications
Banner images

View File

@ -1,4 +1,4 @@
# Generated by Django 2.1.1 on 2018-09-05 17:44
# Generated by Django 2.1.1 on 2018-09-25 13:36
import django.contrib.auth.models
import django.core.validators