コード例 #1
0
def update_tile_meta(request, tilePrefix, tile_key):  # pragma: no cover
    cachedTile = json.loads(getCache().redis.get(tilePrefix))
    options = json.loads(request.POST.get('value', None))
    try:
        for metaItem in options.keys():
            cachedTile['meta'][metaItem] = options[metaItem]
    except Exception as e:
        return HttpResponseBadRequest(f'Invalid Json data: {e}')
    getCache().set(tilePrefix, json.dumps(cachedTile))
    return HttpResponse(f'{tile_key} data updated successfully.')
コード例 #2
0
def is_meta_present_in_request(meta, tile_id):  # pragma: no cover
    """ Update the meta(config) of a tile(widget) """
    if meta is not None:
        tilePrefix = getRedisPrefix(tile_id)
        if getCache().redis.exists(tilePrefix) is not None:
            cachedTile = json.loads(getCache().redis.get(tilePrefix))
            metaTile = cachedTile['meta']['options'] if 'options' in cachedTile['meta'] else cachedTile['meta']
            update_tile_data_from_redis(metaTile, json.loads(meta), None)
            getCache().set(tilePrefix, json.dumps(cachedTile), sendToWS=False)
            print(f"(+) Meta of tile {tilePrefix} has been updated")
            return True
    print(f"(+) Meta of tile not present")
    return False
コード例 #3
0
def get_tile(request, tile_key, unsecured=False):  # pragma: no cover
    """ Return Json from redis for tile_key """
    if not checkAccessToken(method='GET', request=request, unsecured=unsecured):
        return HttpResponse('API KEY incorrect', status=401)
    redis = getCache().redis
    if redis.exists(tile_key):
        return HttpResponse(redis.get(tile_key))
    return HttpResponseBadRequest(f'{tile_key} key does not exist.')
コード例 #4
0
def delete_tile(request, tile_key, unsecured=False):  # pragma: no cover
    """ Delete in redis """
    if not checkAccessToken(method='DELETE', request=request, unsecured=unsecured):
        return HttpResponse('API KEY incorrect', status=401)
    redis = getCache().redis
    if redis.exists(tile_key):
        redis.delete(tile_key)
        return HttpResponse('Tile\'s data deleted.')
    return HttpResponseBadRequest(f'{tile_key} key does not exist.')
コード例 #5
0
def save_tile_ToRedis(tile_id, tile_template, data):  # pragma: no cover
    cache = getCache()
    tilePrefix = getRedisPrefix(tile_id)
    if not cache.redis.exists(tilePrefix) and DEBUG:  # if tile don't exist, create it with template, DEBUG mode only
        buildFakeDataFromTemplate(tile_id, tile_template, cache)
    cachedTile = json.loads(cache.redis.get(tilePrefix))
    cachedTile['data'] = update_tile_data_from_redis(cachedTile['data'], json.loads(data), tile_template)
    cachedTile['modified'] = getIsoTime()
    cachedTile['tile_template'] = tile_template
    cache.set(tilePrefix, json.dumps(cachedTile))
    return HttpResponse(f'{tile_id} data updated successfully.')
コード例 #6
0
def meta_api(request, tile_key, unsecured=False):  # pragma: no cover
    """ Update the meta(config) of a tile(widget) """
    if request.method == 'POST':
        if not checkAccessToken(
                method='POST', request=request, unsecured=unsecured):
            return HttpResponse('API KEY incorrect', status=401)
        tilePrefix = getRedisPrefix(tile_key)
        if not getCache().redis.exists(tilePrefix):
            return HttpResponseBadRequest(
                f'{tile_key} is not present in cache')
        return update_tile_meta(request, tilePrefix, tile_key)
    raise Http404
コード例 #7
0
def sanity_push_api(request, unsecured):
    """ Test token, all data present, correct tile_template and tile_id present in cache """
    if not checkAccessToken(method='POST', request=request, unsecured=unsecured):
        return False, HttpResponse('API KEY incorrect', status=401)
    HttpData = request.POST
    if not HttpData.get('tile_id', None) or not HttpData.get('tile_template', None) or \
            not HttpData.get('data', None):
        return False, HttpResponseBadRequest('Missing data')
    if HttpData.get('tile_template', None) not in ALLOWED_TILES:
        tile_template = HttpData.get('tile_template', None)
        return False, HttpResponseBadRequest(f'tile_template: {tile_template} is unknow')
    cache = getCache()
    tilePrefix = getRedisPrefix(HttpData.get('tile_id', None))
    if not cache.redis.exists(tilePrefix) and not DEBUG:
        return False, HttpResponseBadRequest(f'tile_id: {tilePrefix} is unknow')
    return True, HttpData
コード例 #8
0
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json
from src.tipboard.app.cache import getCache
from src.tipboard.app.properties import COLORS, JS_LOG_LEVEL, LOG
from src.tipboard.app.utils import getRedisPrefix, getTimeStr
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync

cache = getCache()
tipboard_helpers = {
    'color': COLORS,
    'log_level': JS_LOG_LEVEL,
}


class ChatConsumer(WebsocketConsumer):
    """Handles client connections on web sockets and listens on a Redis
    subscription."""
    def connect(self):
        #self.channel_name = "events"
        async_to_sync(self.channel_layer.group_add)("event", self.channel_name)
        if LOG:
            print(
                f"{getTimeStr()} (+) WS: New client with channel:{self.channel_name}",
                flush=True)
        self.accept()
        for tile_id in cache.listOfTilesCached():
            self.update_tile_receive(tile_id=tile_id)

    def disconnect(self, close_code):