Add findable flag.
[wolnelektury.git] / src / search / mock_search.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from unittest.mock import Mock
5 from catalogue.models import Book, Tag
6 from random import randint, choice
7
8
9 class Search(Mock):
10     """
11     Search mock for development without setting up Solr.
12
13     Instead of connecting to an actual search server, it returns
14     some random results for any query.
15     """
16     class MockIndex(Mock):
17         def analyze(*args, **kwargs):
18             return []
19
20     index = MockIndex()
21
22     @staticmethod
23     def _find_some_books(query_terms=None, max_results=20):
24         from .index import SearchResult
25
26         qs = Book.objects.filter(findable=True).order_by('?')
27         results = []
28         for book in qs[:randint(1, max_results)]:
29             doc = {
30                 'score': randint(0, 100),
31                 'book_id': book.pk,
32                 'published_date': randint(1000, 1920),
33                 }
34             res = SearchResult(doc, how_found='mock', query_terms=query_terms)
35             results.append(res)
36         return results
37
38     def search_everywhere(self, searched, query_terms=None):
39         return []
40