Better management of manually-set members.
[wolnelektury.git] / src / search / views.py
index 39284c7..da3a767 100644 (file)
@@ -1,11 +1,9 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf import settings
 from django.http.response import HttpResponseRedirect
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf import settings
 from django.http.response import HttpResponseRedirect
-from django.shortcuts import render_to_response
-from django.template import RequestContext
+from django.shortcuts import render
 from django.views.decorators import cache
 from django.http import HttpResponse, JsonResponse
 
 from django.views.decorators import cache
 from django.http import HttpResponse, JsonResponse
 
@@ -61,8 +59,8 @@ def did_you_mean(query, tokens):
 
 
 @cache.never_cache
 
 
 @cache.never_cache
-def hint(request):
-    prefix = request.GET.get('term', '')
+def hint(request, mozhint=False, param='term'):
+    prefix = request.GET.get(param, '')
     if len(prefix) < 2:
         return JsonResponse([], safe=False)
 
     if len(prefix) < 2:
         return JsonResponse([], safe=False)
 
@@ -94,8 +92,18 @@ def hint(request):
                 'id': b.id,
                 'url': b.get_absolute_url()
             }
                 'id': b.id,
                 'url': b.get_absolute_url()
             }
-            for b in Book.objects.filter(title__iregex='\m' + prefix)[:limit-len(data)]
+            for b in Book.objects.filter(findable=True, title__iregex='\m' + prefix)[:limit-len(data)]
         ]
         ]
+
+    if mozhint:
+        data = [
+            prefix,
+            [
+                item['label']
+                for item in data
+            ]
+        ]
+
     callback = request.GET.get('callback', None)
     if callback:
         return HttpResponse("%s(%s);" % (callback, json.dumps(data)),
     callback = request.GET.get('callback', None)
     if callback:
         return HttpResponse("%s(%s);" % (callback, json.dumps(data)),
@@ -108,42 +116,42 @@ def hint(request):
 def main(request):
     query = request.GET.get('q', '')
     if len(query) < 2:
 def main(request):
     query = request.GET.get('q', '')
     if len(query) < 2:
-        return render_to_response(
-            'catalogue/search_too_short.html', {'prefix': query},
-            context_instance=RequestContext(request))
+        return render(
+            request, 'catalogue/search_too_short.html',
+            {'prefix': query})
     elif len(query) > 256:
     elif len(query) > 256:
-        return render_to_response(
-            'catalogue/search_too_long.html', {'prefix': query}, context_instance=RequestContext(request))
+        return render(
+            request, 'catalogue/search_too_long.html',
+            {'prefix': query})
 
     query = prepare_query(query)
     pd_authors = search_pd_authors(query)
     books = search_books(query)
     pictures = search_pictures(query)
 
     query = prepare_query(query)
     pd_authors = search_pd_authors(query)
     books = search_books(query)
     pictures = search_pictures(query)
-    suggestion = u''
+    suggestion = ''
 
     if not (books or pictures or pd_authors):
         form = PublishingSuggestForm(initial={"books": query + ", "})
 
     if not (books or pictures or pd_authors):
         form = PublishingSuggestForm(initial={"books": query + ", "})
-        return render_to_response(
+        return render(
+            request,
             'catalogue/search_no_hits.html',
             {
                 'form': form,
                 'did_you_mean': suggestion
             'catalogue/search_no_hits.html',
             {
                 'form': form,
                 'did_you_mean': suggestion
-            },
-            context_instance=RequestContext(request))
+            })
 
     if not (books or pictures) and len(pd_authors) == 1:
         return HttpResponseRedirect(pd_authors[0].get_absolute_url())
 
 
     if not (books or pictures) and len(pd_authors) == 1:
         return HttpResponseRedirect(pd_authors[0].get_absolute_url())
 
-    return render_to_response(
+    return render(
+        request,
         'catalogue/search_multiple_hits.html',
         {
             'pd_authors': pd_authors,
             'books': books,
             'pictures': pictures,
             'did_you_mean': suggestion
         'catalogue/search_multiple_hits.html',
         {
             'pd_authors': pd_authors,
             'books': books,
             'pictures': pictures,
             'did_you_mean': suggestion
-        },
-        context_instance=RequestContext(request))
-
+        })
 
 def search_books(query):
     search = Search()
 
 def search_books(query):
     search = Search()
@@ -181,7 +189,7 @@ def search_books(query):
         except Book.DoesNotExist:
             return False
 
         except Book.DoesNotExist:
             return False
 
-    results = filter(ensure_exists, results)
+    results = [r for r in results if ensure_exists(r)]
     return results
 
 
     return results
 
 
@@ -216,7 +224,7 @@ def search_pictures(query):
         except Picture.DoesNotExist:
             return False
 
         except Picture.DoesNotExist:
             return False
 
-    results = filter(ensure_exists, results)
+    results = [r for r in results if ensure_exists(r)]
     return results
 
 
     return results