コード例 #1
0
ファイル: views.py プロジェクト: eduardok/ekb
def show(req, search = None):
    urlexpander = lambda url: _html_url(req, url)
    qsearch = req.REQUEST.get('q', '')
    if not search:
        search = qsearch

    dict = {}
    dict['alltags'] = _alltags()
    dict['alldocs'] = Doc
    dict['menuitems'] = [
        ('/kb/', 'Knowledgebase'),
    ]

    doc = Doc.try_get(id=atoi(search))
    if doc: search = qsearch  # the old search was really a docid
    tag,tagdocs = _tagdocs(search)
    print 'tds: %r %r %r' % (search, tag, tagdocs)

    if search:
        dict['urlappend'] = '?q=%s' % search
    want_words = search.lower().split()

    if search:
        if tag:
            dict['menuitems'].append(('/kb/%s' % search, tag))
        else:
            dict['menuitems'].append(('/kb/%s' % search, '"%s"' % search))

    if tag:
        h = HtmlHighlighter([], '')
    else:
        h = HtmlHighlighter(want_words, 'u')

    dict['search'] = search
        
    if doc:
        # View the specific article they requested.
        doc.use_latest()
        pagebase = doc.get_url()
        page = pagebase + dict.get('urlappend', '')
        if req.path != pagebase and req.path != urllib.unquote(pagebase):
            return HttpResponsePermanentRedirect(page)
        dict['page'] = page
        if not tag and not search and doc.tags:
            t = doc.tags[0]
            dict['menuitems'].append(('/kb/%s' % t, t))
        dict['menuitems'].append((page, 'KB%d' % doc.id))
        dict['title'] = doc.title
        dict['when'] = nicedate(doc.mtime)
        dict['tags'] = doc.tags
        dict['editurl'] = doc.get_edit_url()
        dict['pdfurl'] = doc.get_pdf_url()
        dict['text'] = h.highlight(doc.expanded_text(urlexpander,
                                                     headerdepth=3,
                                                     expandbooks=0),
                                   markdown.markdown)
        dict['reference_parents'] = list(doc.reference_parents())
        dict['similar'] = doc.similar(max=4)
        dict['dissimilar'] = doc.dissimilar(max=4)
        if tag:
            dict['search'] = ''
        return render_to_response('ekb/view.html', dict)
    else:
        # Search for matching articles
        page = '/kb/%s' % search
        dict['page'] = page

        if tag:
            # the search term is actually the name of a tag
            f = tagdocs
            dict['skip_tags'] = 1
            dict['title'] = 'Category: %s' % tag
            dict['search'] = ''
        elif search:
            # the search term is just a search term
            dict['title'] = 'Search: "%s"' % search
            words = []
            docids = list(db.selectcol('select docid from WordWeights '
                                       '  where word=?', want_words[0]))
            for word in want_words[1:]:
                if not docids:
                    # no remaining matches
                    break
                docids = list(db.selectcol('select docid from WordWeights '
                                           '  where word=? and docid in (%s)'
                                           % _marks(docids),
                                           word, *docids))
            l = want_words + docids
            docweights = db.select('select avg(weight)*count(weight), docid '
                                   '  from WordWeights '
                                   '  where word in (%s) and docid in (%s) '
                                   '  group by docid '
                                   % (_marks(want_words), _marks(docids)),
                                   *l)
            f = []
            for weight,docid in sorted(docweights):
                if weight > 0.0:
                    f.append(docid)
        else:
            # there is no search term; toplevel index
            dict['title'] = 'Knowledgebase'
            return render_to_response('ekb/kb.html', dict)

        dict['docs'] = []
        for docid in f:
            d = Doc(docid)
            d.autosummary = autosummarize(d.expanded_text(urlexpander,
                                                         headerdepth=1,
                                                         expandbooks=1),
                                          want_words, h.highlight)
            dict['docs'].append(d)
                
        return render_to_response('ekb/search.html', dict)
コード例 #2
0
ファイル: views.py プロジェクト: eduardok/ekb
def _alltags():
    return db.select('select tag,count(tag) '
                     '  from Tags '
                     '  group by tag '
                     '  order by tag ')