From c879554ba84c695ad85c1f0126f71a8f924976c0 Mon Sep 17 00:00:00 2001
From: vrigal <rigal@teklia.com>
Date: Mon, 2 Dec 2019 10:57:23 +0100
Subject: [PATCH] Add structure metadata filter in filtered ListElement
 endpoints

---
 arkindex/documents/api/elements.py | 46 ++++++++++++++++++++++++++++--
 1 file changed, 44 insertions(+), 2 deletions(-)

diff --git a/arkindex/documents/api/elements.py b/arkindex/documents/api/elements.py
index 87247ff933..0538463f91 100644
--- a/arkindex/documents/api/elements.py
+++ b/arkindex/documents/api/elements.py
@@ -8,9 +8,9 @@ from rest_framework.generics import (
 )
 from rest_framework import status, response
 from rest_framework.response import Response
-from arkindex_common.enums import TranscriptionType
+from arkindex_common.enums import TranscriptionType, MetaType
 from arkindex.documents.models import (
-    Corpus, Element, ElementPath, Right,
+    Corpus, Element, ElementPath, Right, MetaData,
     Classification, ClassificationState, Transcription, Region
 )
 from arkindex.documents.serializers.elements import (
@@ -115,6 +115,13 @@ class ElementsList(CorpusACLMixin, ListAPIView):
                 'schema': {
                     'type': 'boolean',
                 }
+            },
+            {
+                'name': 'structure',
+                'in': 'query',
+                'description': 'Filter elements by a structural metadata.',
+                'required': False,
+                'schema': {'type': 'string'},
             }
         ]
     }
@@ -138,6 +145,13 @@ class ElementsList(CorpusACLMixin, ListAPIView):
         if only_folder is not None:
             filters['type__folder'] = only_folder.lower() not in ('false', '0')
 
+        structure_param = self.request.query_params.get('structure')
+        if structure_param is not None:
+            filters['metadatas__in'] = MetaData.objects.filter(
+                type=MetaType.Structure,
+                value__contains=structure_param
+            )
+
         if 'corpus' in self.request.query_params:
             try:
                 corpus_id = UUID(self.request.query_params['corpus'])
@@ -372,6 +386,13 @@ class ElementParents(ListAPIView):
                 'schema': {
                     'type': 'boolean',
                 }
+            },
+            {
+                'name': 'structure',
+                'in': 'query',
+                'description': 'Filter elements by a structural metadata.',
+                'required': False,
+                'schema': {'type': 'string'},
             }
         ]
     }
@@ -396,6 +417,13 @@ class ElementParents(ListAPIView):
         if only_folder is not None:
             filters['type__folder'] = only_folder.lower() not in ('false', '0')
 
+        structure_param = self.request.query_params.get('structure')
+        if structure_param is not None:
+            filters['metadatas__in'] = MetaData.objects.filter(
+                type=MetaType.Structure,
+                value__contains=structure_param
+            )
+
         recursive_param = self.request.query_params.get('recursive')
 
         prefetch_related_lookups = ('zone__image__server', 'corpus', 'type')
@@ -510,6 +538,13 @@ class ElementChildren(ListAPIView):
                 'schema': {
                     'type': 'boolean',
                 }
+            },
+            {
+                'name': 'structure',
+                'in': 'query',
+                'description': 'Filter elements by a structural metadata.',
+                'required': False,
+                'schema': {'type': 'string'},
             }
         ]
     }
@@ -534,6 +569,13 @@ class ElementChildren(ListAPIView):
         if only_folder is not None:
             filters['type__folder'] = only_folder.lower() not in ('false', '0')
 
+        structure_param = self.request.query_params.get('structure')
+        if structure_param is not None:
+            filters['metadatas__in'] = MetaData.objects.filter(
+                type=MetaType.Structure,
+                value__contains=structure_param
+            )
+
         recursive_param = self.request.query_params.get('recursive')
 
         prefetch_related_lookups = ('zone__image__server', 'corpus', 'type')
-- 
GitLab