Skip to content

Commit

Permalink
Merge ca10b51 into d25380d
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed May 3, 2020
2 parents d25380d + ca10b51 commit a891b2d
Show file tree
Hide file tree
Showing 29 changed files with 387 additions and 220 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ output/*/index.html
# Sphinx
docs/_build
.eggs
local.sqlite
32 changes: 12 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ language: python
sudo: false

python:
- 3.7
- 3.6
- 3.5
- 3.4
- 2.7

env:
matrix:
- DJANGO='django30'
- DJANGO='django22'
- DJANGO='django111'
- DJANGO='django110'
- DJANGO='django19'
- DJANGO='django18'
- TOXENV='pep8'
- TOXENV='isort'
- TOXENV='docs'
Expand All @@ -25,10 +23,9 @@ services:

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
install:
- "if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then export PYVER=py27; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == '3.4' ]]; then export PYVER=py34; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == '3.5' ]]; then export PYVER=py35; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then export PYVER=py35; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then export PYVER=py36; fi"
- "if [[ $TRAVIS_PYTHON_VERSION == '3.7' ]]; then export PYVER=py37; fi"
- "if [[ ${DJANGO}z != 'z' ]]; then export TOXENV=$PYVER-$DJANGO; fi"

# command to run tests, e.g. python setup.py test
Expand All @@ -42,26 +39,21 @@ after_success:

matrix:
exclude:
- python: 2.7
env: TOXENV='pep8'
- python: 2.7
env: TOXENV='isort'
- python: 2.7
env: TOXENV='docs'
- python: 3.4
- python: 3.5
env: TOXENV='pep8'
- python: 3.4
- python: 3.5
env: TOXENV='isort'
- python: 3.4
- python: 3.5
env: TOXENV='docs'
- python: 3.5
env: DJANGO='django30'
- python: 3.6
env: TOXENV='pep8'
- python: 3.5
- python: 3.6
env: TOXENV='isort'
- python: 3.5
- python: 3.6
env: TOXENV='docs'


cache:
directories:
- $HOME/.pip-accel
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Contributors

* Adam Chainz
* Daniel Rios
* Daniel Santos
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Usage

See https://django-knocker.readthedocs.io/en/latest/usage.html

Upgrade
--------

See https://django-knocker.readthedocs.io/en/latest/installation.html#upgrade for information
about upgrading from djang-knocker 0.3 (channels 1).


Features
--------

Expand Down
17 changes: 10 additions & 7 deletions cms_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import os

from tempfile import mkdtemp

HELPER_SETTINGS = dict(
ROOT_URLCONF='tests.example_app.urls',
INSTALLED_APPS=[
'channels',
'sekizai',
'meta',
'tests.example_app',
],
LANGUAGES=(
Expand All @@ -31,27 +31,30 @@
'hide_untranslated': False,
}
},
META_USE_SITES=True,
FILE_UPLOAD_TEMP_DIR=mkdtemp(),
META_SITE_PROTOCOL='http',
ASGI_APPLICATION='tests.example_app.routing.application',
TEST_RUNNER='app_helper.pytest_runner.PytestTestRunner',
CHANNEL_LAYERS={
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
'hosts': [('localhost', 6379)],
},
'ROUTING': 'tests.example_app.routing.channel_routing',
},
}
)


def run():
from djangocms_helper import runner
from app_helper import runner
runner.run('knocker')


def setup():
import sys
from djangocms_helper import runner
from app_helper import runner
runner.setup('knocker', sys.modules[__name__], use_cms=False)

if __name__ == "__main__":
Expand Down
14 changes: 4 additions & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os
import os
import sys

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand All @@ -21,18 +22,11 @@
sys.path.insert(0, parent)
sys.path.insert(0, os.path.join(parent, 'tests'))

import cms_helper
import knocker
import cms_helper # isort:skip
import knocker # isort:skip
cms_helper.setup()
import sphinx.environment
from docutils.utils import get_source_line


def _warn_node(self, msg, node, *args, **kwargs):
if not msg.startswith('nonlocal image URI found:'):
self._warnfunc(msg, '%s:%s' % get_source_line(node))

sphinx.environment.BuildEnvironment.warn_node = _warn_node
# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
Expand Down
104 changes: 78 additions & 26 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,94 @@
Installation
============

* Install it
* Install it:

$ pip install django-knocker
.. code-block:: bash
* Add it to ``INSTALLED_APPS`` with channels::
pip install django-knocker
INSTALLED_APPS = [
...
'channels,
'knocker',
...
]
* Add it to ``INSTALLED_APPS`` with channels:

* Load the ``knocker`` routing into channels configuration::
.. code-block:: python
CHANNEL_LAYERS={
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'CONFIG': {
'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
},
'ROUTING': 'myproject.routing.channel_routing',
},
}
INSTALLED_APPS = [
...
'channels,
'knocker',
...
]
* Load the ``knocker`` routing into channels configuration:

.. code-block:: python
CHANNEL_LAYERS={
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
}
},
}
ASGI_APPLICATION='myproject.routing.channel_routing',
Check `channels documentation`_ for more detailed information on ``CHANNEL_LAYERS`` setup.

.. _channels documentation: https://channels.readthedocs.io/en/latest/deploying.html

* Add to ``myproject.routing.channel_routing.py`` the knocker routes::
* Add to ``myproject.routing.channel_routing.py`` the knocker routes:

.. code-block:: python
# -*- coding: utf-8 -*-
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import path
from knocker.routing import channel_routing as knocker_routing
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('knocker/', knocker_routing),
])
),
})
.. _upgrade:


Upgrade
=======

Upgrade from channels 1 version of django-knocker require updating the configuration and minor changes

Configuration
-------------

* Discard existing configuration
* Rewrite the main router according to channels 2 specifications and include knocker router. Example:

.. code-block:: python
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter([
path('knocker/', knocker_routing),
])
),
})
API Changes
-----------

# -*- coding: utf-8 -*-
If you added a custom ``should_knock`` method, you must add the ``signal_type`` argument to match the current signature:

from channels import include
from knocker.routing import channel_routing as knocker_routing
.. code-block:: python
channel_routing = [
include(knocker_routing, path=r'^/notifications'),
]
def should_knock(self, signal_type, created=False):
...
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Methods
.. _django-hvad: https://github.com/KristianOellegaard/django-hvad
.. _django-parler: https://github.com/edoburu/django-parler
.. _django-meta: https://github.com/nephila/django-meta
.. _channels: https://github.com/andrewgodwin/channels
.. _channels: https://github.com/django/channels
.. _channels documentation: https://channels.readthedocs.io/en/latest/deploying.html
.. _sample app: https://github.com/nephila/django-knocker/tree/master/tests/example_app
.. _open an issue: https://github.com/nephila/django-knocker/issues
44 changes: 15 additions & 29 deletions knocker/consumers.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from channels import Group
from channels.sessions import channel_session
from channels.generic.websocket import JsonWebsocketConsumer


@channel_session
def ws_connect(message):
"""
Channels connection setup.
Register the current client on the related Group according to the language
"""
prefix, language = message['path'].strip('/').split('/')
gr = Group('knocker-{0}'.format(language))
gr.add(message.reply_channel)
message.channel_session['knocker'] = language
message.reply_channel.send({"accept": True})
class KnockerConsumer(JsonWebsocketConsumer):

@property
def groups(self):
"""
Attach the consumer to the selected language
"""
lang = self.scope['url_route']['kwargs'].get('language')
return 'knocker-%s' % lang,

@channel_session
def ws_receive(message):
"""
Currently no-op
"""
pass
def knocker_saved(self, event):
"""
This method handles messages sent to knocker.saved

@channel_session
def ws_disconnect(message):
"""
Channels connection close.
Deregister the client
"""
language = message.channel_session['knocker']
gr = Group('knocker-{0}'.format(language))
gr.discard(message.reply_channel)
:param event: event object
"""
self.send_json(content=event['message'])
Loading

0 comments on commit a891b2d

Please sign in to comment.