Commit 95765d5e by Oleg Marshev

Use annotator store in views directly.

parent 96f8e2de
......@@ -3,7 +3,8 @@ from notesserver.views import StatusView, AnnotationListView, AnnotationDetailVi
urlpatterns = patterns('', # nopep8
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/(?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
import json
from django.http import HttpResponse
from django.conf import settings
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
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):
......@@ -17,6 +32,9 @@ class StatusView(APIView):
permission_classes = (AllowAny,)
def get(self, request):
"""
Service status.
"""
return Response({})
......@@ -27,20 +45,32 @@ class AnnotationListView(APIView):
permission_classes = (AllowAny,)
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)
def post(self, request):
return Response(annotations)
def post(self, request, *args, **kwargs):
"""
Create a new annotation.
"""
conn = ElasticSearch()
if request.DATA is not None:
annotation = Annotation(_filter_input(request.DATA, CREATE_FILTER_FIELDS))
refresh = self.kwargs.get('refresh') != 'false'
annotation.save(refresh=refresh)
url = '{}annotations'.format(settings.ANNOTATOR_STORE)
headers = {'Content-type': 'application/json'}
annotation = requests.post(url, data=json.dumps(request.DATA), headers=headers)
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):
"""
......@@ -48,41 +78,60 @@ class AnnotationDetailView(APIView):
"""
permission_classes = (AllowAny,)
UPDATE_FILTER_FIELDS = ('updated', 'created', 'user', 'consumer')
def get(self, request, *args, **kwargs):
"""
Get an existing annotation.
"""
annotation_id = self.kwargs.get('annotation_id')
annotation = Annotation.fetch(annotation_id)
# TODO: get annotation from elasticsearch
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id)
annotation = requests.get(url)
if not annotation:
return Response(annotation, status=status.HTTP_404_NOT_FOUND)
return Response(annotation.json(), status=status.HTTP_404_NOT_FOUND)
return Response(annotation)
def put(self, request, *args, **kwargs):
"""
Update an existing annotation.
"""
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
headers = {'Content-type': 'application/json'}
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id)
annotation = requests.put(url, data=json.dumps(request.DATA), headers=headers)
if request.DATA is not None:
updated = _filter_input(request.DATA, UPDATE_FILTER_FIELDS)
updated['id'] = annotation_id # use id from URL, regardless of what arrives in JSON payload.
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):
"""
Delete an annotation.
"""
annotation_id = self.kwargs.get('annotation_id')
annotation = Annotation.fetch(annotation_id)
if not annotation:
return Response('Annotation not found! No delete performed.', status=status.HTTP_404_NOT_FOUND)
annotation.delete()
# TODO: delete annotation from elasticsearch
url = '{}annotations/{}'.format(settings.ANNOTATOR_STORE, annotation_id)
annotation = requests.delete(url)
# 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
djangorestframework==2.4.4
django-rest-swagger==0.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