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_ID=""  

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.receivers 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.handlers 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 PushModelChanges mixin class to your views.

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

class MyModelViewSet(PushModelChanges, ModelViewSet):
    serializer_class = MyModelSerializer

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

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.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

drf_model_pusher-0.1.0-py2.py3-none-any.whl (5.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for drf_model_pusher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 52d4d332a99e61da402a6d5831bf54dc78dbfb49db3513e5ee0419fdccf04427
MD5 8e4fa3fea9057d075223d2f1637385ca
BLAKE2b-256 8ee37457bd921cab396bd24b149b68d2e1f786a68b5ac89194ea26d54d7d243a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for drf_model_pusher-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9e7f64cf74da1708c900b4ffbf35cdd002a092ca98b2edcee36310c5bb8ca6c2
MD5 da148fcd3901c46c426aba77d73ed629
BLAKE2b-256 e6963c9562a25ad2d2b99370359377f3dfe9c21f9642faedd695296e5accce12

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