def index(self, **dummy_kwargs):
        """ Output the suggestions page. """

        cherrypy.request.params['format'] = 'json'  # override user

        os = BaseSearcher.OpenSearch()
        os.sort_order = 'nentry'
        os.start_index = 1
        os.items_per_page = 5

        if os.format != 'json':
            raise cherrypy.HTTPError(400, 'Bad Request. Unknown format.')

        if len(os.query) == 0:
            raise cherrypy.HTTPError(400, 'Bad Request. No query.')

        last_word = os.query.split()[-1]
        if len(last_word) < 4:
            raise cherrypy.HTTPError(400, 'Bad Request. Query too short.')

        # ok. request looks sane. process it

        os.log_request('suggestions')

        os.f_format_title = os.format_suggestion
        os.f_format_subtitle = os.format_none
        os.f_format_extra = os.format_none
        os.f_format_url = os.format_none
        os.f_format_thumb_url = os.format_none
        os.f_format_icon = os.format_none

        sql = BaseSearcher.SQLStatement()

        # prepare inner query
        sql.query = 'SELECT tsvec'
        sql.from_ = ('books', )
        sql.fulltext('books.tsvec', os.query)
        inner_sql_query = self.sql_searcher.mogrify(os, sql)

        sql.query = "SELECT substr (word, 2) AS title FROM ts_stat ( %(inner)s )"
        sql.from_ = ()
        sql.params['inner'] = inner_sql_query
        sql.where = ["word ~* %(re_word)s"]
        sql.params['re_word'] = '^0' + last_word

        try:
            os = self.sql_searcher.search(os, sql)
        except DatabaseError as what:
            cherrypy.log("SQL Error: " + str(what),
                         context='REQUEST',
                         severity=logging.ERROR)
            raise cherrypy.HTTPError(500, 'Internal Server Error.')

        os.template = os.page = 'results'
        os.finalize()

        return self.format(os)
示例#2
0
    def fixup(self, os):
        """ strip marc subfields, add social media hints and facet links """

        for e in os.entries:
            if '$' in e.title:
                e.title = DublinCore.strip_marc_subfields(e.title)

        if (os.sort_order == 'release_date' and os.total_results > 0
                and os.start_index == 1):
            cat = BaseSearcher.Cat()
            cat.title = _('Follow new books on Twitter')
            cat.subtitle = _("Follow our new books on Twitter.")
            cat.url = 'https://twitter.com/gutenberg_new'
            cat.class_ += 'navlink grayed'
            cat.icon = 'twitter'
            cat.order = 5
            os.entries.insert(0, cat)

            cat = BaseSearcher.Cat()
            cat.title = _('Follow new books on Facebook')
            cat.subtitle = _(
                "Follow the link and like the page to have us post new books to your wall."
            )
            cat.url = 'https://www.facebook.com/gutenberg.new'
            cat.class_ += 'navlink grayed'
            cat.icon = 'facebook'
            cat.order = 5
            os.entries.insert(0, cat)

        if (len(os.query) and os.start_index == 1):
            sql2 = BaseSearcher.SQLStatement()
            sql2.query = "select count (*) from bookshelves"
            sql2.fulltext('bookshelves.tsvec', os.query)
            rows = BaseSearcher.SQLSearcher.execute(*sql2.build())
            if rows[0][0] > 0:
                cat = BaseSearcher.Cat()
                cat.rel = 'related'
                cat.title = _('Bookshelves')
                cat.subtitle = __('One bookshelf matches your query.',
                                  '{count} bookshelves match your search.',
                                  rows[0][0]).format(count=rows[0][0])
                cat.url = os.url('bookshelf_search', query=os.query)
                cat.class_ += 'navlink grayed'
                cat.icon = 'bookshelf'
                cat.order = 3
                os.entries.insert(0, cat)

            sql2 = BaseSearcher.SQLStatement()
            sql2.query = "select count (*) from subjects"
            sql2.fulltext('subjects.tsvec', os.query)
            rows = BaseSearcher.SQLSearcher.execute(*sql2.build())
            if rows[0][0] > 0:
                cat = BaseSearcher.Cat()
                cat.rel = 'related'
                cat.title = _('Subjects')
                cat.subtitle = __(
                    'One subject heading matches your search.',
                    '{count} subject headings match your search.',
                    rows[0][0]).format(count=rows[0][0])
                cat.url = os.url('subject_search', query=os.query)
                cat.class_ += 'navlink grayed'
                cat.icon = 'subject'
                cat.order = 3
                os.entries.insert(0, cat)

            sql2 = BaseSearcher.SQLStatement()
            sql2.query = "select count (*) from authors"
            sql2.fulltext('authors.tsvec', os.query)
            rows = BaseSearcher.SQLSearcher.execute(*sql2.build())
            if rows[0][0] > 0:
                cat = BaseSearcher.Cat()
                cat.rel = 'related'
                cat.title = _('Authors')
                cat.subtitle = __('One author name matches your search.',
                                  '{count} author names match your search.',
                                  rows[0][0]).format(count=rows[0][0])
                cat.url = os.url('author_search', query=os.query)
                cat.class_ += 'navlink grayed'
                cat.icon = 'author'
                cat.order = 3
                os.entries.insert(0, cat)
示例#3
0
    def index(self, **kwargs):
        """ Output a search result page. """

        os = BaseSearcher.OpenSearch()
        os.log_request('search')

        if 'default_prefix' in kwargs:
            raise cherrypy.HTTPError(
                400, 'Bad Request. Unknown parameter: default_prefix')

        if os.start_index > BaseSearcher.MAX_RESULTS:
            raise cherrypy.HTTPError(
                400, 'Bad Request. Parameter start_index too high')

        sql = BaseSearcher.SQLStatement()
        sql.query = 'SELECT *'
        sql.from_ = ['v_appserver_books_4 as books']

        # let derived classes prepare the query
        try:
            self.setup(os, sql)
        except ValueError as what:
            raise cherrypy.HTTPError(400, 'Bad Request. ' + str(what))

        os.fix_sortorder()

        # execute the query
        try:
            BaseSearcher.SQLSearcher().search(os, sql)
        except DatabaseError as what:
            cherrypy.log("SQL Error: " + str(what),
                         context='REQUEST',
                         severity=logging.ERROR)
            raise cherrypy.HTTPError(400, 'Bad Request. Check your query.')

        # sync os.title and first entry header
        if os.entries:
            entry = os.entries[0]
            if os.title and not entry.header:
                entry.header = os.title
            elif entry.header and not os.title:
                os.title = entry.header

        os.template = os.page = 'results'

        # give derived class a chance to tweak result set
        self.fixup(os)

        # warn user about no records found
        if os.total_results == 0:
            self.nothing_found(os)

        # suggest alternate queries
        if os.total_results < 5:
            self.output_suggestions(os)

        # add sort by links
        if os.start_index == 1 and os.total_results > 1:
            if 'downloads' in os.alternate_sort_orders:
                self.sort_by_downloads(os)
            if 'release_date' in os.alternate_sort_orders:
                self.sort_by_release_date(os)
            if 'title' in os.alternate_sort_orders:
                self.sort_by_title(os)
            if 'alpha' in os.alternate_sort_orders:
                self.sort_alphabetically(os)
            if 'quantity' in os.alternate_sort_orders:
                self.sort_by_quantity(os)

        os.finalize()
        self.finalize(os)

        if os.total_results > 0:
            # call this after finalize ()
            os.entries.insert(0, self.status_line(os))

        return self.format(os)