Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SWAMP_DRAGON_PUBLISH_ENABLED setting #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ This setting will override the default `localhost` host

This setting will override the default `9999` port

### ```SWAMP_DRAGON_PUBLISH_ENABLED```

If set to ```False``` it turns the publishing of changes off. Default value is ```True```.

## JavaScript settings (settings exposed to clients via JavaScript)

Expand Down
9 changes: 9 additions & 0 deletions swampdragon/connections/sockjs_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .. import route_handler
from ..sessions.sessions import get_session_store
from ..same_origin import set_origin_connection, test_origin
from django.utils.importlib import import_module
import json


Expand All @@ -28,6 +29,7 @@ def is_heartbeat_enabled():


class ConnectionMixin(object):

def to_json(self, data):
"""
If the message is a dict, return it.
Expand All @@ -44,6 +46,7 @@ def to_json(self, data):

class SubscriberConnection(ConnectionMixin, SockJSConnection):
pub_sub = None
user = None

def __init__(self, session):
super(SubscriberConnection, self).__init__(session)
Expand Down Expand Up @@ -83,6 +86,12 @@ def on_message(self, data):
if data == {'heartbeat': '1'}:
self.on_heartbeat()
return
middleware_classes = getattr(settings, 'SWAMP_DRAGON_MIDDLEWARE_CLASSES', None)
if middleware_classes:
for middleware_class in middleware_classes:
middleware_path, middleware_name = middleware_classes[0].rsplit('.', 1)
middleware = getattr(import_module(middleware_path), middleware_name)
middleware().process_request(self, data)
handler = route_handler.get_route_handler(data['route'])
handler(self).handle(data)
except Exception as e:
Expand Down
21 changes: 21 additions & 0 deletions swampdragon/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.utils.six import add_metaclass
from abc import abstractmethod, ABCMeta


@add_metaclass(ABCMeta)
class BasicMiddleware(object):

"""MiddleWare Interface for SwampDragon

Inheriting Classes should implement process_request method

Returns:
None (might change in future)
"""

def __init__(self):
pass

@abstractmethod
def process_request(self, connection, data):
pass
14 changes: 14 additions & 0 deletions swampdragon/mock_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from .middleware import BasicMiddleware


class TestMiddleware(BasicMiddleware):

"""
Middleware sets 'dummy' variable in the connection
"""

def __init__(self):
super(TestMiddleware, self).__init__()

def process_request(self, connection, data):
connection.dummy = True
9 changes: 8 additions & 1 deletion swampdragon/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db.models import ForeignKey
from django.conf import settings
from .pubsub_providers.base_provider import PUBACTIONS
from .model_tools import get_property
from .pubsub_providers.model_publisher import publish_model
Expand Down Expand Up @@ -77,7 +78,13 @@ def serialize(self):
return self._serializer.serialize()

def _publish(self, action, changed_fields=None):
publish_model(self, self._serializer, action, changed_fields)
try:
PUBLISH_ENABLED = getattr(settings, 'SWAMP_DRAGON_PUBLISH_ENABLED', True)

if PUBLISH_ENABLED:
publish_model(self, self._serializer, action, changed_fields)
except Exception:
pass # not the best practice, but we don't want anything to fail if Redis is unaccessible...

def save(self, *args, **kwargs):
if not self.pk:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_subscriber_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from swampdragon.route_handler import BaseRouter, UnexpectedVerbException
from swampdragon.testing.dragon_testcase import DragonTestCase
from swampdragon import route_handler
from django.conf import settings as django_settings
import uuid


class TestSession(object):

def __init__(self, is_open=True):
self.session_id = uuid.uuid4().hex
self.is_closed = is_open is False
Expand All @@ -28,7 +30,9 @@ def say_hello(self, **kwargs):


class TestSubscriberConnection(DragonTestCase):

def setUp(self):
django_settings.SWAMP_DRAGON_MIDDLEWARE_CLASSES = ['swampdragon.mock_middleware.TestMiddleware']
self.session = TestSession()
self.connection = SubscriberConnection(self.session)

Expand Down Expand Up @@ -72,3 +76,11 @@ def test_dict_to_json(self):
expected = {'key': 'value'}
actual = self.connection.to_json(expected)
self.assertEqual(expected, actual)

def test_middleware_processing(self):
route_handler.register(TestRouter)
data = {'verb': 'say_hello', 'route': TestRouter.get_name()}
with self.assertRaises(AttributeError):
self.connection.dummy
self.connection.on_message(data)
self.assertTrue(self.connection.dummy)