Skip to content

Commit 67d21fd

Browse files
authored
Merge pull request #385 from UUDigitalHumanitieslab/feature/selective-category-suggestions
Selective category suggestions
2 parents 7acee14 + 5386b75 commit 67d21fd

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

backend/items/views.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@
6060
}
6161
'''
6262
SELECT_ANNO_QUERY = '''
63-
CONSTRUCT {
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,9 +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()
246+
).subjects())
248247
else:
249248
user_items = set(items.subjects(RDF.type, OA.Annotation))
250249
output = sample_graph(items, user_items, request)

backend/items/views_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,22 @@ def test_delete_item(auth_client, itemgraph_db):
136136
assert len(output_graph) == 6
137137
assert (ITEM['1'], RDF.type, OA.TextQuoteSelector) in output_graph
138138
assert len(set(graph().triples((ITEM['1'], None, None)))) == 0
139+
140+
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))

backend/readit/serializers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
Import code names of custom permissions that need to be passed to the frontend when user logs in.
1313
'''
1414
from sources.permissions import DELETE_SOURCE, UPLOAD_SOURCE
15+
from items.permissions import VIEW_ALL_ANNOTATIONS
16+
from sparql.permissions import SPARQL_UPDATE
1517

16-
custom_permissions = [
18+
CUSTOM_PERMISSIONS = [
1719
DELETE_SOURCE,
18-
UPLOAD_SOURCE
20+
UPLOAD_SOURCE,
21+
VIEW_ALL_ANNOTATIONS,
22+
SPARQL_UPDATE,
1923
]
2024

25+
2126
class UserDetailsSerializer(serializers.ModelSerializer):
2227
permissions = serializers.SerializerMethodField()
2328

@@ -33,6 +38,6 @@ def get_permissions(self, user):
3338
index = p.rfind('.')
3439
code_name = p[index + 1:]
3540
# Filter out our own / custom permissions and leave all others
36-
if p.startswith('rdflib_django') and code_name in custom_permissions:
41+
if p.startswith('rdflib_django') and code_name in CUSTOM_PERMISSIONS:
3742
permissions.append(code_name)
3843
return permissions

frontend/src/explorer/explorer-event-controller.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import View from './../core/view';
2-
import Model from './../core/model';
3-
import Node from './../jsonld/node';
1+
import View from '../core/view';
2+
import Model from '../core/model';
3+
import Collection from '../core/collection';
4+
import Node from '../jsonld/node';
5+
import userChannel from '../user/radio';
46

57
import ExplorerView from './explorer-view';
68
import LdItemView from '../panel-ld-item/ld-item-view';
9+
import ldChannel from '../jsonld/radio';
710
import Graph from '../jsonld/graph';
8-
import SourceView from './../panel-source/source-view';
11+
import SourceView from '../panel-source/source-view';
912
import AnnotationListPanel from '../annotation/annotation-list-panel';
1013
import SuggestionsView from '../suggestions/suggestions-view';
1114

@@ -188,8 +191,17 @@ export default class ExplorerEventController {
188191

189192
showAnnotationsOfCategory(view: SuggestionsView, category: Node): SearchResultListView {
190193
let items = new ItemGraph();
191-
items.query({ predicate: oa.hasBody, object: category.id }).catch(console.error);
192-
const flatItems = new FlatItemCollection(items);
194+
items.query({
195+
predicate: oa.hasBody,
196+
object: category.id,
197+
}).catch(console.error);
198+
let flatItems: Collection<FlatItem> = new FlatItemCollection(items);
199+
if (!userChannel.request('permission', 'view_all_annotations')) {
200+
const userUri = ldChannel.request('current-user-uri');
201+
const userNode = ldChannel.request('obtain', userUri);
202+
const filter = item => item.get('creator') === userNode;
203+
flatItems = new FilteredCollection<FlatItem>(flatItems, filter);
204+
}
193205
const resultView = new SearchResultListView({
194206
model: category,
195207
collection: flatItems,

frontend/src/global/user.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import User from '../user/user-model';
2+
import userChannel from '../user/radio';
23
import ldChannel from '../jsonld/radio';
34
import { staff } from '../jsonld/ns';
45

@@ -10,5 +11,6 @@ function getUserURI() {
1011
}
1112

1213
ldChannel.reply('current-user-uri', getUserURI);
14+
userChannel.reply('permission', user.hasPermission, user);
1315

1416
export default user;

frontend/src/home/welcome-template.hbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
<div class="welcome-image">
33
<figure class='image'><img src="{{static 'image/Read-IT-logo-bg.svg'}}" alt="Read-It"></figure>
44
</div>
5-
<a href="explore">Surprise me!</a>
65
</div>

frontend/src/search/search-results/search-result-base-view.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class SearchResultView extends CompositeView<FlatItem> {
2727
initialize(options: ViewOptions) {
2828
this.selectable = (options.selectable === undefined) || options.selectable;
2929
this.model.when('item', this.setContentView, this);
30+
this.model.when('annotation', this.setContentView, this);
3031
this.listenTo(this.model, {
3132
focus: this.highlight,
3233
blur: this.unhighlight,

frontend/src/user/radio.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { channel } from 'backbone.radio';
2+
3+
export const channelName = 'readit-current-user';
4+
5+
export default channel(channelName);

0 commit comments

Comments
 (0)