From 31ed0507187f27411f84b564491c0893245d4465 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 24 Oct 2024 11:53:37 +0200 Subject: [PATCH] endpoint for insert annotations from api --- api/urls.py | 1 + api/views.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/api/urls.py b/api/urls.py index 296a768..4fa86d6 100644 --- a/api/urls.py +++ b/api/urls.py @@ -7,6 +7,7 @@ app_name = 'api' urlpatterns = [ path('v1/snapshot/', views.NewSnapshotView.as_view(), name='new_snapshot'), + path('v1/annotation//', views.AddAnnotationView.as_view(), name='new_annotation'), path('v1/device//', views.DetailsDeviceView.as_view(), name='device'), path('v1/tokens/', views.TokenView.as_view(), name='tokens'), path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'), diff --git a/api/views.py b/api/views.py index b659ebb..676df18 100644 --- a/api/views.py +++ b/api/views.py @@ -261,3 +261,43 @@ class DetailsDeviceView(ApiMixing): data.update({"annotations": list(annotations)}) return data + + +class AddAnnotationView(ApiMixing): + + def post(self, request, *args, **kwargs): + response = self.auth() + if response: + return response + + self.pk = kwargs['pk'] + institution = self.tk.owner.institution + self.annotation = Annotation.objects.filter( + owner=institution, + value=self.pk, + type=Annotation.Type.SYSTEM + ).first() + + if not self.annotation: + return JsonResponse({}, status=404) + + try: + data = json.loads(request.body) + key = data["key"] + value = data["value"] + except Exception: + logger.exception("Invalid Snapshot of user {}".format(self.tk.owner)) + return JsonResponse({'error': 'Invalid JSON'}, status=500) + + Annotation.objects.create( + uuid=self.annotation.uuid, + owner=self.tk.owner.institution, + type = Annotation.Type.USER, + key = key, + value = value + ) + + return JsonResponse({"status": "success"}, status=200) + + def get(self, request, *args, **kwargs): + return JsonResponse({}, status=404)