From 5485bf8aa96dc2b2f8938c10229174667cf7615b Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 24 Aug 2018 17:45:18 -0400 Subject: [PATCH] added opening/closing posts --- quest/consumers.py | 2 +- quest/events.py | 41 +++++++++- quest/jinja2/quest/quest.html | 17 +++-- quest/migrations/0008_auto_20180824_0855.py | 22 ++++++ quest/models.py | 2 +- quest/static/quest.js | 47 +++++++++++- quest/static/questQM.js | 85 +++++++++++++++++---- 7 files changed, 190 insertions(+), 26 deletions(-) create mode 100644 quest/migrations/0008_auto_20180824_0855.py diff --git a/quest/consumers.py b/quest/consumers.py index ed84425..b28d095 100644 --- a/quest/consumers.py +++ b/quest/consumers.py @@ -40,7 +40,7 @@ class QuestConsumer(WebsocketConsumer): return 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 to JSON before sending it. diff --git a/quest/events.py b/quest/events.py index d382882..be3b9f2 100644 --- a/quest/events.py +++ b/quest/events.py @@ -154,10 +154,13 @@ def dice_post(socket, data): dice_roll=dice_roll, strict=form['diceStrict'], dice_challenge=form['diceChal'], - rolls_taken=form['diceRollsTaken'] + rolls_taken=form['diceRollsTaken'], + open=True, ) d.save() + socket.send('close_all_posts') + data = {} data['post_text'] = post_text data['post_type'] = 'dice' @@ -166,6 +169,42 @@ def dice_post(socket, 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 = {} for obj in dir(): if type(locals()[obj]) == types.FunctionType: diff --git a/quest/jinja2/quest/quest.html b/quest/jinja2/quest/quest.html index 18e2386..c5c4f71 100644 --- a/quest/jinja2/quest/quest.html +++ b/quest/jinja2/quest/quest.html @@ -34,9 +34,9 @@ {% if post.post_type == "text" %}
{% elif post.post_type == "dice" %} -
+
{% elif post.post_type == "poll" %} -
+
{% endif %}
{{ localtime(post.timestamp).strftime('%Y-%m-%d %H:%M') }} @@ -44,9 +44,12 @@ {% if post.post_type == "text" %}
Edit - {% elif post.post_type in ("dice", "poll") and post == posts|last %} -
- + {% elif post.post_type == "dice" %} +
+ + {% elif post.post_type == "poll" %} +
+ {% endif %} {% endif %}
@@ -56,14 +59,14 @@ {{ post.post_text }} {% endautoescape %} {% elif post.post_type == "dice" %} -

{{ post.post_text }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}

+

{{ post.post_text }} - {% if post.dicecall.open %}Open{% else %}Closed{% endif %}

{# for dice_roll in dice_rolls.get(post.id, []) %}
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 %}
{% endfor #} {% elif post.post_type == "poll" %} -

{{ post.post_text }} - {% if post.id == quest.open_post_id %}Open{% else %}Closed{% endif %}

+

{{ post.post_text }} - {% if post.poll.open %}Open{% else %}Closed{% endif %}

{# for option in options.get(post.id, []) %} diff --git a/quest/migrations/0008_auto_20180824_0855.py b/quest/migrations/0008_auto_20180824_0855.py new file mode 100644 index 0000000..5cfc820 --- /dev/null +++ b/quest/migrations/0008_auto_20180824_0855.py @@ -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), + ), + ] diff --git a/quest/models.py b/quest/models.py index f0f07a6..79d7629 100644 --- a/quest/models.py +++ b/quest/models.py @@ -14,7 +14,6 @@ class Quest(models.Model): owner = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - open_post_id = models.IntegerField(null=True) class Post(models.Model): @@ -60,6 +59,7 @@ class DiceCall(models.Model): MinValueValidator(1) ] ) + open = models.BooleanField(default=False) class PageTitle(models.Model): diff --git a/quest/static/quest.js b/quest/static/quest.js index e551b38..9d8335c 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -27,7 +27,7 @@ function load() { }); } -/* Websocket events */ +/* Websocket receive */ socket.events['message'] = function(data) { let msg_str = '
'; msg_str = '
' + data.name + ' '; @@ -79,7 +79,18 @@ socket.events['new_post'] = function(data) { 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 */ function padToTwo(number) { @@ -93,7 +104,7 @@ function strftime(date) { return date_str; } -/* User-facing */ +/* DOM editing */ function quote(message_id) { let textbox = document.getElementById('messageTextArea'); textbox.value += '>>' + message_id + '\n'; @@ -121,3 +132,33 @@ function scrollToMsg(message_id) { if (!elem) { return; } 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'; + } + } +} diff --git a/quest/static/questQM.js b/quest/static/questQM.js index 4667e17..e93d957 100644 --- a/quest/static/questQM.js +++ b/quest/static/questQM.js @@ -1,3 +1,4 @@ +/* Websocket receive */ socket.events['new_post'] = function(data) { //deactivate_post(); let qposts = document.getElementById('questPosts'); @@ -46,6 +47,7 @@ socket.events['new_post'] = function(data) { qposts.innerHTML = qposts.innerHTML + post_str; }; +/* socket send */ function makePost() { let qparea = document.getElementById('postTextArea'); let text = qparea.value.trim(); @@ -53,19 +55,6 @@ function makePost() { if (text == '') { return; } 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) { let formData = new FormData(document.getElementById(form_id)); let obj = {}; @@ -77,3 +66,73 @@ function form_post(form_id, emit_msg) { socket.send(emit_msg, obj); 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'; + } + } +}