Skip to content

Commit

Permalink
adding ui code
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed May 22, 2019
1 parent 6cf0bbe commit cd1e19e
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 0 deletions.
Empty file added aat/tests/ui/__init__.py
Empty file.
Empty file.
29 changes: 29 additions & 0 deletions aat/tests/ui/handlers/test_accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import tornado.web
import os.path
from ....ui.handlers.accounts import ServerAccountsHandler
from mock import MagicMock


class TestAccounts:
def setup(self):
settings = {
"debug": "True",
"template_path": os.path.join(os.path.dirname(__file__), '../', '../', '../', 'ui', 'assets', 'templates'),
}

self.app = tornado.web.Application(**settings)
self.app._transforms = []

def test_AccountsHandler(self):
req = MagicMock()
req.body = ''
x = ServerAccountsHandler(self.app, req, trading_engine=MagicMock(), psp_kwargs={})
x.te._ex.accounts = MagicMock()
x.te._ex.accounts.return_value = [MagicMock()]
x.te._ex.accounts.return_value[0].to_dict = MagicMock()
x.te._ex.accounts.return_value[0].to_dict.return_value = {'test': 1}

x._transforms = []
x.get()

assert len(x._write_buffer) > 0
21 changes: 21 additions & 0 deletions aat/tests/ui/handlers/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import tornado.web
import os.path
from ....ui.handlers.base import HTTPHandler
from mock import MagicMock


class TestBase:
def setup(self):
settings = {
"debug": "True",
"template_path": os.path.join(os.path.dirname(__file__), '../', '../', '../', 'ui', 'assets', 'templates'),
}
self.app = tornado.web.Application(**settings)
self.app._transforms = []

def test_validate(self):
req = MagicMock()
req.body = ''
x = HTTPHandler(self.app, req)
x._transforms = []
x.render_template('404.html')
54 changes: 54 additions & 0 deletions aat/tests/ui/handlers/test_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import tornado.web
import os.path
from ....ui.handlers.html import HTMLHandler, HTMLOpenHandler
from mock import MagicMock
from tornado.web import HTTPError


class TestHTML:
def setup(self):
settings = {
"debug": "True",
"template_path": os.path.join(os.path.dirname(__file__), '../', '../', '../', 'ui', 'assets', 'templates'),
}
self.app = tornado.web.Application(**settings)
self.app._transforms = []

def test_HTMLOpenHandler(self):
req = MagicMock()
req.body = ''
x = HTMLOpenHandler(self.app, req, template='404.html')
x._transforms = []
x.get_current_user = lambda: False

x.template = '404.html'
x.get()

x = HTMLOpenHandler(self.app, req, template=None)
x._transforms = []
x.get_current_user = lambda: False
x.get()

def test_HTMLHandler(self):
req = MagicMock()
req.body = ''
x = HTMLHandler(self.app, req, template='404.html')
x._transforms = []
x.get_current_user = lambda: False

x.template = '404.html'
try:
x.get()
assert False
except HTTPError:
pass

x = HTMLHandler(self.app, req, template=None)
x._transforms = []
x.get_current_user = lambda: True
x.get()

x = HTMLHandler(self.app, req, template='404.html')
x._transforms = []
x.get_current_user = lambda: b'test'
x.get()
55 changes: 55 additions & 0 deletions aat/tests/ui/handlers/test_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import tornado.web
import os.path
from ....ui.handlers.messages import ServerMessagesMixin, ServerMessagesHandler, ServerMessagesWSHandler
from mock import MagicMock
import tornado.websocket


class TestMessages:
def setup(self):
settings = {
"debug": "True",
"template_path": os.path.join(os.path.dirname(__file__), '../', '../', 'ui', 'assets', 'templates'),
}
self.app = tornado.web.Application(**settings)
self.app._transforms = []

def test_ServerMessagesMixin(self):
req = MagicMock()
req.body = ''

x = ServerMessagesMixin()
x._transforms = []

x.te = MagicMock()
x.te._ex = MagicMock()
x.te._ex.messages = MagicMock()
x.te._ex.messages.return_value = [MagicMock()]
x.te._ex.messages.return_value[0].to_dict = MagicMock()
x.te._ex.messages.return_value[0].to_dict.return_value = {'test': 1, 'instrument': {'underlying': 'test'}}

x.get_data()
x.get_data('test')
x.get_data(None, None, 0, 'BTCUSD')

x.te._ex.messages.return_value = {'TRADE': [MagicMock()]}
x.te._ex.messages.return_value['TRADE'][0].to_dict = MagicMock()
x.te._ex.messages.return_value['TRADE'][0].to_dict.return_value = {'test': 1}

x.get_data(type='TRADE')
x.get_data(type='TRADE', page=0, pairtype='test')
x.get_data(type='TRADE', page=0, pairtype='BTCUSD')
x.get_data(type='TRADE', page=0, pairtype='BTCUSD')

def test_ServerMessages(self):
req = MagicMock()
req.body = ''

x = ServerMessagesHandler(self.app, req, trading_engine=MagicMock(), psp_kwargs={})
x._transforms = []

x.te._ex.messages = MagicMock()
x.te._ex.messages.return_value = [MagicMock()]
x.te._ex.messages.return_value[0].to_dict = MagicMock()
x.te._ex.messages.return_value[0].to_dict.return_value = {'test': 1, 'instrument': {'underlying': 'test'}}
x.get()
26 changes: 26 additions & 0 deletions aat/tests/ui/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from mock import MagicMock


class TestExecution:
def setup(self):
pass
# setup() before each test method

def teardown(self):
pass
# teardown() after each test method

@classmethod
def setup_class(cls):
pass
# setup_class() before any methods in this class

@classmethod
def teardown_class(cls):
pass
# teardown_class() after any methods in this class

def test_init(self):
from ...ui.server import ServerApplication
s = ServerApplication(MagicMock())
assert s
Empty file added aat/ui/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions aat/ui/assets/static/css/font-awesome.min.css

Large diffs are not rendered by default.

Binary file not shown.
6 changes: 6 additions & 0 deletions aat/ui/assets/static/fonts/fonts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@font-face {
font-family: 'Ubuntu';
font-style: normal;
font-weight: 400;
src: local('Ubuntu Regular'), local('Ubuntu-Regular'), url(https://fonts.gstatic.com/s/ubuntu/v11/4iCs6KVjbNBYlgoKfw7z.ttf) format('truetype');
}
3 changes: 3 additions & 0 deletions aat/ui/assets/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<p>Not found...</p>
</html>
20 changes: 20 additions & 0 deletions aat/ui/assets/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="/static/css/fonts.css">
<link href="/static/css/root.css" rel="stylesheet">
<link href="/static/css/base.css" rel="stylesheet">
<title>{{ title }}</title>

{% block header %}
{% endblock %}

</head>
<body>
{% block main %}
{% endblock %}
</body>

{% block script %}
{% endblock %}
</html>
Empty file added aat/ui/handlers/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions aat/ui/handlers/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import tornado.web
from perspective import PerspectiveHTTPMixin


class ServerAccountsHandler(PerspectiveHTTPMixin, tornado.web.RequestHandler):
'''Server Handler
Extends:
tornado.web.RequestHandler
'''

def initialize(self, trading_engine, **psp_kwargs):
self.te = trading_engine
self.psp_kwargs = psp_kwargs

def get(self):
try:
self.psp_kwargs['data'] = [a.to_dict(True) for ex in self.te.exchanges().values() for a in ex.accounts()]
self.loadData(**self.psp_kwargs)
self.write(self.getData())
except Exception as e:
self.write(e)
53 changes: 53 additions & 0 deletions aat/ui/handlers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import tornado.ioloop
import tornado.web
import tornado.websocket
from jinja2 import Environment, FileSystemLoader, TemplateNotFound


class HTTPHandler(tornado.web.RequestHandler):
'''Just a default handler'''
def initialize(self, *args, **kwargs):
'''Initialize the server competition registry handler
This handler is responsible for managing competition
registration.
Arguments:
competitions {dict} -- a reference to the server's dictionary of competitions
'''
super(HTTPHandler, self).initialize(*args, **kwargs)

def render_template(self, template, **kwargs):
if hasattr(self, 'template_dirs'):
template_dirs = self.template_dirs
else:
template_dirs = []

if self.settings.get('template_path', ''):
template_dirs.append(
self.settings["template_path"]
)
env = Environment(loader=FileSystemLoader(template_dirs))

try:
template = env.get_template(template)
except TemplateNotFound:
raise TemplateNotFound(template)

kwargs['current_user'] = self.current_user.decode('utf-8') if self.current_user else ''
content = template.render(**kwargs)
return content


class WebSocketHandler(tornado.websocket.WebSocketHandler):
'''Just a default handler'''
def initialize(self, *args, **kwargs):
'''Initialize the server competition registry handler
This handler is responsible for managing competition
registration.
Arguments:
competitions {dict} -- a reference to the server's dictionary of competitions
'''
super(WebSocketHandler, self).initialize(*args, **kwargs)
30 changes: 30 additions & 0 deletions aat/ui/handlers/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import tornado.web
from .base import HTTPHandler


class HTMLOpenHandler(HTTPHandler):
def initialize(self, template=None, template_kwargs=None, **kwargs):
super(HTMLOpenHandler, self).initialize()
self.template = template
self.template_kwargs = template_kwargs or {}

def get(self, *args):
'''Get the login page'''
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)


class HTMLHandler(HTMLOpenHandler):
@tornado.web.authenticated
def get(self, *args):
'''Get the login page'''
if not self.template:
self.redirect('/')
else:
template = self.render_template(self.template, **self.template_kwargs)
self.write(template)

0 comments on commit cd1e19e

Please sign in to comment.