-
Notifications
You must be signed in to change notification settings - Fork 0
/
069.py
executable file
·101 lines (87 loc) · 2.48 KB
/
069.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# % cd /path/to/069_bottle
# % python app.py
# http://localhost:8080/ で動くから curl で見る
##########
# app.py #
##########
# -*- coding: utf-8 -*-
import json
from bottle import run, get, post, request, view
from pymongo import MongoClient, DESCENDING
@get('/search')
def get_condition():
return '''\
<h1>hoge</h1>
<form action="/search" method="post">
<p>name: <input name="name" type="text" /></p>
<p>aliases.name: <input name="aliases.name" type="text" /></p>
<p>tags.value: <input name="tags.value" type="text" /></p>
<p><input value="Search" type="submit" /></p>
</form>
'''
@post('/search')
@view('result.tpl')
def do_search():
client = MongoClient()
db = client.nlp100_ryot
col = db.artist
name= request.forms.get('name')
aliases_name = request.forms.get('aliases.name')
if aliases_name:
aliases_name = [{'aliases.name': name.strip()}
for name in aliases_name.split(',')]
else:
aliases_name = []
tags_value = request.forms.get('tags.value')
if tags_value:
tags_value = [{'tags.value': value.strip()}
for value in tags_value.split(',')]
else:
tags_value = []
spec = {
'name': request.forms.get('name'),
'$and': aliases_name + tags_value,
}
spec = {k: v for k, v in spec.items() if v}
return {'cursor': col.find(spec, sort=[('rating.value', DESCENDING)])}
run()
##############
# result.tpl #
##############
<table border=1>
<tr>
<th>rating.value</th>
<th>rating.count</th>
<th>name</th>
<th>aliases.name</th>
<th>area</th>
<th>tags.value</th>
</tr>
% for item in cursor:
<tr>
% rating = item.get('rating')
<td>{{rating['value'] if rating is not None else ''}}</td>
<td>{{rating['count'] if rating is not None else ''}}</td>
<td>{{item['name']}}</td>
% aliases = item.get('aliases')
% if aliases is not None:
% aliases_name = ', '.join(alias['name'] for alias in aliases)
% else:
% aliases_name = ''
% end
<td>{{aliases_name}}</td>
% area = item.get('area')
% if area is None:
% area = ''
% end
<td>{{area}}</td>
% tags = item.get('tags')
% if tags is not None:
% tags_value = ', '.join(tag['value'] for tag in tags)
% else:
% tags_value = ''
% end
<td>{{tags_value}}</td>
</tr>
% end
</table>