diff --git a/quest/jinja2/quest/quest.html b/quest/jinja2/quest/quest.html
index 9b87977..3ef2cb3 100644
--- a/quest/jinja2/quest/quest.html
+++ b/quest/jinja2/quest/quest.html
@@ -21,7 +21,7 @@
{% if request.user == quest.owner %}
Edit Quest
{% endif %}
-
+
- {% if quest.live %}
-
+
LIVE
- {% else %}
- {% if quest.live_time %}
-
- Live in: ({{ localtime(quest.live_time).strftime('%Y-%m-%d %H:%M') }})
+
+ Live in: ({% if quest.live_time %}{{ localtime(quest.live_time).strftime('%Y-%m-%d %H:%M') }}{% endif %})
- {% endif %}
- {% endif %}
{% if request.session.get("hide_chat") == True %}←{% else %}→{% endif %}
{% endblock %}
{% block content %}
diff --git a/quest/static/quest.js b/quest/static/quest.js
index 6de980f..b5e5742 100644
--- a/quest/static/quest.js
+++ b/quest/static/quest.js
@@ -147,6 +147,7 @@ socket.events['vote'] = function(data) {
arr = Array.prototype.slice.call(table.rows);
arr.sort(sort_by_votes);
let new_tbody = document.createElement('tbody');
+
for (let i = 0; i < arr.length; i++) {
new_tbody.appendChild(arr[i]);
}
@@ -160,6 +161,20 @@ socket.events['new_page'] = function(data) {
let 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 */
function vote(post_id, option_id) {
diff --git a/quest/tools.py b/quest/tools.py
index 9db3ea1..095eef4 100644
--- a/quest/tools.py
+++ b/quest/tools.py
@@ -4,11 +4,14 @@ Some miscellaneous tools and helper functions. Primarily for quests.
"""
import os
import re
+import json
import hashlib
import magic
import requests
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/"
ALLOWED_MIMES = [
@@ -69,3 +72,19 @@ def handle_img(text, limit=5):
text = text.replace("[img]" + ext_url + "[/img]", img_tag, 1)
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
+ }
+ )
diff --git a/quest/views.py b/quest/views.py
index a0d8bd0..bd61cef 100644
--- a/quest/views.py
+++ b/quest/views.py
@@ -14,6 +14,7 @@ from django.conf import settings
from .models import Quest, DiceRoll, PollOption, PollVote, Page, Post
from .forms import EditQuestForm, QuestForm, PostForm
from user.models import User
+from .tools import send_to_websocket
def index(request):
"""The quest page index."""
@@ -106,6 +107,13 @@ def edit_quest(request, quest_id, page_num='0'):
else:
quest.live_time = None
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)
else:
messages.error(request, "Error")