From 6117fbe1e7be80e18fe830d1304a64f05845f005 Mon Sep 17 00:00:00 2001 From: iou1name Date: Wed, 5 Sep 2018 08:41:53 -0400 Subject: [PATCH] added writeins --- quest/events.py | 30 ++++++++++++++++++++++++++++++ quest/jinja2/quest/quest.html | 10 +++++----- quest/static/quest.js | 23 ++++++++++++----------- quest/static/questQM.js | 8 ++++---- todo | 4 +++- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/quest/events.py b/quest/events.py index 44d611f..4d87f41 100644 --- a/quest/events.py +++ b/quest/events.py @@ -353,6 +353,36 @@ def vote(socket, data): socket.send('vote', data) +def write_in(socket, data): + """ + Called when a player creates a new write-in. + """ + post_id = data.get('post_id') + option_text = data.get('option_text', '') + user = socket.scope['user'] + + option_text = option_text.strip() + if not option_text: + return + option_text = "Write in: " + bleach.clean(option_text) + if len(option_text) > 200: + # error message? + return + p = Poll.objects.get(post_id=post_id) + o = PollOption( + poll=p, + text=option_text + ) + o.save() + + data = {} + data['post_id'] = post_id + data['post_type'] = 'poll' + data['option_id'] = o.id + data['option_text'] = option_text + socket.send('update_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 38da27b..f46e7dc 100644 --- a/quest/jinja2/quest/quest.html +++ b/quest/jinja2/quest/quest.html @@ -79,12 +79,12 @@ {% endfor %} - {# if post.id == quest.open_post_id and post.id|is_write_in %} -
- Write-in:
- + {% if post.poll.open and post.poll.allow_writein %} +
+ Write-in:
+
- {% endif #} + {% endif %} {% endif %}

diff --git a/quest/static/quest.js b/quest/static/quest.js index e8fece6..b7dfcae 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -70,8 +70,8 @@ socket.events['new_post'] = function(data) { post_str += ''; if (data.allow_writein) { post_str += '
'; - post_str += 'Write-in:
'; - post_str += '
'; + post_str += 'Write-in:
'; + post_str += ''; } } post_str += '
'; @@ -95,16 +95,17 @@ socket.events['close_all_posts'] = function(data) { } socket.events['update_post'] = function(data) { let post = document.getElementById('questPostData-' + data.post_id); - if (data.post_type === 'text') { + if (data.post_type === 'text') { // edit post post.innerHTML = data.post_text; - } else if (data.post_type === 'dice') { + } else if (data.post_type === 'dice') { // diceroll post.innerHTML += '' + data.post_text + '
'; - } else if (data.post_type === 'poll') { + } else if (data.post_type === 'poll') { // write-in let row = post.children[1].insertRow(-1); + row.id = 'optionRow-' + data.option_id; let cell = row.insertCell(0); cell.className = 'pollCheckBox'; - cell.innerHTML = ''; - cell.innerHTML += ''; + cell.innerHTML = ''; + cell.innerHTML += ''; cell = row.insertCell(1); cell.className = 'option_text'; @@ -141,12 +142,12 @@ function vote(post_id, option_id) { socket.send('vote', {post_id: post_id, option_id: option_id, polarity: polarity, quest_id: quest_id}); } function submitWritein(post_id) { - let writeinInput = document.getElementById('writeinInput'); + let writeinInput = document.getElementById('writeinInput-' + post_id); if (!writeinInput) { return; } let option_text = writeinInput.value; writeinInput.value = ''; if (!option_text) { return; } - socket.emit('write_in', {option_text: option_text, post_id: post_id, room: room_id}); + socket.send('write_in', {option_text: option_text, post_id: post_id}); } /* Helpers */ @@ -201,7 +202,7 @@ function close_post(post_id) { for (let i = 0; i < boxes.length; i++) { boxes[i].disabled = true; } - let writein = document.getElementById('writeinContainer'); + let writein = document.getElementById('writeinContainer-' + post_id); if (writein) { writein.style.display = 'none'; } @@ -217,7 +218,7 @@ function open_post(post_id) { for (let i = 0; i < boxes.length; i++) { boxes[i].disabled = false; } - let writein = document.getElementById('writeinContainer'); + let writein = document.getElementById('writeinContainer-' + post_id); if (writein) { writein.style.display = 'initial'; } diff --git a/quest/static/questQM.js b/quest/static/questQM.js index c65d1a7..f92917d 100644 --- a/quest/static/questQM.js +++ b/quest/static/questQM.js @@ -37,8 +37,8 @@ socket.events['new_post'] = function(data) { post_str += ''; if (data.allow_writein) { post_str += '
'; - post_str += 'Write-in:
'; - post_str += '
'; + post_str += 'Write-in:
'; + post_str += ''; } } post_str += '
'; @@ -111,7 +111,7 @@ function close_post(post_id) { for (let i = 0; i < boxes.length; i++) { boxes[i].disabled = true; } - let writein = document.getElementById('writeinContainer'); + let writein = document.getElementById('writeinContainer-' + post_id); if (writein) { writein.style.display = 'none'; } @@ -131,7 +131,7 @@ function open_post(post_id) { for (let i = 0; i < boxes.length; i++) { boxes[i].disabled = false; } - let writein = document.getElementById('writeinContainer'); + let writein = document.getElementById('writeinContainer-' + post_id); if (writein) { writein.style.display = 'initial'; } diff --git a/todo b/todo index ca52b9b..5a2a14a 100644 --- a/todo +++ b/todo @@ -31,4 +31,6 @@ Adjust quote preview postioning Port from old code: Edit post -Writein +Edit quest +Pages +Images