How to use the djangochannelsrestframework.consumers.AsyncAPIConsumer 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 / tests / test_observer.py View on Github external
async def test_model_observer_custom_groups_wrapper(settings):
    settings.CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer",
            "TEST_CONFIG": {"expiry": 100500,},
        },
    }

    layer = channel_layers.make_test_backend(DEFAULT_CHANNEL_LAYER)

    class TestConsumer(AsyncAPIConsumer):
        async def accept(self, **kwargs):
            await self.user_change.subscribe(username="test")
            await super().accept()

        @model_observer(get_user_model())
        async def user_change(self, message, **kwargs):
            await self.send_json(message)

        @user_change.groups
        def user_change(self, instance=None, username=None, **kwargs):
            if username:
                yield "-instance-username-{}".format(slugify(username))
            else:
                yield "-instance-username-{}".format(instance.username)

    communicator = WebsocketCommunicator(TestConsumer, "/testws/")
github hishnash / djangochannelsrestframework / tests / test_consumer.py View on Github external
async def test_decorator():

    results = {}

    class AConsumer(AsyncAPIConsumer):
        @action()
        async def test_async_action(self, pk=None, **kwargs):
            results["test_action"] = pk
            return {"pk": pk}, 200

        @action()
        def test_sync_action(self, pk=None, **kwargs):
            results["test_sync_action"] = pk
            return {"pk": pk, "sync": True}, 200

    # Test a normal connection
    communicator = WebsocketCommunicator(AConsumer, "/testws/")

    connected, _ = await communicator.connect()

    assert connected
github hishnash / djangochannelsrestframework / tests / test_observer.py View on Github external
"TEST_CONFIG": {"expiry": 100500,},
        },
    }

    layer = channel_layers.make_test_backend(DEFAULT_CHANNEL_LAYER)

    class TestConsumer(AsyncAPIConsumer):
        async def accept(self, **kwargs):
            await self.user_change.subscribe()
            await super().accept()

        @model_observer(get_user_model())
        async def user_change(self, message, **kwargs):
            await self.send_json(message)

    class TestConsumer2(AsyncAPIConsumer):
        async def accept(self, **kwargs):
            await self.user_other.subscribe()
            await super().accept()

        @model_observer(get_user_model())
        async def user_other(self, message, **kwargs):
            await self.send_json(message)

    communicator1 = WebsocketCommunicator(TestConsumer, "/testws/")

    connected, _ = await communicator1.connect()

    assert connected

    communicator2 = WebsocketCommunicator(TestConsumer2, "/testws/")
github hishnash / djangochannelsrestframework / djangochannelsrestframework / consumers.py View on Github external
if errors is None:
            errors = []

        payload = {
            "errors": errors,
            "data": data,
            "action": action,
            "response_status": status,
            "request_id": request_id,
        }

        await self.send_json(payload)


class DjangoViewAsConsumer(AsyncAPIConsumer):

    view = None

    @property
    def dumpy_url_config(self):
        return

    # maps actions to HTTP methods
    actions = {}  # type: Dict[str, str]

    async def receive_json(self, content: typing.Dict, **kwargs):
        """
        Called with decoded JSON content.
        """
        # TODO assert format, if does not match return message.
        request_id = content.pop("request_id")
github hishnash / djangochannelsrestframework / djangochannelsrestframework / generics.py View on Github external
from typing import Dict, Type, Optional

from django.db.models import QuerySet, Model
from rest_framework.generics import get_object_or_404
from rest_framework.serializers import Serializer

from djangochannelsrestframework.consumers import AsyncAPIConsumer


class GenericAsyncAPIConsumer(AsyncAPIConsumer):
    """
    Base class for all other generic views.
    """

    # You'll need to either set these attributes,
    # or override `get_queryset()`/`get_serializer_class()`.
    # If you are overriding a view method, it is important that you call
    # `get_queryset()` instead of accessing the `queryset` property directly,
    # as `queryset` will get evaluated only once, and those results are cached
    # for all subsequent requests.

    queryset = None  # type: QuerySet
    serializer_class = None  # type: Type[Serializer]

    # If you want to use object lookups other than pk, set 'lookup_field'.
    # For more complex lookup requirements override `get_object()`.