tg_pubsub package

Submodules

tg_pubsub.config module

tg_pubsub.config.get_extra_models()[source]
Get extra models to mark as listenable. This is useful if one needs to listen to changes

on a model that is coming from external apps so subclassing variant is impossible.

TG_PUBSUB_EXTRA_MODELS: list(config_path, ...)

Return type:dict
tg_pubsub.config.get_hello_packets()[source]

Get all packets to send right after doing websocket handshake

TG_PUBSUB_HELLO_PACKETS: List of import paths to callables that must return an instance of BaseMessage
Returns:
tg_pubsub.config.get_protocol_handler()[source]
tg_pubsub.config.get_protocol_handler_klass()[source]
tg_pubsub.config.get_pubsub_server_host()[source]
tg_pubsub.config.get_pubsub_server_ping_delta()[source]
tg_pubsub.config.get_pubsub_server_port()[source]

tg_pubsub.exceptions module

exception tg_pubsub.exceptions.IgnoreMessageException[source]

Bases: Exception

exception tg_pubsub.exceptions.InvalidMessageException[source]

Bases: Exception

tg_pubsub.messages module

class tg_pubsub.messages.BaseMessage(channels, data)[source]

Bases: object

MESSAGE_IDENTIFIER = 'base'
as_message()[source]
classmethod prepare_for_send(ws, data)[source]
publish()[source]

Publish message to redis queue

class tg_pubsub.messages.ModelChanged(klass, action, instance)[source]

Bases: tg_pubsub.messages.BaseMessage

MESSAGE_IDENTIFIER = 'model'
classmethod prepare_for_send(ws, data)[source]

tg_pubsub.models module

class tg_pubsub.models.ListenableBase[source]

Bases: object

Base mixin that declares the api for listenables

get_serializer(*args, **kwargs)[source]
get_serializer_class()[source]

Return the class to use for the serializer.

classmethod has_access(instance, user)[source]

Does the given user has access to the model instance. This can be used to only send model data to specific users.

(Note: The user may be anonymous)

Parameters:
  • instance – Model instance
  • user – User connected via pubsub
Return type:

bool

pubsub_get_model()[source]
classmethod pubsub_serialize(instance, serializer)[source]

Serialize the given model instance before sending it to the users.

Parameters:
  • instance – Model instance
  • serializer – Drf serializer
Returns:

serializer_class = None
serializer_fields = ('pk',)
classmethod should_notify(instance, action)[source]

Should the given instance send out a change notification. This can be used to limit publishes to only specific instances.

Parameters:
  • instance – Model instance
  • action – Action that triggered the notification (created, saved, deleted)
Return type:

bool

class tg_pubsub.models.ListenableModelMixin[source]

Bases: tg_pubsub.models.ListenableBase

Mixin that will mark model as ‘listenable’. Such models will send messages to Redis queue whenever they’re updated.

class tg_pubsub.models.ModelListenConfig[source]

Bases: tg_pubsub.models.ListenableModelMixin

Special class that can be used to mark external app models as listenable.

via: TG_PUBSUB_EXTRA_MODELS

model_path = None
pubsub_get_model()[source]
tg_pubsub.models.is_model_listenable(cls, action, instance)[source]
tg_pubsub.models.model_changed(cls, action, instance)[source]
tg_pubsub.models.model_post_save_handler(sender, instance, created, **kwargs)[source]
tg_pubsub.models.model_pre_delete_handler(sender, instance, **kwargs)[source]

tg_pubsub.protocol module

class tg_pubsub.protocol.AnonymousUserServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.AnyUserServerProtocol

WebSocketServerProtocol implementation that only allows anonymous users

has_permissions(request)[source]
class tg_pubsub.protocol.AnyUserServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.SessionRequiredServerProtocol

WebSocketServerProtocol implementation that allows any users (that provide a token)

get_handler_kwargs(path, get_header)[source]
has_permissions(request)[source]
class tg_pubsub.protocol.AuthenticatedUserServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.AnyUserServerProtocol

WebSocketServerProtocol implementation that only allows authenticated users

has_permissions(request)[source]
class tg_pubsub.protocol.FakeRequest(path, session_key)[source]

Bases: object

session
class tg_pubsub.protocol.RequestServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: websockets.server.WebSocketServerProtocol

WebSocketServerProtocol that gives handler a request-like object which might contain the session/user if token was provided.

TOKEN_PARAM = 'token'
get_handler_kwargs(path, get_header)[source]
class tg_pubsub.protocol.SessionRequiredServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.RequestServerProtocol

WebSocketServerProtocol which only allows handshakes with a valid token

get_handler_kwargs(path, get_header)[source]
class tg_pubsub.protocol.StaffUserServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.AuthenticatedUserServerProtocol

WebSocketServerProtocol implementation that only allows staff users

has_permissions(request)[source]
class tg_pubsub.protocol.SuperUserServerProtocol(ws_handler, ws_server, *, origins=None, subprotocols=None, extra_headers=None, **kwds)[source]

Bases: tg_pubsub.protocol.StaffUserServerProtocol

WebSocketServerProtocol implementation that only allows superusers

has_permissions(request)[source]

tg_pubsub.pubsub module

tg_pubsub.pubsub.create_redis_connection()[source]
tg_pubsub.pubsub.publish(channel, message)[source]

tg_pubsub.worker module

class tg_pubsub.worker.HandlerProtocol(ws, request)[source]

Bases: object

logging_key
open
ping()[source]
send(data)[source]
user
class tg_pubsub.worker.WebSocketHandler(ws, request)[source]

Bases: object

classmethod message_valid(message)[source]
ping()[source]
run()[source]
send_hello()[source]
send_on_change()[source]
tg_pubsub.worker.client_handler(ws, path, request=None)[source]
tg_pubsub.worker.run_pubsub_server(host, port)[source]

Module contents