Skip to content

Commit

Permalink
[#2845] Don't use the global g object to store party state
Browse files Browse the repository at this point in the history
Until now we stored `partyline_connected` in the g object to avoid
re-registering. But when more than one application was created (ie
during tests) this meant that the second application didn't get the
partyline handler registered (as `g.partyline_connected` was already
True). Just store it in a per-app (or rather per-controller instance)
basis.

We should probably fix our tests though to use only one app.

Also renamed some vars for consistenct across apps
  • Loading branch information
amercader committed Mar 10, 2016
1 parent 11faea5 commit 7dee663
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ckan/config/middleware.py
Expand Up @@ -227,21 +227,21 @@ def __init__(self, import_name, *args, **kwargs):
self.add_url_rule('/__invite__/', endpoint='partyline',
view_func=self.join_party)
self.partyline = None
self.connected = False
self.partyline_connected = False
self.invitation_context = None
self.app_name = None # A label for the app handling this request
# (this app).

def join_party(self, request=flask_request):
# Bootstrap, turn the view function into a 404 after registering.
if self.connected:
if self.partyline_connected:
# This route does not exist at the HTTP level.
flask_abort(404)
self.invitation_context = _request_ctx_stack.top
self.partyline = request.environ.get(WSGIParty.partyline_key)
self.app_name = request.environ.get('partyline_handling_app')
self.partyline.connect('can_handle_request', self.can_handle_request)
self.connected = True
self.partyline_connected = True
return 'ok'

def can_handle_request(self, environ):
Expand Down
9 changes: 5 additions & 4 deletions ckan/controllers/partyline.py
Expand Up @@ -2,7 +2,7 @@
from pylons import config

import ckan.lib.base as base
from ckan.common import request, g
from ckan.common import request

from wsgi_party import WSGIParty, HighAndDry

Expand All @@ -17,15 +17,16 @@ class PartylineController(WSGIController):

def __init__(self, *args, **kwargs):
super(PartylineController, self).__init__(*args, **kwargs)
self.app = None # A reference to the main pylons app.
self.app_name = None # A reference to the main pylons app.
self.partyline_connected = False

def join_party(self):
if hasattr(g, 'partyline_connected'):
if self.partyline_connected:
base.abort(404)
self.partyline = request.environ.get(WSGIParty.partyline_key)
self.app_name = request.environ.get('partyline_handling_app')
self.partyline.connect('can_handle_request', self._can_handle_request)
setattr(g, 'partyline_connected', True)
self.partyline_connected = True
return 'ok'

def _can_handle_request(self, environ):
Expand Down

0 comments on commit 7dee663

Please sign in to comment.