Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Justas Azna committed May 19, 2014
2 parents c4b6f32 + 1dbaf74 commit 72301b6
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ install:
- pip install coveralls
before_install: bash bin/travis-before-install.sh
script:
- nosetests --with-coverage --cover-package=ckanext.realtime --with-pylons=links/test-core.ini
- rake jasmine:ci
- nosetests --with-coverage --cover-package=ckanext.realtime --with-pylons=links/test-core.ini
after_success: coveralls
4 changes: 3 additions & 1 deletion ckanext/realtime/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import ckan.lib.navl.dictization_functions
import ckan.plugins as p
import ckan.model as model
import ckanext.realtime as rt
import ckanext.realtime.db as db
from ckanext.realtime.event.event_dispatcher import EventDispatcher
Expand Down Expand Up @@ -61,7 +60,10 @@ def datastore_make_observable(context, data_dict):

p.toolkit.check_access('datastore_make_observable', context, data_dict)

if not _datastore_exists(data_dict):
return {'success': False}
db.add_datastore_notifier_trigger(data_dict['resource_id'])
return {'success': True}


def realtime_check_observable_datastore(context, data_dict):
Expand Down
26 changes: 19 additions & 7 deletions ckanext/realtime/message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def __init__(self, api_url, apikey):
message_handler_base.__init__(self, api_url, apikey)

def _datastore_subscribe(self, request, client):

def datastore_make_observable(resource_id, apikey):
url = urlparse.urljoin(self.api_url, 'datastore_make_observable')
payload = {'resource_id': resource_id}
r = requests.post(url,
data=jsonpickle.encode(payload),
headers={'Authorization': apikey,
'Content-Type': 'application/json'})
log.msg(r.text)
response = jsonpickle.decode(r.text)
return response['result']['success']

resource_id = request['resource_id']

# ask the CKAN API if the datastore is observable
Expand All @@ -154,16 +166,21 @@ def _datastore_subscribe(self, request, client):
# decide what to do
is_observable = response['result']['is_observable']
if is_observable == rt.YES_MESSAGE:
result = rt.SUCCESS_MESSAGE if self._add_subscribtion(resource_id, client) else rt.FAIL_MESSAGE
result = rt.SUCCESS_MESSAGE if self._add_subscribtion(resource_id, client) else rt.FAIL_MESSAGE

return {'type': 'datastoresubscribe',
'resource_id': request['resource_id'],
'result': result}

elif is_observable == rt.NO_MESSAGE:
if datastore_make_observable(request['resource_id'], self.wss_api_key):
result = rt.SUCCESS_MESSAGE
else:
result = rt.FAIL_MESSAGE

return {'type': 'datastoresubscribe',
'resource_id': request['resource_id'],
'result': rt.FAIL_MESSAGE}
'result': result}

elif is_observable == rt.NON_DATASTORE_MESSAGE:
return {'type': 'datastoresubscribe',
Expand All @@ -186,11 +203,6 @@ def _datastore_subscribe(self, request, client):
'resource_id': resource_id,
'result': result}

elif resource_id == 'nonObservableResource':
return {'type': 'datastoresubscribe',
'resource_id': resource_id,
'result': rt.FAIL_MESSAGE}

elif resource_id == 'nonDatastoreResource':
return {'type': 'datastoresubscribe',
'resource_id': resource_id,
Expand Down
2 changes: 1 addition & 1 deletion ckanext/realtime/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_check_non_datastore(self):
assert res['is_observable'] == rt.NON_DATASTORE_MESSAGE


def test_invalid_resource(self):
def test_check_invalid_resource(self):
# in observable?
tests.call_action_api(self.app, 'realtime_check_observable_datastore',
resource_id='invalidResource',
Expand Down
4 changes: 2 additions & 2 deletions ckanext/realtime/twisted/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def connect_event_processor(redis_host, redis_port,
'''

client_creator = protocol.ClientCreator(reactor,
CKANEventProcessorProtocol,
CkanEventProcessorProtocol,
websocket_factory)

d = client_creator.connectTCP(redis_host, redis_port)
Expand All @@ -38,7 +38,7 @@ def _connection_failure(error):
log.err(error)


class CKANEventProcessorProtocol(RedisSubscriber):
class CkanEventProcessorProtocol(RedisSubscriber):
'''txredis-powered subscriber for receiving realtime events from CKAN'''

def __init__(self, websocket_factory):
Expand Down
63 changes: 11 additions & 52 deletions client/examples/ex1.html → client/examples/ex1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

<title>ckanext-realtime Example 1</title>

<script src="jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="../CkanRT.js"></script>
<script src="../jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="../../CkanRT.js"></script>

<script language="javascript" type="text/javascript">
var output;
Expand All @@ -15,7 +15,7 @@
function initializeConnection() {
wsUri = jQuery("#wss").val();
rt = new CkanRT(wsUri);

// define CkanRT callbacks
rt.onDatastoreSubscribeResult = function(resourceId, status) {
writeToScreen('<span style="color: blue;">Subscribe to ' + resourceId + '. Status: ' + status + '</span>');
Expand All @@ -28,7 +28,7 @@
rt.onDatastoreEvent = function(event) {
writeToScreen('<span style="color: blue;">New event ' + JSON.stringify(event) + '</span>');
};

//end of CkanRT specific callbacks

//some WebSocket callbacks
Expand All @@ -52,16 +52,17 @@
output.appendChild(pre);
}


jQuery(document).ready(function() {
output = document.getElementById("output");

jQuery("input[name='connect']").click(function() {
initializeConnection();
});

jQuery("input[name='subscribe']").click(function() {
var resourceId = jQuery("#resource").val();

//subscribe to Observable Datastore
rt.datastoreSubscribe(resourceId);
});
Expand All @@ -72,33 +73,6 @@
//unsubscribe from Observable Datastore
rt.datastoreUnsubscribe(resourceId);
});

jQuery("input[name='make-observable']").click(function() {
var datastore = jQuery("#datastore").val();
var apikey = jQuery("#apikey").val();
var apiRoot = jQuery("#api").val();

var data = {
resource_id : datastore,
};

writeToScreen('<span style="color: blue;">Make ' + datastore + ' observable. </span>');

//make an ordinary datastore into an Observable Datastore
jQuery.ajax({
url : apiRoot + 'datastore_make_observable',
type : 'POST',
beforeSend : function(request) {
request.setRequestHeader("Authorization", apikey);
},
data : JSON.stringify(data),
dataType : 'application/json',
success : function(data) {
console.log(data);
}
});

});
});

</script>
Expand All @@ -112,27 +86,12 @@ <h2>ckanext-realtime Example 1</h2>
<br>
<br>

<label>1. In order to receive events from datastores you have to first make them "Observable Datastores" (call datastore_make_observable Action API function which inserts a notifier trigger on the particular datastore). Enter datastore resource ID:</label>
<br>
<input type="text" name="datastore" id="datastore">
<br>
<label>Action API Key:</label><br>
<input type="text" name="apikey" id="apikey"><br>
<label>and Action API Url:</label>
<br>
<input type="text" name="api" id="api" value="http://localhost:5000/api/3/action/">
<br>
<input type="button" name="make-observable" value="Make Observable">

<br>
<br>

<label>2. Here you can subscribe to Observable Datastores. Enter Resource ID for an Observable Datastore:</label>
<label>Here you can subscribe to realtime events of Datastores. Enter Resource ID of a Datastore:</label>
<br>
<input type="text" name="resource" id="resource">
<input type="button" name="subscribe" value="Subscribe">
<input type="button" name="unsubscribe" value="Unsubscribe">
<br><br>
<label>After you have subscribed to a datastore, try modifying data in it (CRUD) and observe the events in the log below</label>

<div id="output"></div>

</html>
<div id="output"></div>
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
# built documents.
#
# The short X.Y version.
version = '0.3'
version = '0.4'
# The full version, including alpha/beta/rc tags.
release = '0.3.2'
release = '0.4'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '0.3.2'
version = '0.4'

setup(
name='ckanext-realtime',
Expand Down
13 changes: 0 additions & 13 deletions spec/CkanRTSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,6 @@ describe("CkanRT", function() {
});
});

describe("when you subscribe to non-observable datastores", function() {
it("should fail", function(done) {
var resource = "nonObservableResource";
var expectedResult = "FAIL";
rt.onDatastoreSubscribeResult = function(resourceId, status) {
expect(resourceId).toEqual(resource);
expect(status).toEqual(expectedResult);
done();
};
rt.datastoreSubscribe(resource);
});
});

describe("when you subscribe to non-datastore resources", function() {
it("should fail with error", function(done) {
var resource = "nonDatastoreResource";
Expand Down

0 comments on commit 72301b6

Please sign in to comment.