Skip to content

Commit 75723d4

Browse files
committed
Merge branch 'poc-2' of https://github.com/commonsmachinery/catalog into poc-2
2 parents d226973 + 5d618cb commit 75723d4

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,8 @@ Add post:
107107
Delete source or post:
108108

109109
curl -v -X DELETE http://localhost:8004/works/1392318412903/sources/12345
110-
curl -v -X DELETE http://localhost:8004/works/1392318412903/posts/12345
110+
curl -v -X DELETE http://localhost:8004/works/1392318412903/posts/12345
111+
112+
Query SPARQL endpoint:
113+
114+
curl -g -H 'Accept: application/json' 'http://localhost:8004/sparql?query=SELECT+?s+?p+?o+WHERE+{?s+?p+?o}+LIMIT+50'

backend/catalog/store.py

+16
Original file line numberDiff line numberDiff line change
@@ -923,3 +923,19 @@ def get_complete_metadata(self, **kwargs):
923923

924924
result = temp_model.to_string(name=format, base_uri=None)
925925
return result
926+
927+
def query_sparql(self, query_string=None, results_format="json", **kwargs):
928+
query = RDF.Query(querystring=query_string, query_language="sparql")
929+
query_results = query.execute(self._model)
930+
if query.get_limit() < 0:
931+
query.set_limit(50)
932+
933+
if results_format == "json":
934+
format_uri = "http://www.mindswap.org/%7Ekendall/sparql-results-json/"
935+
elif results_format == "n3":
936+
format_uri = "http://www.w3.org/TeamSubmission/turtle/"
937+
elif results_format == "html":
938+
format_uri = "http://www.w3.org/1999/xhtml/"
939+
else:
940+
format_uri = "http://www.w3.org/TR/2008/REC-rdf-sparql-XMLres-20080115/"
941+
return query_results.to_string(format_uri=format_uri)

backend/catalog_backend.py

+33-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from celery import Celery
1010
from celery import Task
1111
from celery import subtask
12+
from celery.utils.dispatch import Signal
1213
from celery.signals import worker_shutdown
1314
from celery.signals import task_success
1415
from catalog.store import RedlandStore, EntryNotFound
@@ -29,6 +30,8 @@
2930
CELERY_IMPORTS = ("catalog.store", "catalog.log", "catalog_backend"),
3031
)
3132

33+
on_work_updated = Signal(providing_args=('task', 'update_subtask'))
34+
3235
class FileLock(object):
3336
def __init__(self, id, timeout=15, lockdir = '.'):
3437
self._filename = os.path.join(lockdir, 'lock-%s' % id)
@@ -111,7 +114,7 @@ def create_work(self, store='main', **kwargs):
111114
payload = json.dumps(work.get_data())
112115

113116
log_event.apply_async(args=('create_work', time, user, resource, payload))
114-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
117+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
115118

116119
return work.get_data()
117120

@@ -129,7 +132,7 @@ def update_work(self, store='main', **kwargs):
129132
payload = json.dumps(work.get_data())
130133

131134
log_event.apply_async(args=('update_work', time, user, resource, payload))
132-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
135+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
133136

134137
return work.get_data()
135138

@@ -149,7 +152,7 @@ def delete_work(self, store='main', **kwargs):
149152
payload = None
150153

151154
log_event.apply_async(args=('delete_work', time, user, resource, payload))
152-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
155+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
153156

154157
return kwargs
155158

@@ -183,7 +186,7 @@ def add_source(self, store='main', **kwargs):
183186
payload = json.dumps(source.get_data())
184187

185188
log_event.apply_async(args=('add_source', time, user, resource, payload))
186-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
189+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
187190

188191
return source.get_data()
189192
else:
@@ -195,7 +198,7 @@ def add_source(self, store='main', **kwargs):
195198
payload = json.dumps(source.get_data())
196199

197200
log_event.apply_async(args=('add_source', time, user, resource, payload))
198-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
201+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
199202

200203
return source.get_data()
201204

@@ -227,7 +230,7 @@ def update_source(self, store='main', **kwargs):
227230
payload = json.dumps(source.get_data())
228231

229232
log_event.apply_async(args=('update_source', time, user, resource, payload))
230-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
233+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
231234

232235
return source.get_data()
233236
else:
@@ -239,7 +242,7 @@ def update_source(self, store='main', **kwargs):
239242
payload = json.dumps(source.get_data())
240243

241244
log_event.apply_async(args=('update_source', time, user, resource, payload))
242-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
245+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
243246

244247
return source.get_data()
245248

@@ -264,7 +267,7 @@ def delete_source(self, store='main', **kwargs):
264267
payload = json.dumps(kwargs)
265268

266269
log_event.apply_async(args=('delete_source', time, user, resource, payload))
267-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
270+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
268271

269272
return kwargs
270273
else:
@@ -276,7 +279,7 @@ def delete_source(self, store='main', **kwargs):
276279
payload = json.dumps(kwargs)
277280

278281
log_event.apply_async(args=('delete_source', time, user, resource, payload))
279-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
282+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
280283

281284
return kwargs
282285

@@ -300,7 +303,7 @@ def add_post(self, store='main', **kwargs):
300303
payload = json.dumps(post.get_data())
301304

302305
log_event.apply_async(args=('add_post', time, user, resource, payload))
303-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
306+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
304307

305308
return post.get_data()
306309

@@ -330,7 +333,7 @@ def delete_post(self, store='main', **kwargs):
330333
payload = None
331334

332335
log_event.apply_async(args=('delete_post', time, user, resource, payload))
333-
if store != self.public_store: on_work_updated.apply_async(args=(self.subtask(kwargs=kwargs), ))
336+
if store != self.public_store: on_work_updated.send(sender=self, task=self, update_subtask=self.subtask(kwargs=kwargs))
334337

335338
store.delete_post(**kwargs)
336339
return kwargs
@@ -340,6 +343,10 @@ def get_complete_metadata(self, store='main', **kwargs):
340343
store = self.main_store if store == 'main' else self.public_store
341344
return store.get_complete_metadata(**kwargs)
342345

346+
@app.task(base=StoreTask, bind=True)
347+
def query_sparql(self, **kwargs):
348+
return self.public_store.query_sparql(**kwargs)
349+
343350
@app.task(base=StoreTask, bind=True)
344351
def log_event(self, type, time, user, resource, data):
345352
self.log.log_event(type, time, user, resource, data)
@@ -348,29 +355,28 @@ def log_event(self, type, time, user, resource, data):
348355
def query_events(self, type=None, user=None, time_min=None, time_max=None, resource=None, limit=100, offset=0):
349356
return self.log.query_events(type, user, time_min, time_max, resource, limit, offset)
350357

351-
@app.task(base=StoreTask, bind=True)
352-
def on_work_updated(self, update_subtask):
353-
task = update_subtask['task']
354-
kwargs = update_subtask['kwargs']
358+
@on_work_updated.connect
359+
def work_updated_handler(sender=None, task=None, update_subtask=None, **kwargs):
360+
subtask_kwargs = update_subtask['kwargs']
355361

356-
if task == "catalog_backend.create_work":
357-
visibility = kwargs.get('visibility', None)
362+
if sender == create_work:
363+
visibility = subtask_kwargs.get('visibility', None)
358364
if visibility != "public":
359365
return False
360-
elif task == "catalog_backend.update_work":
361-
work_id = kwargs['id']
362-
work = self.main_store.get_work(user=kwargs['user'], id=work_id)
363-
visibility = kwargs.get('visibility', work['visibility'])
366+
elif sender == update_work:
367+
work_id = subtask_kwargs['id']
368+
work = task.main_store.get_work(user=subtask_kwargs['user'], id=work_id)
369+
visibility = subtask_kwargs.get('visibility', work['visibility'])
364370
if visibility != "public":
365371
return False
366-
elif task == "catalog_backend.add_source" or \
367-
task == "catalog_backend.update_source" or \
368-
task == "catalog_backend.add_post":
369-
work_id = kwargs['work_id']
370-
work = self.main_store.get_work(user=kwargs['user'], id=work_id)
372+
elif sender == add_source or \
373+
sender == update_source or \
374+
sender == add_post:
375+
work_id = subtask_kwargs['work_id']
376+
work = task.main_store.get_work(user=subtask_kwargs['user'], id=work_id)
371377
if work['visibility'] != 'public':
372378
return False
373379

374380
# work is public or deleted, ok to re-run the updater task for public store now
375381
sub = subtask(update_subtask)
376-
sub.apply_async(kwargs={"store": "public"})
382+
sub.apply_async(kwargs={"store": "public"})

frontend/lib/rest.js

+25
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ function rest(app, localBackend, localBaseURI) {
6262
app.post('/works/:id/posts', postPost);
6363
app.delete('/works/:workID/posts/:postID', deletePost);
6464

65+
/* sparql */
66+
app.get('/sparql', getSPARQL);
67+
6568
return;
6669
};
6770

@@ -439,4 +442,26 @@ function putWork(req, res) {
439442
return;
440443
}
441444

445+
function getSPARQL(req, res) {
446+
var results_format;
447+
448+
if (req.get('Accept') == "application/json") {
449+
results_format = "json";
450+
} else {
451+
results_format = "xml";
452+
}
453+
454+
455+
function respond(result, err) {
456+
res.send(result);
457+
return;
458+
}
459+
var queryData = {
460+
query_string: req.query.query,
461+
results_format: results_format
462+
}
463+
call(res, queryData, 'query_sparql', null, respond);
464+
return;
465+
}
466+
442467
module.exports = rest;

0 commit comments

Comments
 (0)