-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.py
53 lines (51 loc) · 1.53 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
from common import globaldata, per_page, makelink, pagelinks, tagcloud, tag_prefix, wanted
from bottle import get, post, request, mako_view as view
from wellpapp import Tag, DotDict
@get("/")
@get("/search")
@post("/search")
@view("search")
def r_search(client):
data = globaldata()
def parse_tag(name):
res = client.parse_tag(name, comparison=True)
if res:
guid, cmp, val = res
tag = client.get_tag(guid, with_prefix=True)
return (tag, cmp, val)
try:
page = max(0, int(request.query.page))
except Exception:
page = 0
q = request.query.q.strip()
data.tagnames = qa = q.split()
data.tags = list(map(parse_tag, qa))
data.q = q = ' '.join(qa)
data.cloud = []
data.result_count = 0
ta = []
for i, (tag, cmp, val) in enumerate(filter(None, data.tags)):
if cmp:
qa[i] = tag_prefix(qa[i]) + tag.name
ta.append((tag, cmp, val))
if ta or not q:
if data.user and request.query.ALL:
range = [0, 1 << 31 - 1]
page = -1
else:
range = [per_page * page, per_page * page + per_page - 1]
order = "aaaaaa-aaaac8-faketg-bddate"
if ta and ta[0][0].ordered:
order = "group"
props = DotDict()
ga = [(t.pguid, cmp, val) for t, cmp, val in ta]
posts = client.search_post(guids=ga, order=order, range=range, wanted=wanted, props=props)
if posts:
data.posts = posts
data.result_count = props.result_count
data.page = page
data.pagelink = makelink('search', ('q', q))
data.pages, data.rels = pagelinks(data.pagelink, page, data.result_count)
data.cloud = tagcloud(ga)
return data