2017-06-21 07:08:28 +02:00
|
|
|
import json
|
2018-10-06 12:35:49 +02:00
|
|
|
import math
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2022-06-27 15:34:24 +02:00
|
|
|
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.core.cache import cache
|
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2017-06-12 01:51:10 +02:00
|
|
|
|
|
|
|
from .config import kfet_config
|
|
|
|
|
|
|
|
|
|
|
|
def to_ukf(balance, is_cof=False):
|
|
|
|
"""Convert euro to UKF."""
|
|
|
|
subvention = kfet_config.subvention_cof
|
|
|
|
grant = (1 + subvention / 100) if is_cof else 1
|
|
|
|
return math.floor(balance * 10 * grant)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
# Storage
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
class CachedMixin:
|
|
|
|
"""Object with cached properties.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
cached (dict): Keys are cached properties. Associated value is the
|
|
|
|
returned default by getters in case the key is missing from cache.
|
|
|
|
cache_prefix (str): Used to prefix keys in cache.
|
|
|
|
|
|
|
|
"""
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
cached = {}
|
2018-10-06 12:35:49 +02:00
|
|
|
cache_prefix = ""
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
def __init__(self, cache_prefix=None, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if cache_prefix is not None:
|
|
|
|
self.cache_prefix = cache_prefix
|
|
|
|
|
|
|
|
def cachekey(self, attr):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "{}__{}".format(self.cache_prefix, attr)
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if attr in self.cached:
|
|
|
|
return cache.get(self.cachekey(attr), self.cached.get(attr))
|
2018-10-06 12:35:49 +02:00
|
|
|
elif hasattr(super(), "__getattr__"):
|
2017-06-21 07:08:28 +02:00
|
|
|
return super().__getattr__(attr)
|
|
|
|
else:
|
|
|
|
raise AttributeError("can't get attribute")
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
if attr in self.cached:
|
|
|
|
cache.set(self.cachekey(attr), value)
|
2018-10-06 12:35:49 +02:00
|
|
|
elif hasattr(super(), "__setattr__"):
|
2017-06-21 07:08:28 +02:00
|
|
|
super().__setattr__(attr, value)
|
|
|
|
else:
|
|
|
|
raise AttributeError("can't set attribute")
|
|
|
|
|
|
|
|
def clear_cache(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
cache.delete_many([self.cachekey(attr) for attr in self.cached.keys()])
|
2017-06-21 07:08:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Consumers
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2022-06-27 15:34:24 +02:00
|
|
|
class DjangoJsonWebsocketConsumer(AsyncJsonWebsocketConsumer):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""Custom Json Websocket Consumer.
|
|
|
|
|
|
|
|
Encode to JSON with DjangoJSONEncoder.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
2022-06-27 15:34:24 +02:00
|
|
|
async def encode_json(cls, content):
|
2017-06-21 07:08:28 +02:00
|
|
|
return json.dumps(content, cls=DjangoJSONEncoder)
|
|
|
|
|
|
|
|
|
|
|
|
class PermConsumerMixin:
|
|
|
|
"""Add support to check permissions on consumers.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
perms_connect (list): Required permissions to connect to this
|
|
|
|
consumer.
|
|
|
|
|
|
|
|
message.user is appended as argument to each connection_groups method call.
|
|
|
|
|
|
|
|
"""
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-06-21 07:08:28 +02:00
|
|
|
http_user = True # Enable message.user
|
|
|
|
perms_connect = []
|
|
|
|
|
2022-06-27 15:34:24 +02:00
|
|
|
async def connect(self):
|
2017-06-21 07:08:28 +02:00
|
|
|
"""Check permissions on connection."""
|
2022-06-27 15:34:24 +02:00
|
|
|
self.user = self.scope["user"]
|
|
|
|
|
|
|
|
if self.user.has_perms(self.perms_connect):
|
|
|
|
await super().connect()
|
2017-06-21 07:08:28 +02:00
|
|
|
else:
|
2022-06-27 15:34:24 +02:00
|
|
|
await self.close()
|
|
|
|
|
|
|
|
# async def raw_connect(self, message, **kwargs):
|
|
|
|
# # Same as original raw_connect method of JsonWebsocketConsumer
|
|
|
|
# # We add user to connection_groups call.
|
|
|
|
# groups = self.connection_groups(user=message.user, **kwargs)
|
|
|
|
# for group in groups:
|
|
|
|
# await self.channel_layer.group_add(group, message.reply_channel)
|
|
|
|
# # Group(group, channel_layer=message.channel_layer).add(message.reply_channel)
|
|
|
|
# self.connect(message, **kwargs)
|
|
|
|
#
|
|
|
|
# async def raw_disconnect(self, message, **kwargs):
|
|
|
|
# # Same as original raw_connect method of JsonWebsocketConsumer
|
|
|
|
# # We add user to connection_groups call.
|
|
|
|
# groups = self.connection_groups(user=message.user, **kwargs)
|
|
|
|
# for group in groups:
|
|
|
|
# await self.channel_layer.group_discard(group, message.reply_channel)
|
|
|
|
# # Group(group, channel_layer=message.channel_layer).discard(
|
|
|
|
# # message.reply_channel
|
|
|
|
# # )
|
|
|
|
# self.disconnect(message, **kwargs)
|
|
|
|
#
|
|
|
|
# def connection_groups(self, user, **kwargs):
|
|
|
|
# """`message.user` is available as `user` arg. Original behavior."""
|
|
|
|
# return super().connection_groups(user=user, **kwargs)
|