Ver Fonte

begin work on börse

Eren Yilmaz há 7 anos atrás
pai
commit
f28896ac2b
1 ficheiros alterados com 26 adições e 13 exclusões
  1. 26 13
      model.py

+ 26 - 13
model.py

@@ -59,14 +59,15 @@ def setup():
     replace = False
     replace = False
 
 
     if replace:
     if replace:
-        print('  Dropping old tables...')
+        print(' - Dropping old tables...')
         cursor.execute("DROP TABLE IF EXISTS users")
         cursor.execute("DROP TABLE IF EXISTS users")
         cursor.execute("DROP TABLE IF EXISTS ownables")
         cursor.execute("DROP TABLE IF EXISTS ownables")
         cursor.execute("DROP TABLE IF EXISTS ownership")
         cursor.execute("DROP TABLE IF EXISTS ownership")
         cursor.execute("DROP TABLE IF EXISTS sessions")
         cursor.execute("DROP TABLE IF EXISTS sessions")
+        cursor.execute("DROP TABLE IF EXISTS orders")
         cursor.execute("DROP TABLE IF EXISTS transactions")
         cursor.execute("DROP TABLE IF EXISTS transactions")
 
 
-    print('  Creating tables...')
+    print(' - Creating tables...')
     cursor.execute('''
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS users(
                 CREATE TABLE IF NOT EXISTS users(
                     username VARCHAR(10) UNIQUE NOT NULL, 
                     username VARCHAR(10) UNIQUE NOT NULL, 
@@ -75,15 +76,15 @@ def setup():
     cursor.execute('''
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownables(
                 CREATE TABLE IF NOT EXISTS ownables(
                     name VARCHAR(10) UNIQUE NOT NULL, 
                     name VARCHAR(10) UNIQUE NOT NULL, 
-                    total_amount INTEGER NOT NULL)
+                    total_amount CURRENCY NOT NULL)
                 ''')
                 ''')
     cursor.execute('''
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownership(
                 CREATE TABLE IF NOT EXISTS ownership(
                     user_id INTEGER NOT NULL,
                     user_id INTEGER NOT NULL,
-                    stock_id INTEGER NOT NULL,
-                    amount INTEGER NOT NULL DEFAULT 0,
+                    ownable_id INTEGER NOT NULL,
+                    amount CURRENCY NOT NULL DEFAULT 0,
                     FOREIGN KEY (user_id) REFERENCES users(rowid),
                     FOREIGN KEY (user_id) REFERENCES users(rowid),
-                    FOREIGN KEY (stock_id) REFERENCES ownables(rowid),
+                    FOREIGN KEY (ownable_id) REFERENCES ownables(rowid),
                     UNIQUE (user_id, stock_id)
                     UNIQUE (user_id, stock_id)
                 )
                 )
                 ''')
                 ''')
@@ -95,15 +96,27 @@ def setup():
                 )
                 )
                 ''')
                 ''')
     cursor.execute('''
     cursor.execute('''
-                CREATE TABLE IF NOT EXISTS sessions(
-                    user_id INTEGER NOT NULL,
-                    session_id STRING NOT NULL,
-                    FOREIGN KEY (user_id) REFERENCES users(rowid)
+                CREATE TABLE IF NOT EXISTS orders(
+                    ownership_id INTEGER NOT NULL,
+                    buy BOOLEAN NOT NULL,
+                    limit CURRENCY,
+                    stop_loss BOOLEAN,
+                    ordered_amount CURRENCY,
+                    executed_amount CURRENCY,
+                    FOREIGN KEY (ownership_id) REFERENCES ownership(rowid)
+                )
+                ''')
+    cursor.execute('''
+                CREATE TABLE IF NOT EXISTS transactions(
+                    dt DATETIME NOT NULL,
+                    price CURRENCY NOT NULL,
+                    ownable_id INTEGER NOT NULL,
+                    FOREIGN KEY (ownable_id) REFERENCES ownable(rowid)
                 )
                 )
                 ''')
                 ''')
 
 
-    if replace:
-        print('  Adding initial data...')
+    if replace:  # TODO also seed new databases
+        print(' - Seeding initial data...')
         cursor.execute('''
         cursor.execute('''
                     INSERT INTO users
                     INSERT INTO users
                     (username, password)
                     (username, password)
@@ -128,7 +141,7 @@ def setup():
         kollar_id = cursor.fetchone()[0]
         kollar_id = cursor.fetchone()[0]
         cursor.execute('''
         cursor.execute('''
                     INSERT INTO ownership
                     INSERT INTO ownership
-                    (user_id, stock_id, amount)
+                    (user_id, ownable_id, amount)
                     VALUES (?, ?, ?)
                     VALUES (?, ?, ?)
                     ''', (bank_id, kollar_id, money_amount))
                     ''', (bank_id, kollar_id, money_amount))