Commit 01d7e8d3 authored by Marko Kollo's avatar Marko Kollo
Browse files

Added update endpoint to the Searcher API.

parent f3abf69f
......@@ -3,7 +3,7 @@ version: "3"
services:
elastic:
restart: always
restart: unless-stopped
container_name: texta-elastic
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4
ports:
......@@ -21,7 +21,7 @@ services:
- texta-data-volume:/usr/share/elasticsearch/data
texta:
restart: always
restart: unless-stopped
container_name: texta-toolkit
env_file:
- docker/texta/docker-toolkit.env
......
from rest_framework import serializers
from search_api.validator_serializers.common_validators import validate_auth_token
class UpdateItem(serializers.Serializer):
id = serializers.CharField(required=True, allow_blank=False, min_length=0)
index = serializers.CharField(required=True, allow_blank=False, min_length=0)
doc_type = serializers.CharField(required=False, allow_blank=False, min_length=0)
changes = serializers.DictField(required=True)
class UpdateRequestSerializer(serializers.Serializer):
auth_token = serializers.CharField(validators=[validate_auth_token], required=True, min_length=0, allow_blank=False)
items = serializers.ListField(child=UpdateItem(), required=True, allow_empty=False)
import logging
import elasticsearch
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from rest_framework.views import APIView
from search_api.update_endpoint.update_serializers import UpdateRequestSerializer
from search_api.validator_serializers.common_exceptions import ElasticTransportError
from texta.settings import es_url, ERROR_LOGGER
class UpdateView(APIView):
serializer_class = UpdateRequestSerializer
def get(self, request):
return Response()
def post(self, request):
serializer = UpdateRequestSerializer(data=request.data)
# Will return an error message if not valid.
if serializer.is_valid(raise_exception=True):
validated_data = serializer.validated_data
for item in validated_data["items"]:
response = UpdateView.elastic_update_request(item)
return Response("Editing successfull!")
@staticmethod
def elastic_update_request(item: dict) -> dict:
try:
elastic = elasticsearch.Elasticsearch(es_url)
response = elastic.update(
index=item["index"],
doc_type=item["doc_type"] if "doc_type" in item else item["index"],
id=item["id"],
body={"doc": item["changes"]}
)
return response
except elasticsearch.TransportError as e:
# Will return the appropriate error message along with the status code.
logging.getLogger(ERROR_LOGGER).exception(e)
raise ElasticTransportError(e.error)
except Exception as e:
logging.getLogger(ERROR_LOGGER).exception(e)
raise APIException("There has been an unidentified error in the backend, please contact the developers about this issue.")
from django.conf.urls import url
from . import views
from search_api.update_endpoint import update_views
urlpatterns = [
......@@ -10,4 +11,5 @@ urlpatterns = [
url(r'^list/datasets', views.list_datasets, name='list_datasets'),
url(r'^list/dataset', views.list_fields, name='list_fields'),
url(r'^more_like_this', views.more_like_this, name='more_like_this'),
url(r'^update', update_views.UpdateView.as_view(), name="update")
]
from rest_framework.exceptions import APIException
class ElasticTransportError(APIException):
status_code = 500
default_detail = 'Could not make Elasticsearch request.'
default_code = 'elasticsearch_transport_error'
from account.models import Profile
from rest_framework import serializers
def validate_auth_token(auth_token: str):
authenticated_token = Profile.objects.filter(auth_token=auth_token).first()
if not authenticated_token:
raise serializers.ValidationError('Failed to authenticate token.')
Supports Markdown
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