Show active users.
[redakcja.git] / src / catalogue / views.py
index 697a5ac..097c549 100644 (file)
@@ -1,8 +1,9 @@
 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
+import json
 from django.apps import apps
-from django.db.models import Prefetch
+from django.db.models import Prefetch, Q
 from django.http import Http404, JsonResponse
 from django.urls import reverse
 from django.utils.formats import localize_input
@@ -20,7 +21,7 @@ from rest_framework.permissions import IsAdminUser
 from rest_framework.response import Response
 from rest_framework.views import APIView
 from rest_framework import serializers
-from depot.woblink import get_woblink_session
+import depot.models
 
 
 
@@ -219,7 +220,29 @@ class ThemaChooser(Chooser):
                 res = res[0]['sub']
             return res
 
-        for thema in self.queryset.all():
+        def apply_filter(filt):
+            if 'not' in filt:
+                return ~apply_filter(filt['not'])
+            if 'startswith' in filt:
+                q = None
+                for prefix in filt['startswith']:
+                    q2 = Q(code__startswith=prefix)
+                    if q:
+                        q |= q2
+                    else:
+                        q = q2
+                return q
+            assert False
+
+        qs = self.queryset
+        try:
+            filt = json.loads(request.GET.get('filter'))
+        except:
+            pass
+        else:
+            qs = qs.filter(apply_filter(filt))
+
+        for thema in qs:
             populate(thema)
 
         missing = list(getmissing(tree))
@@ -383,20 +406,20 @@ def publish_collection(request, pk):
 
 
 @login_required
-def woblink_author_autocomplete(request):
-    session = get_woblink_session()
+def woblink_autocomplete(request, category):
+    site = depot.models.Site.objects.filter(site_type='woblink').first()
+    if site is None:
+        return JsonResponse({})
+    woblink = site.get_publisher()
     term = request.GET.get('term')
     if not term:
         return JsonResponse({})
-    response = session.get(
-        'https://publisher.woblink.com/author/autocomplete/' + term
-    ).json()
-    return JsonResponse({
-        "results": [
-            {
-                "id": item['autId'],
-                "text": item['autFullname'],
-            }
-            for item in response
-        ],
-    })
+
+    if category == 'author':
+        results = woblink.search_author_catalogue(term)
+    elif category == 'series':
+        results = woblink.search_series_catalogue(term)
+    else:
+        raise Http404
+
+    return JsonResponse({"results": results})