editing the live settings in /edit_quest sends a websocket event

This commit is contained in:
iou1name 2018-10-09 10:03:32 -04:00
parent e1e38cbbe8
commit 36d0c0c874
4 changed files with 46 additions and 9 deletions

View File

@ -21,7 +21,7 @@
{% if request.user == quest.owner %} {% if request.user == quest.owner %}
<span><a href="{{ url('quest:edit_quest', args=[quest_id, page_num]) }}">Edit Quest</a></span> <span><a href="{{ url('quest:edit_quest', args=[quest_id, page_num]) }}">Edit Quest</a></span>
{% endif %} {% endif %}
<span> <span id="pageSelection">
<select onChange="window.location.href=this.value"> <select onChange="window.location.href=this.value">
<optgroup label="Pages"> <optgroup label="Pages">
{% for page in pages %} {% for page in pages %}
@ -37,17 +37,12 @@
{% endif %} {% endif %}
</select> </select>
</span> </span>
{% if quest.live %} <span id="live" style="display:{% if quest.live %}initial{% else %}none{% endif %};">
<span id="live">
LIVE LIVE
</span> </span>
{% else %} <span id="liveIn" style="display:{% if quest.live_time and not quest.live %}initial{% else %}none{% endif %};">
{% if quest.live_time %} Live in: <span id="liveCountdown"></span> (<span id="liveTime">{% if quest.live_time %}{{ localtime(quest.live_time).strftime('%Y-%m-%d %H:%M') }}{% endif %}</span>)
<span id="liveIn">
Live in: <span id="liveCountdown"></span> (<span id="liveTime">{{ localtime(quest.live_time).strftime('%Y-%m-%d %H:%M') }}</span>)
</span> </span>
{% endif %}
{% endif %}
<span id="toggleChat"><a onclick="toggle_chat()" href="javascript:void(0);">{% if request.session.get("hide_chat") == True %}←{% else %}→{% endif %}</a></span> <span id="toggleChat"><a onclick="toggle_chat()" href="javascript:void(0);">{% if request.session.get("hide_chat") == True %}←{% else %}→{% endif %}</a></span>
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View File

@ -147,6 +147,7 @@ socket.events['vote'] = function(data) {
arr = Array.prototype.slice.call(table.rows); arr = Array.prototype.slice.call(table.rows);
arr.sort(sort_by_votes); arr.sort(sort_by_votes);
let new_tbody = document.createElement('tbody'); let new_tbody = document.createElement('tbody');
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
new_tbody.appendChild(arr[i]); new_tbody.appendChild(arr[i]);
} }
@ -160,6 +161,20 @@ socket.events['new_page'] = function(data) {
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=\'' + SCRIPT_NAME + data.url + '\'"></div>';
document.getElementById('questPane').innerHTML = document.getElementById('questPane').innerHTML + html_str; document.getElementById('questPane').innerHTML = document.getElementById('questPane').innerHTML + html_str;
} }
socket.events['live'] = function(data) {
if (data.live) {
document.getElementById('live').style.display = 'initial';
document.getElementById('liveIn').style.display = 'none';
} else if (data.live_time) {
document.getElementById('live').style.display = 'none';
document.getElementById('liveIn').style.display = 'initial';
document.getElementById('liveTime').innerHTML = data.live_time;
live_countdown();
} else {
document.getElementById('live').style.display = 'none';
document.getElementById('liveIn').style.display = 'none';
}
}
/* Websocket send */ /* Websocket send */
function vote(post_id, option_id) { function vote(post_id, option_id) {

View File

@ -4,11 +4,14 @@ Some miscellaneous tools and helper functions. Primarily for quests.
""" """
import os import os
import re import re
import json
import hashlib import hashlib
import magic import magic
import requests import requests
from django.conf import settings from django.conf import settings
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
IMG_DIR = "/usr/local/www/html/img/" IMG_DIR = "/usr/local/www/html/img/"
ALLOWED_MIMES = [ ALLOWED_MIMES = [
@ -69,3 +72,19 @@ def handle_img(text, limit=5):
text = text.replace("[img]" + ext_url + "[/img]", img_tag, 1) text = text.replace("[img]" + ext_url + "[/img]", img_tag, 1)
return text return text
def send_to_websocket(event, quest_id, data={}):
"""
Acts like QuestConsumer.send() but callable from views.
"""
channel_layer = get_channel_layer()
group_name = f'quest_{quest_id}'
data = json.dumps({'event': event, 'data': data})
async_to_sync(channel_layer.group_send)(
group_name,
{
'type': 'dispatch_send',
'message': data
}
)

View File

@ -14,6 +14,7 @@ from django.conf import settings
from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post
from .forms import EditQuestForm, QuestForm, PostForm from .forms import EditQuestForm, QuestForm, PostForm
from user.models import User from user.models import User
from .tools import send_to_websocket
def index(request): def index(request):
"""The quest page index.""" """The quest page index."""
@ -106,6 +107,13 @@ def edit_quest(request, quest_id, page_num='0'):
else: else:
quest.live_time = None quest.live_time = None
quest.save() quest.save()
data = {
'live': quest.live,
'live_time': quest.live_time,
}
if data['live_time']:
data['live_time'] =data['live_time'].strftime('%Y-%m-%d %H:%M')
send_to_websocket('live', quest_id, data)
return redirect('quest:quest',quest_id=quest.id, page_num=page_num) return redirect('quest:quest',quest_id=quest.id, page_num=page_num)
else: else:
messages.error(request, "Error") messages.error(request, "Error")