Esempio n. 1
0
def ws_call(signal_name, request, sharing, kwargs):
    """ Call a JS signal from the Python side.

    :param signal_name: name of signal
    :type signal_name:
    :param request: a SignalRequest with session_key and username
    :type request: :class:`djangofloor.decorators.SignalRequest`
    :param sharing: the required sharing. Can be SESSION, USER or BROADCAST or any dict which is valid for django-websockets-redis.
    :type sharing:
    :param kwargs: options to pass to the JS signal
    :type kwargs:
    :return: `None`
    """
    if isinstance(sharing, binary_type):
        sharing = force_text(sharing)
    if sharing == SESSION:
        sharing = {SESSION: [request.session_key, ]}
    elif sharing == USER:
        sharing = {USER: [request.username, ]}
    elif sharing == BROADCAST:
        sharing = {BROADCAST: True}
    if BROADCAST in sharing:
        sharing[BROADCAST] = True
    redis_publisher = RedisPublisher(facility=settings.FLOOR_WS_FACILITY, **sharing)
    msg = json.dumps({'signal': signal_name, 'options': kwargs}, cls=get_signal_encoder())
    redis_publisher.publish_message(RedisMessage(msg.encode('utf-8')))
Esempio n. 2
0
def signal_call(request, signal):
    """ Called by JS code when websockets are not available. Allow to call Python signals from JS.
    Arguments are passed in the request body, serialized as JSON.

    :param signal: name of the called signal
    :type signal: :class:`str`
    """
    import_signals()
    if request.body:
        kwargs = json.loads(request.body.decode('utf-8'), cls=get_signal_decoder())
    else:
        kwargs = {}
    try:
        result = df_call(signal, request, sharing=RETURN, from_client=True, kwargs=kwargs)
    except InvalidRequest:
        result = []
    return JsonResponse(result, safe=False, encoder=get_signal_encoder())