added opening/closing posts

This commit is contained in:
iou1name 2018-08-24 17:45:18 -04:00
parent 1c1ed68d7b
commit 5485bf8aa9
7 changed files with 190 additions and 26 deletions

View File

@ -40,7 +40,7 @@ class QuestConsumer(WebsocketConsumer):
return return
self.events[event](self, data.get('data')) self.events[event](self, data.get('data'))
def send(self, event, data): def send(self, event, data={}):
""" """
Overridden method. If a dictionary is provided, it is converted Overridden method. If a dictionary is provided, it is converted
to JSON before sending it. to JSON before sending it.

View File

@ -154,10 +154,13 @@ def dice_post(socket, data):
dice_roll=dice_roll, dice_roll=dice_roll,
strict=form['diceStrict'], strict=form['diceStrict'],
dice_challenge=form['diceChal'], dice_challenge=form['diceChal'],
rolls_taken=form['diceRollsTaken'] rolls_taken=form['diceRollsTaken'],
open=True,
) )
d.save() d.save()
socket.send('close_all_posts')
data = {} data = {}
data['post_text'] = post_text data['post_text'] = post_text
data['post_type'] = 'dice' data['post_type'] = 'dice'
@ -166,6 +169,42 @@ def dice_post(socket, data):
socket.send('new_post', data) socket.send('new_post', data)
def close_post(socket, data):
"""
Called when the QM closes an open post.
"""
post_id = data.get('post_id')
p = Post.objects.get(id=post_id)
if data.get('post_type') == 'dice':
p.dicecall.open = False
p.dicecall.save()
elif data.get('post_type') == 'poll':
p.poll.open = False
p.poll.save()
data = {}
data['post_id'] = post_id
socket.send('close_post', data)
def open_post(socket, data):
"""
Called when the QM opens a closed post.
"""
post_id = data.get('post_id')
p = Post.objects.get(id=post_id)
if data.get('post_type') == 'dice':
p.dicecall.open = True
p.dicecall.save()
elif data.get('post_type') == 'poll':
p.poll.open = True
p.poll.save()
data = {}
data['post_id'] = post_id
socket.send('open_post', data)
events = {} events = {}
for obj in dir(): for obj in dir():
if type(locals()[obj]) == types.FunctionType: if type(locals()[obj]) == types.FunctionType:

View File

@ -34,9 +34,9 @@
{% if post.post_type == "text" %} {% if post.post_type == "text" %}
<div class="questPost textPost"> <div class="questPost textPost">
{% elif post.post_type == "dice" %} {% elif post.post_type == "dice" %}
<div class="questPost dicePost{% if post == posts|last %} activePost{% endif %}"> <div class="questPost dicePost{% if post.dicecall.open %} activePost{% endif %}">
{% elif post.post_type == "poll" %} {% elif post.post_type == "poll" %}
<div class="questPost pollPost{% if post == posts|last %} activePost{% endif %}"> <div class="questPost pollPost{% if post.poll.open %} activePost{% endif %}">
{% endif %} {% endif %}
<div class="questPostMeta"> <div class="questPostMeta">
{{ localtime(post.timestamp).strftime('%Y-%m-%d %H:%M') }} {{ localtime(post.timestamp).strftime('%Y-%m-%d %H:%M') }}
@ -44,9 +44,12 @@
{% if post.post_type == "text" %} {% if post.post_type == "text" %}
<br /><a href="javascript:void(0);" id="editPost-{{ post.id }}" onclick="edit_post({{ post.id }})">Edit</a> <br /><a href="javascript:void(0);" id="editPost-{{ post.id }}" onclick="edit_post({{ post.id }})">Edit</a>
<a href="javascript:void(0);" id="savePost-{{ post.id }}" onclick="save_post('{{ post.id }}')" style="display:none;">Save</a> <a href="javascript:void(0);" id="savePost-{{ post.id }}" onclick="save_post('{{ post.id }}')" style="display:none;">Save</a>
{% elif post.post_type in ("dice", "poll") and post == posts|last %} {% elif post.post_type == "dice" %}
<br /><a href="javascript:void(0);" id="closePost-{{ post.id }}" onclick="close_post({{ post.id }})"{% if post.id != quest.open_post_id %} style="display:none;"{% endif %}>Close</a> <br /><a href="javascript:void(0);" id="closePost-{{ post.id }}" onclick="close_post_send({{ post.id }})"{% if not post.dicecall.open %} style="display:none;"{% endif %}>Close</a>
<a href="javascript:void(0);" id="openPost-{{ post.id }}" onclick="open_post({{ post.id }})"{% if post.id == quest.open_post_id %} style="display:none;"{% endif %}>Open</a> <a href="javascript:void(0);" id="openPost-{{ post.id }}" onclick="open_post_send({{ post.id }})"{% if post.dicecall.open %} style="display:none;"{% endif %}>Open</a>
{% elif post.post_type == "poll" %}
<br /><a href="javascript:void(0);" id="closePost-{{ post.id }}" onclick="close_post_send({{ post.id }})"{% if not post.poll.open %} style="display:none;"{% endif %}>Close</a>
<a href="javascript:void(0);" id="openPost-{{ post.id }}" onclick="open_post_send({{ post.id }})"{% if post.poll.open %} style="display:none;"{% endif %}>Open</a>
{% endif %} {% endif %}
{% endif %} {% endif %}
</div> </div>
@ -56,14 +59,14 @@
{{ post.post_text }} {{ post.post_text }}
{% endautoescape %} {% endautoescape %}
{% elif post.post_type == "dice" %} {% elif post.post_type == "dice" %}
<h3>{{ post.post_text }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}</h3> <h3>{{ post.post_text }} - {% if post.dicecall.open %}Open{% else %}Closed{% endif %}</h3>
{# for dice_roll in dice_rolls.get(post.id, []) %} {# for dice_roll in dice_rolls.get(post.id, []) %}
<div id="questRollId-{{ dice_roll[0] }}"> <div id="questRollId-{{ dice_roll[0] }}">
<b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if post.id|dice_chal != 0 %} - {% if dice_roll[5] >= post.id|dice_chal %}Pass{% else %}Fail{% endif %}{% endif %}</b> <b>Rolled {{ dice_roll[4] }} = {{ dice_roll[5] }} ({{ dice_roll[3] }}){% if post.id|dice_chal != 0 %} - {% if dice_roll[5] >= post.id|dice_chal %}Pass{% else %}Fail{% endif %}{% endif %}</b>
</div> </div>
{% endfor #} {% endfor #}
{% elif post.post_type == "poll" %} {% elif post.post_type == "poll" %}
<h3>{{ post.post_text }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}</h3> <h3>{{ post.post_text }} - {% if post.poll.open %}Open{% else %}Closed{% endif %}</h3>
<table class="poll" id="poll-{{ post.id }}"> <table class="poll" id="poll-{{ post.id }}">
{# for option in options.get(post.id, []) %} {# for option in options.get(post.id, []) %}
<tr id="optionRow-{{ option[0] }}"> <tr id="optionRow-{{ option[0] }}">

View File

@ -0,0 +1,22 @@
# Generated by Django 2.1 on 2018-08-24 12:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('quest', '0007_dicecall'),
]
operations = [
migrations.RemoveField(
model_name='quest',
name='open_post_id',
),
migrations.AddField(
model_name='dicecall',
name='open',
field=models.BooleanField(default=False),
),
]

View File

@ -14,7 +14,6 @@ class Quest(models.Model):
owner = models.ForeignKey( owner = models.ForeignKey(
settings.AUTH_USER_MODEL, settings.AUTH_USER_MODEL,
on_delete=models.CASCADE) on_delete=models.CASCADE)
open_post_id = models.IntegerField(null=True)
class Post(models.Model): class Post(models.Model):
@ -60,6 +59,7 @@ class DiceCall(models.Model):
MinValueValidator(1) MinValueValidator(1)
] ]
) )
open = models.BooleanField(default=False)
class PageTitle(models.Model): class PageTitle(models.Model):

View File

@ -27,7 +27,7 @@ function load() {
}); });
} }
/* Websocket events */ /* Websocket receive */
socket.events['message'] = function(data) { socket.events['message'] = function(data) {
let msg_str = '<div id="message-' + data.message_id + '" class="message">'; let msg_str = '<div id="message-' + data.message_id + '" class="message">';
msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> '; msg_str = '<div class="messageHeader"><span class="messageName">' + data.name + '</span> ';
@ -79,7 +79,18 @@ socket.events['new_post'] = function(data) {
post_str += '</div></div><br />'; post_str += '</div></div><br />';
qposts.innerHTML = qposts.innerHTML + post_str; qposts.innerHTML = qposts.innerHTML + post_str;
}; };
socket.events['close_post'] = function(data) {
close_post(data.post_id);
}
socket.events['open_post'] = function(data) {
open_post(data.post_id);
}
socket.events['close_all_posts'] = function(data) {
let posts = document.getElementsByClassName('activePost');
for (let i = 0; i < posts.length; i++) {
close_post(post.children[1].id.slice(14)); // retreive the number at the end
}
}
/* Helpers */ /* Helpers */
function padToTwo(number) { function padToTwo(number) {
@ -93,7 +104,7 @@ function strftime(date) {
return date_str; return date_str;
} }
/* User-facing */ /* DOM editing */
function quote(message_id) { function quote(message_id) {
let textbox = document.getElementById('messageTextArea'); let textbox = document.getElementById('messageTextArea');
textbox.value += '>>' + message_id + '\n'; textbox.value += '>>' + message_id + '\n';
@ -121,3 +132,33 @@ function scrollToMsg(message_id) {
if (!elem) { return; } if (!elem) { return; }
elem.scrollIntoView(); elem.scrollIntoView();
} }
function close_post(post_id) {
let post = document.getElementById('questPostData-' + post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + post_id);
let boxes = table.getElementsByTagName('input');
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = true;
}
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.style.display = 'none';
}
}
}
function open_post(post_id) {
let post = document.getElementById('questPostData-' + post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + post_id);
let boxes = table.getElementsByTagName('input');
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = false;
}
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.style.display = 'initial';
}
}
}

View File

@ -1,3 +1,4 @@
/* Websocket receive */
socket.events['new_post'] = function(data) { socket.events['new_post'] = function(data) {
//deactivate_post(); //deactivate_post();
let qposts = document.getElementById('questPosts'); let qposts = document.getElementById('questPosts');
@ -46,6 +47,7 @@ socket.events['new_post'] = function(data) {
qposts.innerHTML = qposts.innerHTML + post_str; qposts.innerHTML = qposts.innerHTML + post_str;
}; };
/* socket send */
function makePost() { function makePost() {
let qparea = document.getElementById('postTextArea'); let qparea = document.getElementById('postTextArea');
let text = qparea.value.trim(); let text = qparea.value.trim();
@ -53,19 +55,6 @@ function makePost() {
if (text == '') { return; } if (text == '') { return; }
socket.send('text_post', {text: text, page_num: page_num, quest_id: quest_id}); socket.send('text_post', {text: text, page_num: page_num, quest_id: quest_id});
} }
function openPostTab(event, modeName) {
let QMPostTabContent = document.getElementsByClassName("QMPostTabContent");
for (let i = 0; i < QMPostTabContent.length; i++) {
QMPostTabContent[i].style.display = "none";
}
let QMPostTab = document.getElementsByClassName("QMPostTab");
for (let i = 0; i < QMPostTab.length; i++) {
QMPostTab[i].className = QMPostTab[i].className.replace(" active", "");
}
document.getElementById(modeName).style.display = "block";
event.currentTarget.className += " active";
}
function form_post(form_id, emit_msg) { function form_post(form_id, emit_msg) {
let formData = new FormData(document.getElementById(form_id)); let formData = new FormData(document.getElementById(form_id));
let obj = {}; let obj = {};
@ -77,3 +66,73 @@ function form_post(form_id, emit_msg) {
socket.send(emit_msg, obj); socket.send(emit_msg, obj);
document.getElementById(form_id).reset(); document.getElementById(form_id).reset();
} }
function close_post_send(post_id) {
let post = document.getElementById('questPostData-' + post_id);
if (post.parentElement.classList.contains('dicePost')) {
data = {post_type: 'dice', post_id: post_id};
} else if (post.parentElement.classList.contains('pollPost')) {
data = {post_type: 'poll', post_id: post_id};
}
socket.send('close_post', data);
}
function open_post_send(post_id) {
let post = document.getElementById('questPostData-' + post_id);
if (post.parentElement.classList.contains('dicePost')) {
data = {post_type: 'dice', post_id: post_id};
} else if (post.parentElement.classList.contains('pollPost')) {
data = {post_type: 'poll', post_id: post_id};
}
socket.send('open_post', data);
}
/* DOM editing */
function openPostTab(event, modeName) {
let QMPostTabContent = document.getElementsByClassName("QMPostTabContent");
for (let i = 0; i < QMPostTabContent.length; i++) {
QMPostTabContent[i].style.display = "none";
}
let QMPostTab = document.getElementsByClassName("QMPostTab");
for (let i = 0; i < QMPostTab.length; i++) {
QMPostTab[i].className = QMPostTab[i].className.replace(" active", "");
}
document.getElementById(modeName).style.display = "block";
event.currentTarget.className += " active";
}
function close_post(post_id) {
let post = document.getElementById('questPostData-' + post_id);
post.children[0].textContent = post.children[0].textContent.replace('Open', 'Closed');
/* QM only */
document.getElementById('closePost-' + post_id).style.display = 'none';
document.getElementById('openPost-' + post_id).style.display = 'initial';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + post_id);
let boxes = table.getElementsByTagName('input');
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = true;
}
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.style.display = 'none';
}
}
}
function open_post(post_id) {
let post = document.getElementById('questPostData-' + post_id);
post.firstElementChild.textContent = post.firstElementChild.textContent.replace('Closed', 'Open');
/* QM only */
document.getElementById('closePost-' + post_id).style.display = 'initial';
document.getElementById('openPost-' + post_id).style.display = 'none';
/* end QM only */
if (post.parentElement.classList.contains('pollPost')) {
let table = document.getElementById('poll-' + post_id);
let boxes = table.getElementsByTagName('input');
for (let i = 0; i < boxes.length; i++) {
boxes[i].disabled = false;
}
let writein = document.getElementById('writeinContainer');
if (writein) {
writein.style.display = 'initial';
}
}
}