path('me/', views.UserView.as_view()),
path('', include('catalogue.api.urls2')),
path('', include('social.api.urls2')),
- path('', include('bookmarks.api.urls'))
+ path('', include('bookmarks.api.urls')),
+ path('', include('search.api.urls')),
]
--- /dev/null
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
+from django.urls import path
+from . import views
+
+
+urlpatterns = [
+ path('search/hint/', views.HintView.as_view()),
+ path('search/', views.SearchView.as_view()),
+ path('search/books/', views.BookSearchView.as_view()),
+ path('search/text/', views.TextSearchView.as_view()),
+]
--- /dev/null
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
+from rest_framework.generics import ListAPIView
+from rest_framework.response import Response
+from rest_framework.views import APIView
+from search.views import get_hints
+from search.forms import SearchFilters
+
+
+class HintView(APIView):
+ def get(self, request):
+ term = request.query_params.get('q')
+ hints = get_hints(term, request.user)
+ return Response(hints)
+
+
+class SearchView(APIView):
+ def get(self, request):
+ term = self.request.query_params.get('q')
+ f = SearchFilters({'q': term})
+ r = {}
+ if f.is_valid():
+ r = f.results()
+ return Response(r)
+
+class BookSearchView(ListAPIView):
+ def get_queryset(self, request):
+ term = self.request.query_params.get('q')
+
+class TextSearchView(ListAPIView):
+ def get_queryset(self, request):
+ term = self.request.query_params.get('q')
return query_syntax_chars.sub(replace, query)
-@cache.never_cache
-def hint(request, mozhint=False, param='term'):
- prefix = request.GET.get(param, '')
- if len(prefix) < 2:
- return JsonResponse([], safe=False)
-
- prefix = re_escape(' '.join(remove_query_syntax_chars(prefix).split()))
-
- try:
- limit = int(request.GET.get('max', ''))
- except ValueError:
- limit = 20
- else:
- if limit < 1:
- limit = 20
-
+def get_hints(prefix, user=None, limit=10):
+ if not prefix: return []
data = []
if len(data) < limit:
authors = catalogue.models.Tag.objects.filter(
}
for author in authors[:limit - len(data)]
])
- if request.user.is_authenticated and len(data) < limit:
+
+ if user is not None and user.is_authenticated and len(data) < limit:
tags = social.models.UserList.objects.filter(
- user=request.user, name__iregex='\m' + prefix).only('name', 'id', 'slug')
+ user=user, name__iregex='\m' + prefix).only('name', 'id', 'slug')
data.extend([
{
'type': 'set',
}
for info in infos[:limit - len(data)]
])
+ return data
+
+
+@cache.never_cache
+def hint(request, mozhint=False, param='term'):
+ prefix = request.GET.get(param, '')
+ if len(prefix) < 2:
+ return JsonResponse([], safe=False)
+
+ prefix = re_escape(' '.join(remove_query_syntax_chars(prefix).split()))
+
+ try:
+ limit = int(request.GET.get('max', ''))
+ except ValueError:
+ limit = 20
+ else:
+ if limit < 1:
+ limit = 20
+
+ data = get_hints(
+ prefix,
+ user=request.user if request.user.is_authenticated else None,
+ limit=limit
+ )
if mozhint:
data = [