Django Logux integration engine https://logux.io/
Project description
Logux Django
Django Logux integration engine.
- Guide, recipes, and API
- Chat for any questions
- Issues and roadmap
- Projects inside Logux ecosystem
Installation
Install from PyPI
pip install logux-django
Install dev version from current master.
pip install -e git://github.com/logux/django.git#egg=logux_django
Add path(r'logux/', include('logux.urls')),
into your urls.py
Sets Logux settings in your settings.py
:
# Logux settings: https://logux.io/guide/starting/proxy-server/
LOGUX_CONTROL_SECRET = "secret"
LOGUX_URL = "http://localhost:31338"
LOGUX_AUTH_FUNC = your_auth_function # your_auth_function(user_id, token: str) -> bool
Storing passwords or secrets in settings.py
is bad practice. Use ENV.
For urls and settings examples, please checkout test_app
settings
Keep in mind: the path in your urls.py
(logux/
) and the LOGUX_CONTROL_SECRET
from the settings should be passed
into Logux Server by ENV as
LOGUX_BACKEND
and LOGUX_CONTROL_SECRET
respectively.
For example:
LOGUX_BACKEND=http://localhost:8000/logux/
LOGUX_CONTROL_SECRET=secret
Usage
Actions
For action
handling add logux_actions.py
file in your app, add ActionCommand
inheritors and implement all his
abstract methods.
Actions classes requirements:
- Set
action_type: str
- Implement all
ActionCommand
abstracts methods - Implement
resend
andprocess
methods if you need (optional) - import
logux
dispatcher:from logux.dispatchers import logux
- Register all your action handlers:
logux.actions.register(YourAction)
For example – User rename action handler:
from typing import Optional, Dict
from django.contrib.auth.models import User
from logux.core import ActionCommand, Meta, Action
from logux.dispatchers import logux
class RenameUserAction(ActionCommand):
""" Action Handler for example from https://logux.io/protocols/backend/examples/ """
action_type = 'user/rename'
def resend(self, action: Action, meta: Optional[Meta]) -> Dict:
return {'channels': [f'users/{action["user"]}']}
def access(self, action: Action, meta: Meta) -> bool:
# user can rename only himself
return action['user'] == int(meta.user_id)
def process(self, action: Action, meta: Optional[Meta]) -> None:
user = User.objects.get(pk=action['user'])
user.first_name = action['name']
user.save()
logux.actions.register(RenameUserAction)
Channels (Subscription)
For subsription
handling add logux_subsriptions.py
file in your app, and ChannelCommand
inheritors
and implement all his abstract methods.
Subscription classes requirements:
- Set
channel_pattern: str
– this is a regexp like Django's url's patters inurls.py
- Implement all
ChannelCommand
abstracts methods - import
logux
dispatcher:from logux.dispatchers import logux
- Register all your subscription handlers:
logux.channels.register(YourChannelCommand)
For example:
from django.contrib.auth.models import User
from logux.core import ChannelCommand, Action, Meta
from logux.dispatchers import logux
class UserChannel(ChannelCommand):
channel_pattern = r'^user/(?P<user_id>\w+)$'
def access(self, action: Action, meta: Meta) -> bool:
return self.params['user_id'] == meta.user_id
def load(self, action: Action, meta: Meta):
user = User.objects.get(pk=self.params['user_id'])
self.send_back(
{'type': 'user/name', 'user': 38, 'name': user.first_name}
)
logux.channels.register(UserChannel)
For more examples, please checkout test app
(tests/test_app)
Utils
logux.core.logux_add
logux_add(action: Action, raw_meta: Optional[Dict] = None) -> None
is low level API function to send any actions and meta into Logux server.
If raw_meta
is None
just empty Dict will be passed to Logux server.
Keep in mind, in the current version logux_add
is sync.
For more information: https://logux.io/node-api/#log-add
Development
Create dev environment
make venv
make install
make run
Type checking and linting:
make lint
Test:
make test
License
The package is available as open source under the terms of the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for logux_django-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d8c84c82423def2350d8bbb544b0d1243c21543a11bdbe847d0e4c811b595f2 |
|
MD5 | 6385190254875f04b2894b4612d2e9eb |
|
BLAKE2b-256 | 18b8b5331d3b3076077efa2da8a0cb8da64d096d0d0e940137578544c49cbb0b |