Exemple #1
0
    def test_authors(self, mock_site, mock_ia):
        a1 = mock_site.quicksave("/authors/OL1A", "/type/author", name="A1")
        a2 = mock_site.quicksave("/authors/OL2A", "/type/author", name="A2")
        work = mock_site.quicksave(
            "/works/OL1W",
            "/type/work",
            title="Foo",
            authors=[{"author": {"key": "/authors/OL2A"}}],
        )

        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo")
        assert home.format_book_data(book)['authors'] == []

        # when there is no work and authors, the authors field must be picked from the book
        book = mock_site.quicksave(
            "/books/OL1M",
            "/type/edition",
            title="Foo",
            authors=[{"key": "/authors/OL1A"}],
        )
        assert home.format_book_data(book)['authors'] == [
            {"key": "/authors/OL1A", "name": "A1"}
        ]

        # when there is work, the authors field must be picked from the work
        book = mock_site.quicksave(
            "/books/OL1M",
            "/type/edition",
            title="Foo",
            authors=[{"key": "/authors/OL1A"}],
            works=[{"key": "/works/OL1W"}],
        )
        assert home.format_book_data(book)['authors'] == [
            {"key": "/authors/OL2A", "name": "A2"}
        ]
Exemple #2
0
    def test_cover_url(self, mock_site, mock_ia):
        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo")
        assert home.format_book_data(book).get("cover_url") is None

        book = mock_site.quicksave("/books/OL1M",
                                   "/type/edition",
                                   title="Foo",
                                   covers=[1, 2])
        assert home.format_book_data(book).get(
            "cover_url") == "http://covers.openlibrary.org/b/id/1-M.jpg"
Exemple #3
0
def fulltext_search(q, page=1, limit=100, js=False):
    offset = (page - 1) * limit
    ia_results = fulltext_search_api({
        'q': q,
        'from': offset,
        'size': limit,
        'olonly': 'true'
    })

    if 'error' not in ia_results and ia_results['hits']:
        hits = ia_results['hits'].get('hits', [])
        ocaids = [hit['fields'].get('identifier', [''])[0] for hit in hits]
        availability = get_availability_of_ocaids(ocaids)
        if 'error' in availability:
            return []
        editions = web.ctx.site.get_many([
            '/books/%s' % availability[ocaid].get('openlibrary_edition')
            for ocaid in availability
            if availability[ocaid].get('openlibrary_edition')
        ])
        for ed in editions:
            if ed.ocaid in ocaids:
                idx = ocaids.index(ed.ocaid)
                ia_results['hits']['hits'][idx]['edition'] = (
                    format_book_data(ed) if js else ed)
                ia_results['hits']['hits'][idx]['availability'] = availability[
                    ed.ocaid]
    return ia_results
Exemple #4
0
    def test_authors(self, mock_site, mock_ia):
        a1 = mock_site.quicksave("/authors/OL1A", "/type/author", name="A1")
        a2 = mock_site.quicksave("/authors/OL2A", "/type/author", name="A2")
        work = mock_site.quicksave("/works/OL1W", "/type/work", title="Foo", authors=[{"author": {"key": "/authors/OL2A"}}])

        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo")
        assert home.format_book_data(book)['authors'] == []

        # when there is no work and authors, the authors field must be picked from the book
        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo", authors=[{"key": "/authors/OL1A"}])
        assert home.format_book_data(book)['authors'] == [{"key": "/authors/OL1A", "name": "A1"}]

        # when there is work, the authors field must be picked from the work
        book = mock_site.quicksave("/books/OL1M", "/type/edition",
            title="Foo",
            authors=[{"key": "/authors/OL1A"}],
            works=[{"key": "/works/OL1W"}]
        )
        assert home.format_book_data(book)['authors'] == [{"key": "/authors/OL2A", "name": "A2"}]
Exemple #5
0
    def test_cover_url(self, mock_site, mock_ia):
        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo")
        assert home.format_book_data(book).get("cover_url") is None

        book = mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo", covers=[1, 2])
        assert home.format_book_data(book).get("cover_url") == "http://covers.openlibrary.org/b/id/1-M.jpg"