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())
def publish_message(self, message, expire=None): """ Publish a ``message`` on the subscribed channel on the Redis datastore. ``expire`` sets the time in seconds, on how long the message shall additionally of being published, also be persisted in the Redis datastore. If unset, it defaults to the configuration settings ``WS4REDIS_EXPIRE``. """ # noinspection PyUnusedLocal expire = expire if not isinstance(message, RedisMessage): raise ValueError('message object is not of type RedisMessage') if self._publishers != self.internal_publishers: return # noinspection PyBroadException try: message_dict = json.loads(message.decode('utf-8'), cls=get_signal_decoder()) df_call(message_dict['signal'], self.request, sharing=None, from_client=True, kwargs=message_dict['options']) except ApiException as e: df_call('df.messages.error', self.request, sharing=SESSION, from_client=True, kwargs={'html': _('Error: %(msg)s') % {'msg': force_text(e)}}) except Exception as e: df_call('df.messages.error', self.request, sharing=SESSION, from_client=True, kwargs={'html': _('Invalid websocket message: %(msg)s.') % {'msg': force_text(e)}})