Browse Source

Split transactions to trades and trades_on

Eren Yilmaz 6 years ago
parent
commit
0875e672e4
5 changed files with 74 additions and 22 deletions
  1. 24 8
      client_controller.py
  2. 28 1
      model.py
  3. 5 4
      run_client.py
  4. 6 5
      run_server.py
  5. 11 4
      server_controller.py

+ 24 - 8
client_controller.py

@@ -3,10 +3,9 @@ from getpass import getpass
 from inspect import signature
 
 import connection
-from run_client import allowed_commands, fake_loading_bar
 from connection import client_request
-
 from debug import debug
+from run_client import allowed_commands, fake_loading_bar
 from util import my_tabulate
 
 exiting = False
@@ -249,7 +248,7 @@ def buy(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
     if 'error_message' in response:
         print('Order placement failed with message:', response['error_message'])
     else:
-        print('You might want to use the `transactions` or `depot` commands',
+        print('You might want to use the `trades` or `depot` commands',
               'to see if the order has been executed already.')
 
 
@@ -296,7 +295,7 @@ def sell(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
     if 'error_message' in response:
         print('Order placement failed with message:', response['error_message'])
     else:
-        print('You might want to use the `transactions` or `depot` commands',
+        print('You might want to use the `trades` or `depot` commands',
               'to see if the order has been executed already.')
 
 
@@ -387,12 +386,12 @@ def tradables():
             print('Data access failed.')
 
 
-def transactions(obj_name=None, limit=5):
+def trades_on(obj_name=None, limit=5):
     limit = float(limit)
     if obj_name is None:  # TODO list some available objects
         obj_name = input('Name of object to check: ')
     fake_loading_bar('Loading Data', duration=1.3)
-    response = client_request('transactions', {"session_id": connection.session_id,
+    response = client_request('trades_on', {"session_id": connection.session_id,
                                                "ownable": obj_name,
                                                "limit": limit})
     success = 'data' in response
@@ -402,9 +401,26 @@ def transactions(obj_name=None, limit=5):
                           tablefmt="pipe"))
     else:
         if 'error_message' in response:
-            print('Transactions access failed with message:', response['error_message'])
+            print('Trades access failed with message:', response['error_message'])
+        else:
+            print('Trades access failed.')
+
+
+def trades(limit=10):
+    limit = float(limit)
+    fake_loading_bar('Loading Data', duration=2.7)
+    response = client_request('trades', {"session_id": connection.session_id,
+                                               "limit": limit})
+    success = 'data' in response
+    if success:
+        print(my_tabulate(response['data'],
+                          headers=['Buy?', 'Name', 'Volume', 'Price', 'Time'],
+                          tablefmt="pipe"))
+    else:
+        if 'error_message' in response:
+            print('Trades access failed with message:', response['error_message'])
         else:
-            print('Transactions access failed.')
+            print('Trades access failed.')
 
 
 # noinspection PyShadowingBuiltins

+ 28 - 1
model.py

@@ -816,7 +816,7 @@ def place_order(buy, ownership_id, limit, stop_loss, amount, expiry):
     return True
 
 
-def transactions(ownable_id, limit):
+def trades_on(ownable_id, limit):
     connect()
 
     cursor.execute('''
@@ -830,6 +830,23 @@ def transactions(ownable_id, limit):
     return cursor.fetchall()
 
 
+def trades(user_id, limit):
+    connect()
+    cursor.execute('''
+        SELECT 
+            (CASE WHEN seller_id = ? THEN 'Sell' ELSE 'Buy' END), 
+            (SELECT name FROM ownables WHERE rowid = transactions.ownable_id), 
+            amount, 
+            price,
+            datetime(dt,'localtime')
+        FROM transactions
+        WHERE seller_id = ? OR buyer_id = ?
+        LIMIT ?
+        ''', (user_id, user_id, user_id, limit,))
+
+    return cursor.fetchall()
+
+
 def drop_expired_orders():
     connect()
 
@@ -1110,3 +1127,13 @@ def cleanup():
     if connection is not None:
         connection.commit()
         connection.close()
+
+
+def ownable_ids():
+    connect()
+
+    cursor.execute('''
+        SELECT rowid FROM ownables
+        ''')
+
+    return [ownable_id[0] for ownable_id in cursor.fetchall()]

+ 5 - 4
run_client.py

@@ -1,9 +1,9 @@
 from __future__ import print_function
+
 import sys
 import time
 
 import client_controller
-
 from debug import debug
 
 
@@ -60,14 +60,15 @@ allowed_commands = ['help',
                     'tradables',
                     'depot',
                     'orders',
-                    'activate_key',
+                    'orders_on',
+                    'trades',
+                    'trades_on',
                     'buy',
                     'sell',
-                    'transactions',
-                    'orders_on',
                     'cancel_order',
                     'gift',
                     'leaderboard',
+                    'activate_key',
                     'exit']
 
 

+ 6 - 5
run_server.py

@@ -1,13 +1,13 @@
 import sqlite3
 import time
 
-import connection
-import server_controller
-import model
 from bottle import run, response, route, redirect
 
-from server_controller import not_found
+import connection
+import model
+import server_controller
 from debug import debug
+from server_controller import not_found
 
 if __name__ == '__main__':
     print('sqlite3.version', model.db.version)
@@ -18,7 +18,8 @@ if __name__ == '__main__':
                     'activate_key',
                     'order', 'orders',
                     'news',
-                    'transactions',
+                    'trades',
+                    'trades_on',
                     'orders_on',
                     'cancel_order',
                     'leaderboard',

+ 11 - 4
server_controller.py

@@ -2,10 +2,10 @@ import json
 from datetime import timedelta, datetime
 
 from bottle import request, response
-import model
-from debug import debug
 from passlib.hash import sha256_crypt
 
+import model
+from debug import debug
 from util import salt
 
 
@@ -214,13 +214,20 @@ def tradables():
     return {'data': model.ownables()}
 
 
-def transactions():
+def trades():
+    missing = missing_attributes(['session_id', 'limit'])
+    if missing:
+        return bad_request(missing)
+    return {'data': model.trades(model.get_user_id_by_session_id(request.json['session_id']), request.json['limit'])}
+
+
+def trades_on():
     missing = missing_attributes(['session_id', 'ownable', 'limit'])
     if missing:
         return bad_request(missing)
     if not model.ownable_name_exists(request.json['ownable']):
         return bad_request('This kind of object can not have transactions.')
-    return {'data': model.transactions(model.ownable_id_by_name(request.json['ownable']), request.json['limit'])}
+    return {'data': model.trades_on(model.ownable_id_by_name(request.json['ownable']), request.json['limit'])}
 
 
 def leaderboard():