Skip to content

Commit

Permalink
fixes #5, updates for handles for attribute changes
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Jul 2, 2019
1 parent 9d92eec commit fcf2246
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 12 deletions.
1 change: 0 additions & 1 deletion aat/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def __init__(self, options: ExecutionConfig, exchanges: List[Exchange], accounts
self.trading_type = options.trading_type
self.exchanges = exchanges
self.accounts = accounts

self._backtest_id = 1

def insufficientFunds(self, req):
Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def initialize(self, trading_engine, psp_kwargs=None):

@run_on_executor
def get_data(self, **psp_kwargs):
dat = [a.to_dict(True) for ex in self.te.exchanges().values() for a in ex.accounts()]
dat = [a.to_dict(True) for ex in self.te.exchanges.values() for a in ex.accounts().values()]
super(AccountsHandler, self).loadData(data=dat, **psp_kwargs)
self.psp.schema['asOf'] = 'datetime'
return super(AccountsHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
3 changes: 3 additions & 0 deletions aat/ui/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class HTTPHandler(tornado.web.RequestHandler):
'''Just a default handler'''
executor = ThreadPoolExecutor(16)

def get_current_user(self):
return self.get_secure_cookie("token")

def initialize(self, *args, **kwargs):
'''Initialize the server competition registry handler
Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(self, trading_engine, psp_kwargs=None):

@run_on_executor
def get_data(self, **psp_kwargs):
exchanges = self.te.query().query_exchanges()
exchanges = self.te.query.query_exchanges()
msgs = [{'id': j + i*len(exchanges),
'name': ExchangeType_to_string(x['exchange']),
'instrument': y.to_dict(True, True)['underlying']}
Expand All @@ -26,6 +26,7 @@ def get_data(self, **psp_kwargs):
super(ExchangesHandler, self).loadData(data=msgs, **psp_kwargs)
return super(ExchangesHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
2 changes: 0 additions & 2 deletions aat/ui/handlers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def get(self, *args):
if not self.template:
self.redirect('/')
else:
if self.request.path == '/logout':
self.clear_cookie("user")
template = self.render_template(self.template, **self.template_kwargs)
self.write(template)

Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/instruments.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def initialize(self, trading_engine, psp_kwargs=None):
def get_data(self, exchange, **psp_kwargs):
if exchange:
exchange = ExchangeType(exchange)
msgs = [x.to_dict(True, True) for x in self.te.query().query_instruments(exchange)]
msgs = [x.to_dict(True, True) for x in self.te.query.query_instruments(exchange)]
super(InstrumentsHandler, self).loadData(data=msgs, **psp_kwargs)
return super(InstrumentsHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
exchange = self.get_argument('exchange', '')
Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/last_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(self, trading_engine, psp_kwargs=None):

@run_on_executor
def get_data(self, **psp_kwargs):
msgs = [s.to_dict(True, True) for s in self.te.query().query_lastpriceall()]
msgs = [s.to_dict(True, True) for s in self.te.query.query_lastpriceall()]
if len(msgs) > 0:
for msg in msgs:
msg['underlying'] = msg['instrument']['underlying']
Expand All @@ -28,6 +28,7 @@ def get_data(self, **psp_kwargs):
super(LastPriceHandler, self).loadData(data=list(dat.values()), **psp_kwargs)
return super(LastPriceHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
1 change: 1 addition & 0 deletions aat/ui/handlers/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def get_data(self, **psp_kwargs):
super(StrategiesHandler, self).loadData(data=dat, **psp_kwargs)
return super(StrategiesHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/strategy_trade_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(self, trading_engine, psp_kwargs=None):

@run_on_executor
def get_data(self, **psp_kwargs):
msgs = [s.to_dict(True, True) for s in self.te.query().query_tradereqs()]
msgs = [s.to_dict(True, True) for s in self.te.query.query_tradereqs()]
if len(msgs) > 0:
for msg in msgs:
msg['underlying'] = msg['instrument']['underlying']
Expand All @@ -24,6 +24,7 @@ def get_data(self, **psp_kwargs):
super(StrategyTradeRequestHandler, self).loadData(data=msgs, **psp_kwargs)
return super(StrategyTradeRequestHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
3 changes: 2 additions & 1 deletion aat/ui/handlers/strategy_trade_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(self, trading_engine, psp_kwargs=None):

@run_on_executor
def get_data(self, **psp_kwargs):
msgs = [s.to_dict(True, True) for s in self.te.query().query_traderesps()]
msgs = [s.to_dict(True, True) for s in self.te.query.query_traderesps()]
if len(msgs) > 0:
for msg in msgs:
msg.pop('request', None)
Expand All @@ -25,6 +25,7 @@ def get_data(self, **psp_kwargs):
super(StrategyTradeResponseHandler, self).loadData(data=msgs, **psp_kwargs)
return super(StrategyTradeResponseHandler, self).getData()

@tornado.web.authenticated
@tornado.gen.coroutine
def get(self):
dat = yield self.get_data(**self.psp_kwargs)
Expand Down
2 changes: 1 addition & 1 deletion aat/ui/handlers/trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_data(self, exchange=None, pair=None, **psp_kwargs):
except (ValueError, TypeError):
instrument = None

msgs = [x.to_dict(True, True) for x in self.te.query().query_trades(instrument)]
msgs = [x.to_dict(True, True) for x in self.te.query.query_trades(instrument)]
if len(msgs) > 0:
for msg in msgs:
msg['underlying'] = msg['instrument']['underlying']
Expand Down
28 changes: 26 additions & 2 deletions aat/ui/server.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import base64
import hashlib
import hmac
import os
import os.path
import logging
import secrets
import string
import time
import tornado.ioloop
import tornado.web
import ujson
import uuid
from .handlers.accounts import AccountsHandler
from .handlers.exchanges import ExchangesHandler
from .handlers.instruments import InstrumentsHandler
from .handlers.last_price import LastPriceHandler
from .handlers.login import LoginHandler, LogoutHandler
from .handlers.strategies import StrategiesHandler
from .handlers.strategy_trade_request import StrategyTradeRequestHandler
from .handlers.strategy_trade_response import StrategyTradeResponseHandler
from .handlers.trades import TradesHandler
from .handlers.html import HTMLOpenHandler
from .handlers.html import HTMLHandler, HTMLOpenHandler
from ..utils import log


class ServerApplication(tornado.web.Application):
Expand All @@ -28,9 +38,19 @@ def __init__(self,

logging.getLogger('tornado.access').disabled = False

if not cookie_secret:
nonce = int(time.time() * 1000)
encoded_payload = ujson.dumps({"nonce": nonce}).encode()
b64 = base64.b64encode(encoded_payload)
cookie_secret = hmac.new(str(uuid.uuid1()).encode(), b64, hashlib.sha384).hexdigest()

login_code = ''.join(secrets.choice(string.ascii_letters + string.digits) for i in range(20))
log.critical(f'\n**********\nLogin code: {login_code}\n**********')

settings = {
"cookie_secret": cookie_secret or "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", # TODO
"cookie_secret": cookie_secret,
"login_url": "/login",
"login_code": login_code,
"debug": debug,
"template_path": os.path.join(root, 'templates'),
}
Expand Down Expand Up @@ -69,5 +89,9 @@ def __init__(self,
(r"/api/v1/arrow/trades", TradesHandler, {'trading_engine': trading_engine,
'psp_kwargs': {'index': 'time', 'view': 'hypergrid', 'limit': 100, 'transfer_as_arrow': True}}),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": static}),
(r"/api/v1/login", LoginHandler, {}),
(r"/api/v1/logout", LogoutHandler, {}),
(r"/login", HTMLOpenHandler, {'template': '404.html'}),
(r"/logout", HTMLHandler, {'template': '404.html'}),
(r"/(.*)", HTMLOpenHandler, {'template': '404.html'})
], **settings)

0 comments on commit fcf2246

Please sign in to comment.