Eren Yilmaz %!s(int64=6) %!d(string=hai) anos
pai
achega
c3676795f6
Modificáronse 4 ficheiros con 26 adicións e 18 borrados
  1. 3 3
      client_controller.py
  2. 10 2
      create_new_stock.py
  3. 5 11
      model.py
  4. 8 2
      server_controller.py

+ 3 - 3
client_controller.py

@@ -231,7 +231,7 @@ def buy(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
     if limit is not None and \
             float(limit) <= 0 and \
             input(input('Are you sure you want to use such a low limit (limit=' + str(
-                limit) + '? (type in "yes" or something else):') != 'yes'):
+                limit) + ')? (type in "yes" or something else):') != 'yes'):
         print('Order was not placed.')
         return
 
@@ -277,8 +277,8 @@ def sell(obj_name=None, amount=None, limit='', stop_loss='', expiry=None):
         stop_loss = yn_dialog('Is this a stop-loss limit?')
     if limit is not None and \
             float(limit) <= 0 and \
-            input(input('Are you sure you want to use such a low limit (limit=' + str(
-                limit) + '? (type in "yes" or something else):') != 'yes'):
+            input('Are you sure you want to use such a low limit (limit=' + str(
+                limit) + ')? (type in "yes" or something else):') != 'yes':
         print('Order was not placed.')
         return
 

+ 10 - 2
create_new_stock.py

@@ -1,6 +1,14 @@
+from datetime import timedelta
+from time import strptime
+
 import model
 from admin_console import cleanup
 
 if __name__ == '__main__':
-    print(model.new_stock(float(input('How long will the initial selling be available? (minutes):'))))
-    cleanup()
+    timeout = float(input('How long will the initial selling be available? (minutes):'))
+    try:
+        expiry = strptime(model.current_db_time(), '%Y-%m-%d %H:%M:%S') + timedelta(minutes=timeout)
+        print(model.new_stock(expiry))
+        cleanup()
+    except OverflowError:
+        print('The expiration time is too far in the future.')

+ 5 - 11
model.py

@@ -480,7 +480,7 @@ def ownable_name_exists(name):
         return False
 
 
-def new_stock(timeout=60, name=None):
+def new_stock(expiry, name=None):
     connect()
 
     while name is None:
@@ -507,14 +507,10 @@ def new_stock(timeout=60, name=None):
                ownable_id,
                price,
                amount,
-               timeout)
+               expiry)
     return name
 
 
-def new_stocks(timeout=60, count=1):
-    return [new_stock(timeout=timeout) for _ in range(count)]
-
-
 def ownable_id_by_name(ownable_name):
     connect()
 
@@ -796,7 +792,7 @@ def ownable_name_by_id(ownable_id):
     return cursor.fetchone()[0]
 
 
-def bank_order(buy, ownable_id, limit, amount, time_until_expiration):
+def bank_order(buy, ownable_id, limit, amount, expiry):
     if not limit:
         raise AssertionError('The bank does not give away anything.')
     place_order(buy,
@@ -804,7 +800,7 @@ def bank_order(buy, ownable_id, limit, amount, time_until_expiration):
                 limit,
                 False,
                 amount,
-                time_until_expiration)
+                expiry)
     ownable_name = ownable_name_by_id(ownable_id)
     new_news('External investors are selling ' + ownable_name + ' atm')
 
@@ -819,10 +815,8 @@ def current_db_time():  # might differ from datetime.datetime.now() for time zon
     return cursor.fetchone()[0]
 
 
-def place_order(buy, ownership_id, limit, stop_loss, amount, time_until_expiration):
+def place_order(buy, ownership_id, limit, stop_loss, amount, expiry):
     connect()
-    expiry = datetime.strptime(current_db_time(), '%Y-%m-%d %H:%M:%S') + timedelta(minutes=time_until_expiration)
-
     cursor.execute('''
                 INSERT INTO orders 
                 (buy, ownership_id, "limit", stop_loss, ordered_amount, expiry_dt)

+ 8 - 2
server_controller.py

@@ -1,4 +1,7 @@
 import json
+from datetime import timedelta
+from time import strptime
+
 from bottle import request, response
 import model
 from debug import debug
@@ -129,8 +132,11 @@ def order():
     if sell:
         if not model.user_owns_at_least(amount, user_id, ownable_id):
             return bad_request('You can not sell more than you own.')
-
-    model.place_order(buy, ownership_id, limit, stop_loss, amount, time_until_expiration)
+    try:
+        expiry = strptime(model.current_db_time(), '%Y-%m-%d %H:%M:%S') + timedelta(minutes=time_until_expiration)
+    except OverflowError:
+        return bad_request('The expiration time is too far in the future.')
+    model.place_order(buy, ownership_id, limit, stop_loss, amount, expiry)
     return {'message': "Order placed."}