Commit 95765d5e by Oleg Marshev

Use annotator store in views directly.

parent 96f8e2de
...@@ -3,7 +3,8 @@ from notesserver.views import StatusView, AnnotationListView, AnnotationDetailVi ...@@ -3,7 +3,8 @@ from notesserver.views import StatusView, AnnotationListView, AnnotationDetailVi
urlpatterns = patterns('', # nopep8 urlpatterns = patterns('', # nopep8
url(r'^status/$', StatusView.as_view(), name='status'), url(r'^status/$', StatusView.as_view(), name='status'),
url(r'^$', 'notesserver.views.root', name='root'),
url(r'^annotations/$', AnnotationListView.as_view(), name='annotations'), url(r'^annotations/$', AnnotationListView.as_view(), name='annotations'),
url(r'^annotations/(?P<annotation_id>[a-zA-Z0-9_]+)$', AnnotationDetailView.as_view(), name='annotations_detail') url(r'^annotations/(?P<annotation_id>[a-zA-Z0-9_-]+)$', AnnotationDetailView.as_view(), name='annotations_detail')
) )
...@@ -2,12 +2,27 @@ import requests ...@@ -2,12 +2,27 @@ import requests
import json import json
from django.http import HttpResponse from django.http import HttpResponse
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse
from rest_framework import status from rest_framework import status
from rest_framework.permissions import AllowAny from rest_framework.permissions import AllowAny
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.decorators import api_view, permission_classes
from annotator.elasticsearch import ElasticSearch
from annotator.annotation import Annotation
CREATE_FILTER_FIELDS = ('updated', 'created', 'consumer', 'id')
UPDATE_FILTER_FIELDS = ('updated', 'created', 'user', 'consumer')
@api_view(['GET'])
@permission_classes([AllowAny])
def root(request):
"""
Root view.
"""
return Response({})
class StatusView(APIView): class StatusView(APIView):
...@@ -17,6 +32,9 @@ class StatusView(APIView): ...@@ -17,6 +32,9 @@ class StatusView(APIView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def get(self, request): def get(self, request):
"""
Service status.
"""
return Response({}) return Response({})
...@@ -27,20 +45,32 @@ class AnnotationListView(APIView): ...@@ -27,20 +45,32 @@ class AnnotationListView(APIView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def get(self, request): def get(self, request):
url = '{}annotations'.format(settings.ANNOTATOR_STORE) """
ann_store = requests.get(url) """
return Response(ann_store.json()) # TODO: get user.
user = None
conn = ElasticSearch()
annotations = Annotation.search(user)
return Response(annotations)
def post(self, request): def post(self, request, *args, **kwargs):
""" """
Create a new annotation. Create a new annotation.
""" """
conn = ElasticSearch()
url = '{}annotations'.format(settings.ANNOTATOR_STORE) if request.DATA is not None:
headers = {'Content-type': 'application/json'} annotation = Annotation(_filter_input(request.DATA, CREATE_FILTER_FIELDS))
annotation = requests.post(url, data=json.dumps(request.DATA), headers=headers)
refresh = self.kwargs.get('refresh') != 'false'
annotation.save(refresh=refresh)
location = reverse('annotations_detail', kwargs={'annotation_id': annotation['id']})
return Response(annotation, status=status.HTTP_201_CREATED, headers={'Location': location})
return Response(annotation.json(), status=status.HTTP_201_CREATED)
class AnnotationDetailView(APIView): class AnnotationDetailView(APIView):
""" """
...@@ -48,41 +78,60 @@ class AnnotationDetailView(APIView): ...@@ -48,41 +78,60 @@ class AnnotationDetailView(APIView):
""" """
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
UPDATE_FILTER_FIELDS = ('updated', 'created', 'user', 'consumer')
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
""" """
Get an existing annotation. Get an existing annotation.
""" """
annotation_id = self.kwargs.get('annotation_id') annotation_id = self.kwargs.get('annotation_id')
annotation = Annotation.fetch(annotation_id)
# TODO: get annotation from elasticsearch if not annotation:
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id) return Response(annotation, status=status.HTTP_404_NOT_FOUND)
annotation = requests.get(url)
return Response(annotation.json(), status=status.HTTP_404_NOT_FOUND) return Response(annotation)
def put(self, request, *args, **kwargs): def put(self, request, *args, **kwargs):
""" """
Update an existing annotation. Update an existing annotation.
""" """
annotation_id = self.kwargs.get('annotation_id') annotation_id = self.kwargs.get('annotation_id')
annotation = Annotation.fetch(annotation_id)
if not annotation:
return Response('Annotation not found! No update performed.', status=status.HTTP_404_NOT_FOUND)
# TODO: update annotation from elasticsearch if request.DATA is not None:
headers = {'Content-type': 'application/json'} updated = _filter_input(request.DATA, UPDATE_FILTER_FIELDS)
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id) updated['id'] = annotation_id # use id from URL, regardless of what arrives in JSON payload.
annotation = requests.put(url, data=json.dumps(request.DATA), headers=headers)
annotation.update(updated)
refresh = self.kwargs.get('refresh') != 'false'
annotation.save(refresh=refresh)
return Response(annotation)
return Response(annotation.json(), status=status.HTTP_404_NOT_FOUND)
def delete(self, request, *args, **kwargs): def delete(self, request, *args, **kwargs):
""" """
Delete an annotation. Delete an annotation.
""" """
annotation_id = self.kwargs.get('annotation_id') annotation_id = self.kwargs.get('annotation_id')
annotation = Annotation.fetch(annotation_id)
# TODO: delete annotation from elasticsearch if not annotation:
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id) return Response('Annotation not found! No delete performed.', status=status.HTTP_404_NOT_FOUND)
annotation = requests.delete(url)
annotation.delete()
# Annotation deleted successfully.
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(annotation, status=status.HTTP_404_NOT_FOUND)
def _filter_input(annotation, fields):
for field in fields:
annotation.pop(field, None)
return annotation
...@@ -2,3 +2,4 @@ Django==1.7.1 ...@@ -2,3 +2,4 @@ Django==1.7.1
djangorestframework==2.4.4 djangorestframework==2.4.4
django-rest-swagger==0.2.0 django-rest-swagger==0.2.0
elasticsearch==1.2.0 elasticsearch==1.2.0
annotator==0.12.0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment