DRF channels one-way WS
Simple one-way bindings for django-channels with some specific support for django-rest-framework serializers and websockets. I.e. enrich your existing API with push notifications.
Installation
pipenv install drf-channels-oneway-wsorpip install drf-channels-oneway-ws
Usage
Bindings
from django.db import models
from rest_framework import serializers
from channels_oneway.bindings import Binding
class Family(models.Model):
name = models.CharField(max_length=255)
class Bird(models.Model):
name = models.CharField(max_length=255)
family = models.ForeignKey('Family', models.CASCADE)
class BirdSerializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ('__all__')
class BirdBinding(Binding):
model = Bird
stream = 'birds'
serializer = BirdSerializer
@classmethod
def group_names(cls, instance):
return [instance.family.name]
class FamilyBinding(Binding):
"""
example of a binding not using a drf serializer
"""
model = Family
stream = 'bird-families'
@classmethod
def group_names(cls, instance):
return [instance.name]
def serialize_data(self, instance):
return {'id': instance.id, 'name': instance.name}
Now you make sure you have a WebsocketConsumer, which does something like self.channel_layer.group_add('thrushes', self.channel_name) in its connect coroutine.
Family.objects.create(name='thrushes') will then cause the following to be sent over the associated websocket:
{
"stream": "bird-families",
"payload": {
"action": "create",
"data": {"id": 1, "name": "thrushes"},
"model": "your_app.family",
"pk": 1
}
}
Upon modification ("action": "update") or deletion ("action": "delete") you will receive messages with an equal structure.
Registration
In order to let the bindings register their signals make sure they are imported at some point. In case you use a dedicated file, import it from AppConfig.ready just like your signals.
Helpers
In order to send a ws message from outside a binding, but using the same format (stream + payload) (and also the drf json encoder) use the async channels_oneway.utils.groupSend(group, stream, payload) or its sync equivalent groupSendSync.
Contributing
Installation
git clone git@github.com:evocount/drf-channels-oneway-ws.gitcd drf-channels-oneway-wspipenv install --dev
Running tests
pipenv run pytest --cov
License
This project is licensed under the MIT License.
Metadata
Release files for drf-channels-oneway-ws 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf-channels-oneway-ws-0.3.0.tar.gz | 6.6 kB | Without provenance | Details for drf-channels-oneway-ws-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_channels_oneway_ws-0.3.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for drf_channels_oneway_ws-0.3.0-py2.py3-none-any.whl |
Total release size: 13.4 kB
Release files / drf-channels-oneway-ws-0.3.0.tar.gz
| Download URL | drf-channels-oneway-ws-0.3.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fca8159c4d439fc02cda744d868feb7ecad1ff25d75f9bcd70756b9af17c03d6
|
|
BLAKE2b-256 checksum How to use checksums |
4d66da2d420473f8604dc4b3857ef6ead8bbc44a80c45112b401b1a7b9dcc616
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.4
|
Release files / drf_channels_oneway_ws-0.3.0-py2.py3-none-any.whl
| Download URL | drf_channels_oneway_ws-0.3.0-py2.py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3a6ebaec2e5985a5818b42c55090e0abf38f6f5e575595190d02bf940f7c8b1f
|
|
BLAKE2b-256 checksum How to use checksums |
f08761a937cf031bf8804a0fa51e417258dcfa132d58cef59a71853cd95f2a3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.4
|
Security for drf-channels-oneway-ws 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.