11#!/usr/bin/env python
2+
23import time
34import logging
45
89from sockjs .tornado import SockJSRouter
910
1011import set_path
11- from tilde .core .settings import settings , connect_database
12+ from tilde .core .settings import settings
1213from tilde .core .api import API
1314import tilde .core .model as model
14- from tilde .berlinium import Connection , add_redirection
15+ from tilde .berlinium import Async_Connection , add_redirection
1516
1617
1718logging .basicConfig (level = logging .INFO )
2223
2324class TildeGUIProvider :
2425 @staticmethod
25- def login (req , session_id ):
26+ def login (req , client_id , db_session ):
2627 result , error = None , None
2728 if not isinstance (req , dict ): return result , 'Invalid request!'
2829
@@ -36,59 +37,25 @@ def login(req, session_id):
3637
3738 if user != USER or not pass_match :
3839 return result , 'Unauthorized!'
39- Connection .Clients [session_id ].authorized = True
40- Connection .Clients [session_id ].db = connect_database (settings , default_actions = False , scoped = True )
41- return "OK" , None
42-
43- @staticmethod
44- def tags (req , session_id ):
45- result , error = [], None
46- if not isinstance (req , dict ): return result , 'Invalid request!'
47-
48- if not 'tids' in req : tids = None # NB json may contain nulls
49- else : tids = req ['tids' ]
50-
51- if not tids :
52- for tid , cid , topic in Connection .Clients [session_id ].db .query (model .uiTopic .tid , model .uiTopic .cid , model .uiTopic .topic ).all ():
53- # TODO assure there are checksums for every tid
54- try : match = [x for x in Tilde .hierarchy if x ['cid' ] == cid ][0 ]
55- except IndexError : return None , 'Schema and data do not match: different versions of code and database?'
56-
57- if not match .get ('has_facet' ): continue
58-
59- sort = 1000 if not 'sort' in match else match ['sort' ]
40+ Connection .Clients [client_id ].authorized = True
6041
61- ready_topic = {'tid' : tid , 'topic' : topic , 'sort' : 0 }
62-
63- for n , tag in enumerate (result ):
64- if tag ['category' ] == match ['category' ]:
65- result [n ]['content' ].append ( ready_topic )
66- break
67- else : result .append ({'cid' : match ['cid' ], 'category' : match ['category' ], 'sort' : sort , 'content' : [ ready_topic ]})
68-
69- result .sort (key = lambda x : x ['sort' ])
70- result = {'blocks' : result , 'cats' : Tilde .supercategories }
71-
72- return result , error
42+ return "OK" , None
7343
7444 @staticmethod
75- def sleep (req , session_id ):
45+ def sleep (req , client_id , db_session ):
7646 result , error = '' , None
7747 try : req = float (req )
7848 except : return result , 'Not a number!'
7949
8050 time .sleep (req )
8151
82- result = Tilde .count (Connection . Clients [ session_id ]. db )
52+ result = Tilde .count (db_session )
8353
8454 return result , error
8555
86- @staticmethod
87- def example (req , session_id ):
88- result , error = '' , None
89- return result , error
9056
9157if __name__ == "__main__" :
58+ Connection = Async_Connection # test with: select * from pg_stat_activity;
9259 Connection .GUIProvider = TildeGUIProvider
9360 DuplexRouter = SockJSRouter (Connection )
9461
@@ -98,7 +65,9 @@ def example(req, session_id):
9865 )
9966 application .listen (settings ['webport' ], address = '0.0.0.0' )
10067
101- logging .debug ("Server started" )
68+ logging .info ("DB is %s" % settings ['db' ]['engine' ])
69+ logging .info ("Connections are %s" % Connection .Type )
70+ logging .info ("Server started" )
10271
10372 try : ioloop .IOLoop .instance ().start ()
10473 except KeyboardInterrupt : pass
0 commit comments