How to use the djangochannelsrestframework.observer.base_observer.BaseObserver function in djangochannelsrestframework

To help you get started, we’ve selected a few djangochannelsrestframework examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hishnash / djangochannelsrestframework / djangochannelsrestframework / observer / model_observer.py View on Github external
UPDATE = "update"
    DELETE = "delete"


class UnsupportedWarning(Warning):
    """

    """


class ModelObserverInstanceState:
    # this is set when the instance is created
    current_groups: Set[str] = set()


class ModelObserver(BaseObserver):
    def __init__(self, func, model_cls: Type[Model], **kwargs):
        super().__init__(func)
        self._model_cls = None
        self.model_cls = model_cls  # type: Type[Model]
        self.id = uuid4()

    @property
    def model_cls(self) -> Type[Model]:
        return self._model_cls

    @model_cls.setter
    def model_cls(self, value: Type[Model]):
        was_none = self._model_cls is None
        self._model_cls = value

        if self._model_cls is not None and was_none:
github hishnash / djangochannelsrestframework / djangochannelsrestframework / observer / observer.py View on Github external
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from django.dispatch import Signal

from djangochannelsrestframework.observer.base_observer import BaseObserver


class Observer(BaseObserver):
    def __init__(self, func, signal: Signal = None, kwargs=None):
        super().__init__(func)
        if kwargs is None:
            kwargs = {}
        self.signal = signal
        self.signal_kwargs = kwargs
        self._serializer = None
        self.signal.connect(self.handle, **self.signal_kwargs)

    def handle(self, signal, *args, **kwargs):
        message = self.serialize(signal, *args, **kwargs)
        channel_layer = get_channel_layer()
        for group_name in self.group_names_for_signal(*args, message=message, **kwargs):
            async_to_sync(channel_layer.group_send)(group_name, message)

    def group_names(self, *args, **kwargs):