Better status management.

Status is mainly computed in Python. That fix inconsistent datetime between
client and server.

Client only receives status and keep timestamp of last received ws msg.
This commit is contained in:
Aurélien Delobelle 2017-06-22 16:36:08 +02:00
parent 19847ac9d8
commit 5673fabeff
3 changed files with 78 additions and 53 deletions

View file

@ -1,3 +1,5 @@
from datetime import timedelta
from django.utils import timezone
from ..decorators import kfet_is_team
@ -13,6 +15,17 @@ class OpenKfet(CachedMixin, object):
Current state persists through cache.
"""
# status is unknown after this duration
time_unknown = timedelta(minutes=15)
# status
OPENED = 'opened'
CLOSED = 'closed'
UNKNOWN = 'unknown'
# admin status
FAKE_CLOSED = 'fake_closed'
# cached attributes config
cached = {
'_raw_open': False,
'_last_update': None,
@ -40,6 +53,19 @@ class OpenKfet(CachedMixin, object):
"""Take into account force_close."""
return False if self.force_close else self.raw_open
def status(self):
if (self.last_update is None or
timezone.now() - self.last_update >= self.time_unknown):
return self.UNKNOWN
return self.OPENED if self.is_open else self.CLOSED
def admin_status(self, status=None):
if status is None:
status = self.status()
if status == self.CLOSED and self.raw_open:
return self.FAKE_CLOSED
return status
def _export(self):
"""Export internal state.
@ -51,27 +77,26 @@ class OpenKfet(CachedMixin, object):
- base for others.
"""
status = self.status()
base = {
'is_open': self.is_open,
'last_update': self.last_update,
'status': status,
}
restrict = {
'raw_open': self.raw_open,
'admin_status': self.admin_status(status),
'force_close': self.force_close,
}
return base, {**base, **restrict}
def export(self, user=None):
"""Export internal state.
def export(self, user):
"""Export internal state for a given user.
Returns:
(dict): Internal state. Only variables visible for the user are
exported, according to its permissions. If no user is given, it
returns all available values.
exported, according to its permissions.
"""
base, team = self._export()
return team if user is None or kfet_is_team(user) else base
return team if kfet_is_team(user) else base
def send_ws(self):
"""Send internal state to websocket channels."""