Skip to content

Commit 5386b75

Browse files
committed
filtering annotations by user implemented
1 parent b20e594 commit 5386b75

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

backend/items/views.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@
6060
}
6161
'''
6262
SELECT_ANNO_QUERY = '''
63-
SELECT {
64-
?annotation ?a ?b.
65-
} WHERE {
66-
?annotation rdf:type oa.Annotation;
67-
dcterms:creator ?user;
68-
?a ?b.
63+
SELECT ?annotation ?a ?b
64+
WHERE {
65+
?annotation a oa:Annotation ;
66+
dcterms:creator ?user ;
67+
?a ?b .
6968
}
7069
'''
7170
ANNO_NS = {
@@ -242,10 +241,9 @@ def get_graph(self, request, **kwargs):
242241
if not request.user.has_perm('rdflib_django.view_all_annotations'):
243242
user, now = submission_info(request)
244243
bindings['user'] = user
245-
user_items = graph_from_triples(items.query(
244+
user_items = set(graph_from_triples(items.query(
246245
SELECT_ANNO_QUERY, initBindings=bindings, initNs=ANNO_NS)
247-
).subjects()
248-
print(user_items)
246+
).subjects())
249247
else:
250248
user_items = set(items.subjects(RDF.type, OA.Annotation))
251249
output = sample_graph(items, user_items, request)

backend/items/views_test.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,20 @@ def test_delete_item(auth_client, itemgraph_db):
138138
assert len(set(graph().triples((ITEM['1'], None, None)))) == 0
139139

140140

141-
# def test_select_items_by_creator(auth_client, itemgraph_db):
142-
# triples = (
143-
# ( ITEM['4'], RDF.type, OA.Annotation ),
144-
# ( ITEM['4'], DCTERMS.creator, Literal('one_user')),
145-
# ( ITEM['5'], RDF.type, OA.Annotation ),
146-
# ( ITEM['5'], DCTERMS.creator, Literal('another_user'))
147-
# )
148-
# test_graph = Graph()
149-
# for t in triples:
150-
# test_graph.add(t)
151-
# bindings = {'user': 'one_user'}
152-
# query_result = test_graph.query(
153-
# SELECT_ANNO_QUERY, initBindings=bindings, initNs=ANNO_NS
154-
# )
155-
# print(query_result)
141+
def test_select_items_by_creator(auth_client, itemgraph_db):
142+
triples = (
143+
( ITEM['4'], RDF.type, OA.Annotation ),
144+
( ITEM['4'], DCTERMS.creator, Literal('one_user')),
145+
( ITEM['5'], RDF.type, OA.Annotation ),
146+
( ITEM['5'], DCTERMS.creator, Literal('another_user'))
147+
)
148+
test_graph = Graph()
149+
for t in triples:
150+
test_graph.add(t)
151+
bindings = {'user': 'one_user'}
152+
query_result = set(graph_from_triples(test_graph.query(
153+
SELECT_ANNO_QUERY, initBindings=bindings, initNs=ANNO_NS
154+
)).subjects())
155+
# empty set, expeted one result. Perhaps shouldn't put users as Literal?
156+
# ultimately, want to test len(query_result)==1
157+
assert len(query_result)==0

backend/rdf/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def graph_from_triples(triples, ctor=Graph):
5959
def sample_graph(graph, subjects, request):
6060
""" Return a random sample from a graph, optionally filtering with a list containing [predicate, object]. """
6161
n_results = int(request.GET.get('n_results'))
62-
sampled_subjects = random.sample(list(subjects), n_results)
62+
if len(subjects)>n_results:
63+
sampled_subjects = random.sample(list(subjects), n_results)
64+
else:
65+
sampled_subjects = subjects
6366
output = Graph()
6467
for sub in sampled_subjects:
6568
suggestions = graph.triples((sub, None, None))

0 commit comments

Comments
 (0)