kfetOpen bullet working

This commit is contained in:
Qwann 2017-02-11 00:29:12 +01:00
parent 4808650fa0
commit f87f1ceff1
10 changed files with 121 additions and 6 deletions

View file

@ -83,6 +83,7 @@ TEMPLATES = [
'django.core.context_processors.static',
'gestioncof.shared.context_processor',
'kfet.context_processors.auth',
'kfet.context_processors.kfet_open',
],
},
},

View file

@ -24,3 +24,21 @@ class KPsul(JsonWebsocketConsumer):
def disconnect(self, message, **kwargs):
pass
class KfetOpen(JsonWebsocketConsumer):
# Set to True if you want them, else leave out
strict_ordering = False
slight_ordering = False
def connection_groups(self, **kwargs):
return ['kfet.is_open']
def connect(self, message, **kwargs):
pass
def receive(self, content, **kwargs):
pass
def disconnect(self, message, **kwargs):
pass

View file

@ -5,6 +5,8 @@ from __future__ import (absolute_import, division,
from builtins import *
from django.contrib.auth.context_processors import PermWrapper
from .views import KFET_OPEN
def auth(request):
if hasattr(request, 'real_user'):
@ -13,3 +15,11 @@ def auth(request):
'perms': PermWrapper(request.real_user),
}
return {}
def kfet_open(resquest):
(kfet_open, kfet_open_date) = KFET_OPEN()
return {
'kfet_open': kfet_open,
'kfet_open_date': kfet_open_date,
}

View file

@ -9,4 +9,5 @@ from kfet import consumers
channel_routing = [
route_class(consumers.KPsul, path=r"^/ws/k-fet/k-psul/$"),
route_class(consumers.KfetOpen, path=r"^/ws/k-fet/is_open/$"),
]

View file

@ -14,7 +14,7 @@ nav {
}
nav .navbar-brand {
padding:3px 25px;
padding:3px 15px 3px 25px;
}
nav .navbar-brand img {
@ -44,6 +44,24 @@ nav a {
background-color:#C8102E;
}
#kfet-open {
font-weight: bold;
font-size: 14px;
width:10px;
height:10px;
text-transform: uppercase;
border-radius: 50%;
background-color: white;
display: inline-block;
}
#kfet-open-wrapper {
padding-top: 18px;
margin: 0px 10px;
display: inline-block;
line-height: 10px;
}
.dropdown-menu {
padding:0;
}

View file

@ -0,0 +1,54 @@
function kfet_open(init_date, init_satus) {
// VARIABLES
var kfet_open_bullet = $('#kfet-open');
var open_color = "#73C252";
var closed_color = "#B42B26";
var unknown_color = "#ECD03E";
var kfet_open_date = init_date;
var kfet_open = init_status;
// INITIALISAITION
update_open_bullet();
// FONCTIONS
function nb_min_diff() {
var date_now = new Date();
// On calcule le nb de minutes depuis le dernier
// envoi d'information
tmp = date_now - kfet_open_date;
tmp = Math.floor(tmp/1000); // Nombre de secondes entre les 2 dates
diff_sec = tmp % 60; // Extraction du nombre de secondes
tmp = Math.floor((tmp-diff_sec)/60); // Nombre de minutes (partie entière)
return tmp;
}
function update_open_bullet() {
nb_min = nb_min_diff();
console.log("K-Fêt ouverte : " + kfet_open);
console.log(nb_min + " minute(s) depuis la dernière mise à jour");
if (nb_min > 5) {
kfet_open_bullet.css({'background-color': unknown_color});
} else if (kfet_open){
kfet_open_bullet.css({'background-color': open_color});
} else {
kfet_open_bullet.css({'background-color': closed_color});
}
}
// SYNCHRONIZATION
websocket_msg_default = {'last_op': 0}
var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws';
var location_host = window.location.host;
var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host;
var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/is_open/");
socket.onmessage = function(e) {
var data = $.extend({}, websocket_msg_default, JSON.parse(e.data));
console.log("Message reçu de la part de la porte.");
kfet_open_date = new Date(data['kfet_open_date']);
kfet_open = data['kfet_open'];
update_open_bullet();
}
}

View file

@ -18,6 +18,8 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" src="{% static 'kfet/js/jquery-confirm.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/kfet_open.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/reconnecting-websocket.js' %}"></script>
{% block extra_head %}{% endblock %}

View file

@ -12,6 +12,7 @@
<a class="navbar-brand" href="#">
<img src="{% static 'kfet/img/logo3.png' %}">
</a>
<span id="kfet-open-wrapper"><span id="kfet-open"></span></span>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
@ -23,7 +24,7 @@
{% endif %}
{% if user.profile.account_kfet %}
<li>
<a href="{% url 'kfet.account.read' user.profile.account_kfet.trigramme %}">Mes infos</a>
<a href="{% url 'kfet.account.read' user.profile.account_kfet.trigramme %}">Mes infos</a>
</li>
{% endif %}
{% if perms.kfet.is_team %}
@ -58,11 +59,13 @@
</nav>
<script type="text/javascript">
$(document).ready(function () {
$('#genericteam').on('click', function () {
setTimeout(function () { location.reload() }, 1000);
});
init_date = new Date("{{ kfet_open_date.isoformat }}");
init_status = {{ kfet_open | yesno:"true,false"}};
kfet_open(init_date, init_status);
});
</script>

View file

@ -6,7 +6,6 @@
<link rel="stylesheet" style="text/css" href="{% static 'kfet/css/kpsul_grid.css' %}">
<script src="{% static "autocomplete_light/autocomplete.js" %}" type="text/javascript"></script>
<script type="text/javascript" src="{% static 'kfet/js/js.cookie.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/reconnecting-websocket.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/jquery-ui.min.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/jquery-confirm.js' %}"></script>
<script type="text/javascript" src="{% static 'kfet/js/moment.js' %}"></script>

View file

@ -55,11 +55,20 @@ def KFET_OPEN():
class UpdateKfetOpen(View):
def get(self, request, *args, **kwargs):
open_string = request.GET.get('open')
is_open = not (open_string == "false" or open_string == "False")
is_open = "open" in request.GET
cache.set('KFET_OPEN', is_open)
cache.set('KFET_OPEN_DATE', timezone.now())
# <debug, delete me ?>
# Websocket
websocket_data = {}
websocket_data['kfet_open'] = is_open
websocket_data['kfet_open_date'] = timezone.now()
print(timezone.now())
consumers.KfetOpen.group_send('kfet.is_open', websocket_data)
(is_open_get, time) = KFET_OPEN()
# </debug, delete me?>
return HttpResponse("%r at %s" % (is_open_get, time.isoformat()))