Browse Source

implement accessing news

Eren Yilmaz 6 năm trước cách đây
mục cha
commit
5f1051b992
6 tập tin đã thay đổi với 76 bổ sung17 xóa
  1. 8 1
      client.py
  2. 14 0
      client_controller.py
  3. 8 2
      db_setup.py
  4. 38 13
      model.py
  5. 1 1
      server.py
  6. 7 0
      server_controller.py

+ 8 - 1
client.py

@@ -50,7 +50,14 @@ To display an overview of available commands type \'help\'.
 ''')
 
 
-allowed_commands = ['login', 'register', 'help', 'depot', 'activate_key', 'buy', 'orders']
+allowed_commands = ['login',
+                    'register',
+                    'help',
+                    'depot',
+                    'activate_key',
+                    'buy',
+                    'orders',
+                    'news']
 
 
 def one_command():

+ 14 - 0
client_controller.py

@@ -146,3 +146,17 @@ def orders():
             print('Order access failed with message:', response['error_message'])
         else:
             print('Order access failed.')
+
+
+def news():
+    response = client_request('news', {"session_id": connection.session_id})
+    success = 'data' in response
+    if success:
+        print(tabulate(response['data'],
+                       headers=['Date', 'Title'],
+                       tablefmt="pipe"))
+    else:
+        if 'error_message' in response:
+            print('Order access failed with message:', response['error_message'])
+        else:
+            print('Order access failed.')

+ 8 - 2
db_setup.py

@@ -4,7 +4,7 @@ from game import CURRENCY_NAME
 def setup(cursor):
     print('Database setup...')
 
-    replace = True
+    replace = False
 
     if replace:
         print(' - Dropping old tables...')
@@ -19,7 +19,7 @@ def setup(cursor):
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS users(
                     username VARCHAR(10) UNIQUE NOT NULL, 
-                    password VARCHAR(6) NOT NULL)
+                    password VARCHAR(200) NOT NULL)
                 ''')
     cursor.execute('''
                 CREATE TABLE IF NOT EXISTS ownables(
@@ -69,6 +69,12 @@ def setup(cursor):
                     FOREIGN KEY (used_by_user_id) REFERENCES user(rowid)
                 )
                 ''')
+    cursor.execute('''
+                CREATE TABLE IF NOT EXISTS news(
+                    dt DATETIME NOT NULL,
+                    title VARCHAR(50) NOT NULL
+                )
+                ''')
 
     print(' - Integrity checks...')
     cursor.execute('''

+ 38 - 13
model.py

@@ -93,7 +93,7 @@ def register(username, password, game_key):
                 (username, password)
                 VALUES (? , ?)
                 ''', (username, password))
-    own(username, CURRENCY_NAME)
+    own(get_user_id_by_name(username), CURRENCY_NAME)
     if game_key != '':
         if valid_key(game_key):
             activate_key(game_key, get_user_id_by_name(username))
@@ -102,15 +102,15 @@ def register(username, password, game_key):
 
 def own(user_id, ownable_name):
     cursor.execute('''
-                WITH (SELECT rowid FROM ownables WHERE name = ?) AS ownableid,
-                     (SELECT ?) AS userid
-                INSERT INTO ownership
-                (user_id, ownable_id)
-                SELECT userid, ownableid
+                WITH ownableid AS (SELECT rowid FROM ownables WHERE name = ?),
+                     userid AS (SELECT ?)
+                INSERT INTO ownership (user_id, ownable_id)
+                SELECT *
+                FROM userid, ownableid
                 WHERE NOT EXISTS (
                     SELECT * FROM ownership
-                    WHERE ownership.user_id = userid
-                    AND ownership.ownable_id = (SELECT rowid FROM ownables WHERE name = ownableid)
+                    WHERE ownership.user_id IN userid
+                    AND ownership.ownable_id IN ownableid
                 )
                 ''', (ownable_name, user_id,))
 
@@ -142,8 +142,20 @@ def send_ownable(from_user_id, to_user_id, ownable_name, amount):
     return True
 
 
-def valid_key(key):  # TODO possible performance increase when database gets larger by using sql directly
-    return key in unused_keys()
+def valid_key(key):
+    connect()
+
+    cursor.execute('''
+                SELECT key
+                FROM keys
+                WHERE used_by_user_id IS NULL
+                AND key = ?
+                ''', (key,))
+
+    if cursor.fetchone():
+        return True
+    else:
+        return False
 
 
 def new_session(user_id):
@@ -299,14 +311,14 @@ def get_user_orders(user_id):
         SELECT 
             orders.buy, 
             ownables.name, 
-            orders.ordered_amount - order.executed_amount, 
-            orders.limit, 
+            orders.ordered_amount - orders.executed_amount, 
+            orders."limit", 
             orders.stop_loss, 
             orders.ordered_amount
         FROM orders, ownables, ownership
         WHERE ownership.user_id = ?
         AND ownership.ownable_id = ownables.rowid
-        AND orders.ownable_id = ownership.rowid
+        AND orders.ownership_id = ownership.rowid
         ORDER BY orders.buy, ownables.name
         ''', (user_id,))
 
@@ -346,3 +358,16 @@ def user_owns_at_least(amount, user_id, ownable_id):
         return True
     else:
         return False
+
+
+def news():
+    connect()
+
+    cursor.execute('''
+        SELECT *
+        FROM news
+        ORDER BY dt
+        LIMIT 20
+        ''')
+
+    return cursor.fetchall()

+ 1 - 1
server.py

@@ -13,7 +13,7 @@ if __name__ == '__main__':
 
     model.setup()
 
-    valid_routes = ['login', 'register', 'depot', 'activate_key', 'order']
+    valid_routes = ['login', 'register', 'depot', 'activate_key', 'order', 'orders', 'news']
 
 
     @route('/<path>', method='POST')

+ 7 - 0
server_controller.py

@@ -108,6 +108,13 @@ def orders():
     return {'data': data}
 
 
+def news():
+    missing = missing_attributes(['session_id'])
+    if missing:
+        return bad_request(missing)
+    return {'data': model.news()}
+
+
 def not_found(msg=''):
     response.status = 404
     if debug: