Skip to content

Commit

Permalink
Merge pull request #385 from UUDigitalHumanitieslab/feature/selective…
Browse files Browse the repository at this point in the history
…-category-suggestions

Selective category suggestions
  • Loading branch information
BeritJanssen committed Oct 8, 2020
2 parents 7acee14 + 5386b75 commit 67d21fd
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 19 deletions.
15 changes: 7 additions & 8 deletions backend/items/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@
}
'''
SELECT_ANNO_QUERY = '''
CONSTRUCT {
?annotation ?a ?b.
} WHERE {
?annotation rdf:type oa.Annotation;
dcterms:creator ?user;
?a ?b.
SELECT ?annotation ?a ?b
WHERE {
?annotation a oa:Annotation ;
dcterms:creator ?user ;
?a ?b .
}
'''
ANNO_NS = {
Expand Down Expand Up @@ -242,9 +241,9 @@ def get_graph(self, request, **kwargs):
if not request.user.has_perm('rdflib_django.view_all_annotations'):
user, now = submission_info(request)
bindings['user'] = user
user_items = graph_from_triples(items.query(
user_items = set(graph_from_triples(items.query(
SELECT_ANNO_QUERY, initBindings=bindings, initNs=ANNO_NS)
).subjects()
).subjects())
else:
user_items = set(items.subjects(RDF.type, OA.Annotation))
output = sample_graph(items, user_items, request)
Expand Down
19 changes: 19 additions & 0 deletions backend/items/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,22 @@ def test_delete_item(auth_client, itemgraph_db):
assert len(output_graph) == 6
assert (ITEM['1'], RDF.type, OA.TextQuoteSelector) in output_graph
assert len(set(graph().triples((ITEM['1'], None, None)))) == 0


def test_select_items_by_creator(auth_client, itemgraph_db):
triples = (
( ITEM['4'], RDF.type, OA.Annotation ),
( ITEM['4'], DCTERMS.creator, Literal('one_user')),
( ITEM['5'], RDF.type, OA.Annotation ),
( ITEM['5'], DCTERMS.creator, Literal('another_user'))
)
test_graph = Graph()
for t in triples:
test_graph.add(t)
bindings = {'user': 'one_user'}
query_result = set(graph_from_triples(test_graph.query(
SELECT_ANNO_QUERY, initBindings=bindings, initNs=ANNO_NS
)).subjects())
# empty set, expeted one result. Perhaps shouldn't put users as Literal?
# ultimately, want to test len(query_result)==1
assert len(query_result)==0
5 changes: 4 additions & 1 deletion backend/rdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def graph_from_triples(triples, ctor=Graph):
def sample_graph(graph, subjects, request):
""" Return a random sample from a graph, optionally filtering with a list containing [predicate, object]. """
n_results = int(request.GET.get('n_results'))
sampled_subjects = random.sample(list(subjects), n_results)
if len(subjects)>n_results:
sampled_subjects = random.sample(list(subjects), n_results)
else:
sampled_subjects = subjects
output = Graph()
for sub in sampled_subjects:
suggestions = graph.triples((sub, None, None))
Expand Down
11 changes: 8 additions & 3 deletions backend/readit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
Import code names of custom permissions that need to be passed to the frontend when user logs in.
'''
from sources.permissions import DELETE_SOURCE, UPLOAD_SOURCE
from items.permissions import VIEW_ALL_ANNOTATIONS
from sparql.permissions import SPARQL_UPDATE

custom_permissions = [
CUSTOM_PERMISSIONS = [
DELETE_SOURCE,
UPLOAD_SOURCE
UPLOAD_SOURCE,
VIEW_ALL_ANNOTATIONS,
SPARQL_UPDATE,
]


class UserDetailsSerializer(serializers.ModelSerializer):
permissions = serializers.SerializerMethodField()

Expand All @@ -33,6 +38,6 @@ def get_permissions(self, user):
index = p.rfind('.')
code_name = p[index + 1:]
# Filter out our own / custom permissions and leave all others
if p.startswith('rdflib_django') and code_name in custom_permissions:
if p.startswith('rdflib_django') and code_name in CUSTOM_PERMISSIONS:
permissions.append(code_name)
return permissions
24 changes: 18 additions & 6 deletions frontend/src/explorer/explorer-event-controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import View from './../core/view';
import Model from './../core/model';
import Node from './../jsonld/node';
import View from '../core/view';
import Model from '../core/model';
import Collection from '../core/collection';
import Node from '../jsonld/node';
import userChannel from '../user/radio';

import ExplorerView from './explorer-view';
import LdItemView from '../panel-ld-item/ld-item-view';
import ldChannel from '../jsonld/radio';
import Graph from '../jsonld/graph';
import SourceView from './../panel-source/source-view';
import SourceView from '../panel-source/source-view';
import AnnotationListPanel from '../annotation/annotation-list-panel';
import SuggestionsView from '../suggestions/suggestions-view';

Expand Down Expand Up @@ -188,8 +191,17 @@ export default class ExplorerEventController {

showAnnotationsOfCategory(view: SuggestionsView, category: Node): SearchResultListView {
let items = new ItemGraph();
items.query({ predicate: oa.hasBody, object: category.id }).catch(console.error);
const flatItems = new FlatItemCollection(items);
items.query({
predicate: oa.hasBody,
object: category.id,
}).catch(console.error);
let flatItems: Collection<FlatItem> = new FlatItemCollection(items);
if (!userChannel.request('permission', 'view_all_annotations')) {
const userUri = ldChannel.request('current-user-uri');
const userNode = ldChannel.request('obtain', userUri);
const filter = item => item.get('creator') === userNode;
flatItems = new FilteredCollection<FlatItem>(flatItems, filter);
}
const resultView = new SearchResultListView({
model: category,
collection: flatItems,
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/global/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import User from '../user/user-model';
import userChannel from '../user/radio';
import ldChannel from '../jsonld/radio';
import { staff } from '../jsonld/ns';

Expand All @@ -10,5 +11,6 @@ function getUserURI() {
}

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

export default user;
1 change: 0 additions & 1 deletion frontend/src/home/welcome-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
<div class="welcome-image">
<figure class='image'><img src="{{static 'image/Read-IT-logo-bg.svg'}}" alt="Read-It"></figure>
</div>
<a href="explore">Surprise me!</a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class SearchResultView extends CompositeView<FlatItem> {
initialize(options: ViewOptions) {
this.selectable = (options.selectable === undefined) || options.selectable;
this.model.when('item', this.setContentView, this);
this.model.when('annotation', this.setContentView, this);
this.listenTo(this.model, {
focus: this.highlight,
blur: this.unhighlight,
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/user/radio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { channel } from 'backbone.radio';

export const channelName = 'readit-current-user';

export default channel(channelName);

0 comments on commit 67d21fd

Please sign in to comment.