Pārlūkot izejas kodu

try to fix changing password

Eren Yilmaz 6 gadi atpakaļ
vecāks
revīzija
60dd1b4256
5 mainītis faili ar 47 papildinājumiem un 3 dzēšanām
  1. 26 2
      client_controller.py
  2. 10 0
      model.py
  3. 1 0
      run_client.py
  4. 2 1
      run_server.py
  5. 8 0
      server_controller.py

+ 26 - 2
client_controller.py

@@ -78,6 +78,30 @@ def cancel_order(order_no=None):
         print('Order cancelling failed with message:', response['error_message'])
 
 
+def change_password(password=None, retype_password=None):
+    if password is None:
+        while True:
+            if sys.stdin.isatty():
+                password = getpass('New password: ')
+                retype_password = getpass('Retype password: ')
+            else:
+                password = input('New password: ')
+                retype_password = getpass('Retype password: ')
+            if password == retype_password:
+                break
+            print('Passwords do not match.')
+
+    fake_loading_bar('Validating password', duration=1.2)
+    fake_loading_bar('Changing password', duration=5.2)
+    response = client_request('change_password', {"session_id": connection.session_id, "password": password})
+
+    if 'error_message' in response:
+        print('Changing password failed with message:', response['error_message'])
+
+    fake_loading_bar('Signing out', duration=0.7)
+    connection.session_id = None
+
+
 # noinspection PyShadowingBuiltins
 def help():
     print('Allowed commands:')
@@ -214,7 +238,7 @@ def sell(amount=None, object_name=None, limit=None, stop_loss=None, time_until_e
 
 
 def orders():
-    fake_loading_bar('Validating Key', duration=0.9)
+    fake_loading_bar('Loading Data', duration=0.9)
     response = client_request('orders', {"session_id": connection.session_id})
     success = 'data' in response
     if success:
@@ -231,7 +255,7 @@ def orders():
 def orders_on(object_name=None):
     if object_name is None:  # TODO list some available objects
         object_name = input('Name of object to check: ')
-    fake_loading_bar('Validating Key', duration=2.3)
+    fake_loading_bar('Loading Data', duration=2.3)
     response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
     success = 'data' in response
     if success:

+ 10 - 0
model.py

@@ -856,3 +856,13 @@ def user_wealth(user_id):
         ''', (currency_id(), user_id,))
 
     return cursor.fetchone()[0]
+
+
+def change_password(session_id, password):
+    connect()
+
+    cursor.execute('''
+                UPDATE users
+                SET password = ?
+                WHERE ? IN (SELECT session_id FROM sessions WHERE sessions.user_id = users.rowid)
+                ''', (password, session_id,))

+ 1 - 0
run_client.py

@@ -55,6 +55,7 @@ To display an overview of available commands type \'help\'.
 allowed_commands = ['help',
                     'login',
                     'register',
+                    'change_password',
                     'depot',
                     'orders',
                     'news',

+ 2 - 1
run_server.py

@@ -20,7 +20,8 @@ if __name__ == '__main__':
                     'transactions',
                     'orders_on',
                     'cancel_order',
-                    'leaderboard']
+                    'leaderboard',
+                    'change_password']
 
 
     @route('/<path>', method='POST')

+ 8 - 0
server_controller.py

@@ -154,6 +154,14 @@ def cancel_order():
     return {'message': "Successfully deleted order"}
 
 
+def change_password():
+    missing = missing_attributes(['session_id', 'password'])
+    if missing:
+        return bad_request(missing)
+    model.change_password(request.json['session_id'], request.json['password'])
+    return {'message': "Successfully deleted order"}
+
+
 def news():
     missing = missing_attributes(['session_id'])
     if missing: