-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
68 lines (56 loc) · 2.05 KB
/
server.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
import os
import tornado.httpserver
import tornado.httpclient
import tornado.ioloop
import tornado.web
import tornado.escape
import tornado.template
from ismi_search import Objects, Globals
class MainHandler(tornado.web.RequestHandler):
def initialize(self):
self.loader = tornado.template.Loader('templates')
@tornado.web.asynchronous
def get(self, *args, **kwargs):
filts = Globals.parse_filters(self.request)
filters = [[f.query_string, f.query_type, f.fquery_string, f.fquery_type] for f in filts]
self.render('templates/index.html')
class ResultsHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, *args, **kwargs):
filters = Globals.parse_filters(self.request)
results_dict = Objects.search(filters).get_dict()
num_results = results_dict.pop('num_results')
class Group:
def __init__(self, name, rslts):
self.name = name
self.num_results = len(rslts)
self.headers = list(set.union(
*(set(a.keys()) for a in rslts)
))
class Item:
def __init__(self, d, headers):
self.fields = [
d.get(h, '') for h in headers
]
self.items = [Item(r, self.headers) for r in rslts]
groups = enumerate(Group(*t) for t in results_dict.iteritems())
self.render('templates/results.html', num_results=num_results, groups=groups)
def main(port):
settings = {
'static_path': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'),
'autoescape' : None,
'debug': True
}
application = tornado.web.Application([
(r'/?', MainHandler),
(r'/results/?', ResultsHandler)
], **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 8888
main(port)