Skip to content

Commit

Permalink
User-specific annotation endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
benwbrum committed Sep 3, 2019
1 parent 09e3e0b commit 50e0f98
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
45 changes: 27 additions & 18 deletions apps/readux/annotations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.serializers import serialize
from django.http import JsonResponse
from django.views import View
Expand All @@ -6,33 +7,41 @@
from ..iiif.canvases.models import Canvas
import json
import uuid
from ..users.models import User


class Annotations(ListView):

def get_queryset(self):
if self.request.user.is_authenticated:
def get_queryset(self, owner):
if self.request.user.is_authenticated and owner == self.request.user:
return UserAnnotation.objects.filter(
owner=self.request.user,
owner=owner,
canvas=Canvas.objects.get(pid=self.kwargs['canvas'])
)
return None

def get(self, request, *args, **kwargs):
annotations = self.get_queryset()
if annotations is not None:
for anno in annotations:
return JsonResponse(
json.loads(
serialize(
'annotation',
annotations,
# version=kwargs['version'],
is_list = True
)
),
safe=False,
status=200
)
username = kwargs['username']
try:
owner = User.objects.get(username=username)
annotations = self.get_queryset(owner)
if annotations is not None:
for anno in annotations:
return JsonResponse(
json.loads(
serialize(
'annotation',
annotations,
# version=kwargs['version'],
is_list = True
)
),
safe=False,
status=200
)
except ObjectDoesNotExist:
# attempt to get annotations for non-existent user
return JsonResponse(status=404, data={"User not found.": username})
return JsonResponse(status=200, data={})

class AnnotationCrud(View):
Expand Down
15 changes: 13 additions & 2 deletions apps/readux/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,16 @@ def rando_anno(self):

def test_get_user_annotations_unauthenticated(self):
self.create_user_annotations(5, self.user_a)
assert len(UserAnnotation.objects.all()) == 5
kwargs = {'username': 'readux', 'volume': 'readux:st7r6', 'canvas': 'fedora:emory:5622'}
url = reverse('user_annotations', kwargs=kwargs)
response = self.client.get(url)
assert len(UserAnnotation.objects.all()) == 5
assert response.status_code == 404
kwargs = {'username': self.user_a_uname, 'volume': 'readux:st7r6', 'canvas': 'fedora:emory:5622'}
url = reverse('user_annotations', kwargs=kwargs)
response = self.client.get(url)
annotation = self.load_anno(response)
assert len(annotation) == 0
assert response.status_code == 200

def test_mirador_svg_annotation_creation(self):
Expand Down Expand Up @@ -173,13 +179,18 @@ def test_get_only_users_user_annotations(self):
self.create_user_annotations(5, self.user_b)
self.create_user_annotations(4, self.user_a)
self.client.login(username=self.user_b_uname, password=self.user_b_passwd)
kwargs = {'username': 'marvin', 'volume': 'readux:st7r6', 'canvas': 'fedora:emory:5622'}
kwargs = {'username': self.user_b_uname, 'volume': 'readux:st7r6', 'canvas': 'fedora:emory:5622'}
url = reverse('user_annotations', kwargs=kwargs)
response = self.client.get(url)
annotation = self.load_anno(response)
assert len(annotation) == 5
assert response.status_code == 200
assert len(UserAnnotation.objects.all()) == 9
kwargs = {'username': self.user_a_uname, 'volume': 'readux:st7r6', 'canvas': 'fedora:emory:5622'}
url = reverse('user_annotations', kwargs=kwargs)
response = self.client.get(url)
annotation = self.load_anno(response)
assert len(annotation) == 0

def test_update_user_annotation(self):
self.create_user_annotations(1, self.user_a)
Expand Down

0 comments on commit 50e0f98

Please sign in to comment.