Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement GET API #616

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/annotation_project/annotation_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

urlpatterns = [
path('admin/', admin.site.urls),
path('annotations/', include("annotations.urls"))
path('', include("annotations.urls"))

]
51 changes: 49 additions & 2 deletions project/annotation_project/annotations/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
from django.test import TestCase
from django.test import TestCase, Client
from django.urls import reverse
from datetime import datetime

from .models import *

class AnnotationGetTest(TestCase):
def setUp(self):
source = Source.objects.create(uri='http://example.com/source1')
body = Body.objects.create(value='Annotation Test Content')
creator = Creator.objects.create(name='testcreator@example.com')
selector = Selector.objects.create(start=0, end=5, source=source)
self.annotation = Annotation.objects.create(
body=body,
target=selector,
creator=creator,
created=datetime.now(),
)

def test_get_annotation_by_id(self):
#Test the annotation response format
client = Client()

url = reverse('get_annotation_by_id', args=[self.annotation.id])

response = client.get(url)
self.assertEqual(response.status_code, 200)

response = response.json()
self.assertEqual(response['@context'], 'http://www.w3.org/ns/anno.jsonld')
self.assertTrue(response['id'].endswith(f'annotation{self.annotation.id}'))
self.assertEqual(response['type'], self.annotation.type)
self.assertEqual(response['body'], {
'type': self.annotation.body.type,
'format': self.annotation.body.format,
'language': self.annotation.body.language,
'value': self.annotation.body.value,
})
self.assertEqual(response['target'], {
'id': self.annotation.target.source.uri,
'type': 'text',
'selector': {
'type': self.annotation.target.type,
'start': self.annotation.target.start,
'end': self.annotation.target.end,
}
})
self.assertTrue(response['creator']['id'], self.annotation.creator.name)
self.assertIn('created', response)

# Create your tests here.
3 changes: 3 additions & 0 deletions project/annotation_project/annotations/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from .views import *

urlpatterns = [
path('get_annotation/', matched_annotations_get_view, name='get_annotation'),
path('annotation<annotation_id>/', get_annotation_by_id, name='get_annotation_by_id'),

]


65 changes: 65 additions & 0 deletions project/annotation_project/annotations/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
from django.shortcuts import render
from django.http import JsonResponse

from .models import *


def serialize_annotation(annotation, scheme, host):
return {
'@context': 'http://www.w3.org/ns/anno.jsonld',
'id': f'{scheme}://{host}/annotation{annotation.id}',
'type': annotation.type,
'body': {
'type': annotation.body.type,
'format': annotation.body.format,
'language': annotation.body.language,
'value': annotation.body.value,
},
'target': {
'id': annotation.target.source.uri,
'type': 'text',
'selector': {
'type': annotation.target.type,
'start': annotation.target.start,
'end': annotation.target.end,
},
},
'creator': {
'id': f'http://13.51.205.39/profile/{annotation.creator.name}',
'type': annotation.creator.type,
},
'created': annotation.created,
}


def matched_annotations_get_view(request):
source_uri = request.GET.get('source')
creator_name = request.GET.get('creator')

filtering_conditions = {}

if source_uri:
filtering_conditions['target__source__uri__iexact'] = source_uri
if creator_name:
filtering_conditions['creator__name__iexact'] = creator_name
try:
matched_annotations = Annotation.objects.filter(**filtering_conditions)

if len(matched_annotations) == 0:
return JsonResponse({'message': 'No annotation found!'}, status=404)

matched_data = [
serialize_annotation(annotation, request.scheme, request.get_host()) for annotation in matched_annotations
]

return JsonResponse(matched_data, status=200, safe=False)
except Exception as e:
return JsonResponse({'message': str(e)}, status=500)


def get_annotation_by_id(request, annotation_id):
try:
annotation = Annotation.objects.get(id=annotation_id)

if not annotation:
return JsonResponse({'message': 'No annotation found!'}, status=404)
return JsonResponse(data = serialize_annotation(annotation, request.scheme, request.get_host()), status=200)
except Exception as e:
return JsonResponse({'message': str(e)}, status=500)