from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.functional import SimpleLazyObject
from django.utils.module_loading import import_string
from rest_framework import serializers
get_model = apps.get_model
[docs]def get_protocol_handler():
return getattr(settings, 'TG_PUBSUB_PROTOCOL_HANDLER', 'tg_pubsub.protocol.RequestServerProtocol')
[docs]def get_protocol_handler_klass():
return import_string(get_protocol_handler())
[docs]def get_pubsub_server_host():
return getattr(settings, 'TG_PUBSUB_HOST', 'localhost')
[docs]def get_pubsub_server_port():
return getattr(settings, 'TG_PUBSUB_PORT', 8090)
[docs]def get_pubsub_server_ping_delta():
return getattr(settings, 'TG_PUBSUB_PING_DELTA', 30)
[docs]def get_hello_packets():
""" 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
:return:
"""
from .messages import BaseMessage
packets = getattr(settings, 'TG_PUBSUB_HELLO_PACKETS', [])
res = []
assert isinstance(packets, (list, tuple))
for path in packets:
fn = import_string(path)
assert callable(fn)
instance = fn()
assert isinstance(instance, BaseMessage)
res.append(instance)
return res
extra_models = SimpleLazyObject(get_extra_models)