2017-06-21 07:08:28 +02:00
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from ..decorators import kfet_is_team
|
|
|
|
from ..utils import CachedMixin
|
|
|
|
|
|
|
|
|
|
|
|
class OpenKfet(CachedMixin, object):
|
|
|
|
"""Manage "open" status of a place.
|
|
|
|
|
|
|
|
Stores raw data (e.g. sent by raspberry), and user-set values
|
|
|
|
(as force_close).
|
2017-06-21 23:31:27 +02:00
|
|
|
Setting differents `cache_prefix` allows different places management.
|
2017-06-21 07:08:28 +02:00
|
|
|
Current state persists through cache.
|
|
|
|
|
|
|
|
"""
|
|
|
|
cached = {
|
|
|
|
'_raw_open': False,
|
|
|
|
'_last_update': None,
|
|
|
|
'force_close': False,
|
|
|
|
}
|
|
|
|
cache_prefix = 'kfetopen'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def raw_open(self):
|
|
|
|
"""Defined as property to update `last_update` on `raw_open` update."""
|
|
|
|
return self._raw_open
|
|
|
|
|
|
|
|
@raw_open.setter
|
|
|
|
def raw_open(self, value):
|
|
|
|
self._last_update = timezone.now()
|
|
|
|
self._raw_open = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def last_update(self):
|
|
|
|
"""Prevent `last_update` to be set."""
|
|
|
|
return self._last_update
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_open(self):
|
|
|
|
"""Take into account force_close."""
|
|
|
|
return False if self.force_close else self.raw_open
|
|
|
|
|
|
|
|
def _export(self):
|
|
|
|
"""Export internal state.
|
|
|
|
|
|
|
|
Used by WS initialization and updates.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
(tuple): (base, team)
|
|
|
|
- team for team users.
|
|
|
|
- base for others.
|
|
|
|
|
|
|
|
"""
|
|
|
|
base = {
|
|
|
|
'is_open': self.is_open,
|
|
|
|
'last_update': self.last_update,
|
|
|
|
}
|
|
|
|
restrict = {
|
|
|
|
'raw_open': self.raw_open,
|
|
|
|
'force_close': self.force_close,
|
|
|
|
}
|
|
|
|
return base, {**base, **restrict}
|
|
|
|
|
|
|
|
def export(self, user=None):
|
|
|
|
"""Export internal state.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
base, team = self._export()
|
|
|
|
return team if user is None or kfet_is_team(user) else base
|
|
|
|
|
|
|
|
def send_ws(self):
|
|
|
|
"""Send internal state to websocket channels."""
|
|
|
|
from .consumers import OpenKfetConsumer
|
|
|
|
base, team = self._export()
|
|
|
|
OpenKfetConsumer.group_send('kfet.open.base', base)
|
|
|
|
OpenKfetConsumer.group_send('kfet.open.team', team)
|
|
|
|
|
|
|
|
|
|
|
|
kfet_open = OpenKfet()
|