Compare commits

...

3 Commits

14 changed files with 186 additions and 56 deletions

View File

@ -9,7 +9,7 @@
<input type="text" name="title" placeholder="Search">
<input type="submit">
</form>
<a href="{{ url('quest:index') }}">Advanced</a><br>
<a href="{{ url('quest:index') }}">Quests</a><br>
<br>
<a href="./quest/1">Unga Bunga Quest</a><br />
{% if request.user.is_authenticated %}

View File

@ -431,10 +431,11 @@ def new_page(socket, data):
else:
page_num = 'a'
else:
page_num = Page.objects.filter(
last_page = Page.objects.filter(
quest=quest,
appendix=False
).order_by('page_num').last().page_num + 1
).order_by('page_num').last()
page_num = int(last_page.page_num) + 1
p = Page(
quest=quest,
page_num=page_num,

View File

@ -56,7 +56,7 @@ class QuestForm(forms.ModelForm):
"""
class Meta:
model = Quest
fields = ('title',)
fields = ('title', 'description', 'tags')
class PostForm(forms.ModelForm):

View File

@ -5,20 +5,43 @@
{% endblock %}
{% block content %}
<form method="get" action="{{ url('quest:index') }}">
Author: <input type="text" name="author"><br>
Title: <input type="text" name="title"><br>
Tags: <input type="text" name="tags"><br>
<input type="submit">
Search terms: <input type="text" name="search_terms">
<select name="search_type">
<option value="title"{% if search_type == 'title' %} selected{% endif %}>Title</option>
<option value="author"{% if search_type == 'author' %} selected{% endif %}>Author</option>
</select>
<br>Tags: <input type="text" name="tags">
<br>Order by:
<select name="order_by">
<option value="creation_date"{% if order_by == 'creation_date' %} selected{% endif %}>Creation date</option>
<option value="last_post"{% if order_by == 'last_post' %} selected{% endif %}>Last post</option>
<option value="next_live"{% if order_by == 'next_live' %} selected{% endif %}>Next live</option>
</select>
<select name="order_way">
<option value="desc"{% if order_way == 'desc' %} selected{% endif %}>Descending</option>
<option value="asc"{% if order_way == 'asc' %} selected{% endif %}>Ascending</option>
</select>
<br><input type="submit">
</form>
<h2>Quests</h2>
{% if results %}
<table id="results">
{% for quest in results %}
<tr>
<td class="title"><a href="{{ url('quest:quest', args=[quest.id, '0']) }}">{{ quest.title }}</a></td>
<td class="summary"></td>
<td class="author"><a href="{{ url('user:profile', args=[quest.owner.id]) }}">{{ quest.owner.username }}</a></td>
</tr>
{% endfor %}
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Author</th>
</tr>
</thead>
<tbody>
{% for quest in results %}
<tr>
<td class="title"><a href="{{ url('quest:quest', args=[quest.id, '0']) }}">{{ quest.title }}</a></td>
<td class="description">{{ quest.description or "" }}</td>
<td class="author"><a href="{{ url('user:profile', args=[quest.owner.id]) }}">{{ quest.owner.username }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}

View File

@ -4,12 +4,9 @@
<h1>New Quest</h1>
<form method="post" action="{{ url('quest:new_quest') }}">
{{ 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"/>
<input type="text" placeholder="Quest Title" name="title" maxlength="100" required><br>
<textarea name="description" maxlength="256"></textarea><br>
<input type="text" placeholder="Tags" name="tags" id=><br>
<input type="submit" name="submit" value="Submit">
</form>
{% endblock %}

View File

@ -162,7 +162,7 @@
</div>
<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>
New Page: <input type="text" name="page_title" maxlength="200" value="Page {{ pages.last().page_num|int + 1 }}"><br>
Appendix: <input type="checkbox" name="appendix"><br>
<input type="submit" name="submit" value="Submit">
</form>

View File

@ -2,6 +2,9 @@
{% block title %}{{ quest.title }}{% endblock %}
{% block head %}
<link rel="stylesheet" type="text/css" href="{{ static('quest.css') }}">
{% if request.user == quest.owner %}
<link rel="stylesheet" type="text/css" href="{{ static('questQM.css') }}">
{% endif %}
<script>
const quest_id = {{ quest.id }};
const page_num = '{{ page_num }}';
@ -9,8 +12,12 @@
const anon_name = '{{ quest.anon_name }}';
</script>
<script type="text/javascript" src="{{ static('quest.js') }}"></script>
{% if request.user == quest.owner %}
<script type="text/javascript" src="{{ static('questQM.js') }}"></script>
{% endif %}
<script>events = [socket.events['message'], socket.events['new_page']]; socket.events = {'message': events[0], 'new_page': events[1]}; delete events;</script>
<!-- We only want the message and new_page events on this page, others will error. Hacky solution, but it will do for now. -->
<script>window.onload = load;</script>
<!-- We only want the message event on this page, others will error. Hacky solution, but it will do for now. -->
{% endblock %}
{% block header %}
{% if request.user == quest.owner %}
@ -20,7 +27,7 @@
<select onChange="window.location.href=this.value">
<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>
<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 %}
@ -72,6 +79,22 @@
{% endfor %}
</ul>
{% endif %}
{% if quest.owner == request.user %}
<div id="QMPostPane">
<div>
<ul id="QMPostTabs">
<li><a class="QMPostTab" href="javascript:void(0);" onclick="openPostTab(event, 'QMPage')">Page</a></li>
</ul>
</div>
<div id="QMPage" class="QMPostTabContent" style="display:initial;">
<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.last().page_num|int + 1 }}"><br>
Appendix: <input type="checkbox" name="appendix"><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
{% endif %}
{% if vars['next_page'] %}
<div id="nextPageContainer">
<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]) }}'">

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-10-05 11:52
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0006_quest_anonymize'),
]
operations = [
migrations.AddField(
model_name='quest',
name='description',
field=models.CharField(blank=True, max_length=256, null=True),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-10-05 12:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0007_quest_description'),
]
operations = [
migrations.AlterField(
model_name='quest',
name='live',
field=models.BooleanField(default=False),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-10-05 13:21
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0008_auto_20181005_0825'),
]
operations = [
migrations.AddField(
model_name='quest',
name='latest_post_date',
field=models.DateTimeField(blank=True, null=True),
),
]

View File

@ -16,10 +16,20 @@ class Quest(models.Model):
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
anon_name = models.CharField(max_length=20, default="Anonymous")
live = models.BooleanField()
live = models.BooleanField(default=False)
live_time = models.DateTimeField(blank=True, null=True)
tags = TaggableManager()
anonymize = models.BooleanField(default=True)
description = models.CharField(max_length=256, blank=True, null=True)
latest_post_date = models.DateTimeField(null=True, blank=True)
def update_post_date(self):
try:
latest_post = self.post_set.order_by('-timestamp')[0]
self.latest_post_date = latest_post.timestamp
self.save()
except:
pass # no posts yet
class Message(models.Model):
@ -64,6 +74,14 @@ class Post(models.Model):
post_text = models.TextField()
timestamp = models.DateTimeField(auto_now=True)
def save(self, **kwargs):
super(Post, self).save(**kwargs)
self.quest.update_post_date()
def delete(self):
super(Post, self).delete()
self.quest.update_post_date()
class DiceCall(models.Model):
"""

View File

@ -157,7 +157,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=\'' + data.url + '\'"></div>';
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>';
document.getElementById('questPane').innerHTML = document.getElementById('questPane').innerHTML + html_str;
}

View File

@ -10,14 +10,24 @@
border: 1px solid #ccc;
}
thead {
border: 1px solid #ccc;
}
tr:nth-child(even) {
background-color: #DDDDDD;
}
.title {
width: 20%;
padding-left: 1em;
}
.description {
padding-left: 1em;
}
.author {
width: 20%;
text-align: center;
}

View File

@ -8,6 +8,7 @@ import bleach
from django.views.decorators.http import require_POST
from django.contrib import messages
from django.shortcuts import render, redirect
from django.db.models import F
from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post
from .forms import EditQuestForm, QuestForm, PostForm
@ -15,19 +16,33 @@ from user.models import User
def index(request):
"""The quest page index."""
author = request.GET.get('author')
title = request.GET.get('title')
search_terms = request.GET.get('search_terms')
search_type = request.GET.get('search_type')
tags = request.GET.get('tags')
order_by = request.GET.get('order_by')
order_way = request.GET.get('order_way', 'desc')
order_keys = {
'ceation_date': 'id',
'last_post': 'latest_post_date',
'next_live': 'live_time',
}
order_by = order_keys.get(order_by, 'id')
results = Quest.objects.all()
if author:
results = results.filter(
owner__username__unaccent__icontains=author)
if title:
results = results.filter(title__unaccent__icontains=title)
if search_terms:
if search_type == 'title':
results = results.filter(title__unaccent__icontains=search_terms)
elif search_type == 'author':
results = results.filter(
owner__username__unaccent__icontains=search_terms)
if tags:
results = results.filter(tags__name__in=tags.split())
results = results.distinct().order_by('-id')
results = results.filter(tags__name__in=tags.split()).distinct()
if order_way == 'asc':
results = results.order_by(F(order_by).asc(nulls_last=True))
elif order_way == 'desc':
results = results.order_by(F(order_by).desc(nulls_last=True))
context = locals()
return render(request, 'quest/index.html', context)
@ -106,35 +121,24 @@ def new_quest(request):
return redirect('login:index')
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.live = False
quest_form = QuestForm(request.POST)
if quest_form.is_valid():
quest = quest_form.save(commit=False)
quest.owner = request.user
quest.save()
page0 = Page(
quest_form.save_m2m()
print(quest.tags.names())
page = Page(
quest=quest,
page_num=0,
title="Homepage",
appendix=False,
)
page0.save()
page1 = Page(
quest=quest,
page_num=1,
title="Page 1",
appendix=False,
)
page1.save()
post.quest = quest
post.page = page1
post.save()
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}
context = locals()
return render(request, 'quest/new_quest.html', context)