Skip to main content

Easy to use class mixins for Django Rest Framework and Pusher to keep your API realtime.

Project description

DRF Model Pusher

Easy to use class mixins for Django Rest Framework and Pusher to keep your API realtime.

Installation

Download from PyPI:

pip install drf_model_pusher

Configuration

Settings Config

In your settings set your Pusher App Id and credentials, the cluster is optional

PUSHER_APP_ID=""  
PUSHER_KEY=""  
PUSHER_SECRET=""
PUSHER_CLUSTER=""  

Application Config

Edit your applications AppConfig and import the connect_pusher_views method and run it in the AppConfig's ready method.

from django.apps import AppConfig
from drf_model_pusher.config import connect_pusher_views

class MyAppConfig(AppConfig):
    def ready(self):
        connect_pusher_views()

Implement Pusher Backends

Define some PusherBackends for your models and serializers. The PusherBackend class just needs to define a serializer_class attribute which inherits from ModelSerializer.

from django.db.models import Model
from rest_framework.serializers import ModelSerializer
from drf_model_pusher.backends import PusherBackend, PrivatePusherBackend

class MyModel(Model):
    pass

class MyModelSerializer(ModelSerializer):
    class Meta:
        model = MyModel

class MyModelPrivateSerializer(ModelSerializer):
    class Meta:
        model = MyModel

class MyModelPusherBackend(PusherBackend):
    serializer_class = MyModelSerializer

class MyModelPrivatePusherBackend(PrivatePusherBackend):
    serializer_class = MyModelPrivateSerializer

Then import your pusher backends in your AppConfig to register them.

At some point I think it would be worth auto-loading from some predefined paths, such as scanning for a pusher_backends.py file in each app and importing them automatically.

from django.apps import AppConfig
from drf_model_pusher.config import connect_pusher_views

class MyAppConfig(AppConfig):
    def ready(self):
        connect_pusher_views()
        #  The line below imports the backends which in turn register's them in the global registry.
        from example.pusher_backends import MyModelPusherBackend, MyModelPrivatePusherBackend

Implement Views

Add the ModelPusherViewMixin mixin class to your views.

from rest_framework.viewsets import ModelViewSet
from drf_model_pusher.views import ModelPusherViewMixin

class MyModelViewSet(ModelPusherViewMixin, ModelViewSet):
    serializer_class = MyModelSerializer

    def get_pusher_channel(self):
        return "<channel_id>"

Common Issues

Unregistered Backends

If you have followed the above steps correctly and your backends are not registering, your app config may not be running it's ready method. To force this, in your apps __init__.py add the line default_app_config = 'myapp.apps.MyAppConfig'

Contributions

It's early days, but if you'd like to report any issues or work on an improvement then please check for any similar existing issues before you report them.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_model_pusher-0.1.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

drf_model_pusher-0.1.1-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file drf_model_pusher-0.1.1.tar.gz.

File metadata

File hashes

Hashes for drf_model_pusher-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3839f3fa798055e60c2f1b66362eca15e1bedb75089e875d2c9ddbf284f4e3b8
MD5 c92c48a298c249aff240e0489a39b5f9
BLAKE2b-256 b78edd83c41b340c5d47c480ed628cc3438558af498c6f1914b45bec2760a96d

See more details on using hashes here.

Provenance

File details

Details for the file drf_model_pusher-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for drf_model_pusher-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f6439e19ba9bca365da8058588ed7abf6a78d54b5161ac30b7645a9bfbae7024
MD5 26320c6f601d0766379be2af64e4fd84
BLAKE2b-256 180f91c36387ce95b8c6ede920988a9dfe43691110f8a77147350b449f3f5623

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page