fixed some database backend stuff
This commit is contained in:
parent
26b69a1cb6
commit
223f953648
8 changed files with 58 additions and 41 deletions
16
py/api.py
16
py/api.py
|
@ -89,20 +89,20 @@ class API:
|
|||
|
||||
@self.app.route('/interstellar_ai/db', methods=['POST'])
|
||||
def db_manipulate():
|
||||
action = request.args.get('action')
|
||||
data = request.args.get('data')
|
||||
sent_data = request.get_json()
|
||||
action = sent_data.get('action')
|
||||
if action == "create_account":
|
||||
return jsonify({'status': 200, 'response': self.db.add_user(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.add_user(sent_data)})
|
||||
elif action == "change_password":
|
||||
return jsonify({'status': 200, 'response': self.db.update_password(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.update_password(sent_data)})
|
||||
elif action == "get_data":
|
||||
return jsonify({'status': 200, 'response': self.db.get_data(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.get_data(sent_data)})
|
||||
elif action == "change_data":
|
||||
return jsonify({'status': 200, 'response': self.db.change_data(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.change_data(sent_data)})
|
||||
elif action == "check_credentials":
|
||||
return jsonify({'status': 200, 'response': self.db.check_credentials(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.check_credentials(sent_data)})
|
||||
elif action == "delete_account":
|
||||
return jsonify({'status': 200, 'response': self.db.delete_user(data)})
|
||||
return jsonify({'status': 200, 'response': self.db.delete_user(sent_data)})
|
||||
|
||||
return jsonify({'status': 401, 'response': "Invalid action"})
|
||||
|
||||
|
|
24
py/db.py
24
py/db.py
|
@ -10,10 +10,10 @@ class DB:
|
|||
|
||||
def ensure_username(self, data):
|
||||
if hasattr(data, 'username'):
|
||||
return data.get['username']
|
||||
return data.get('username')
|
||||
elif hasattr(data, 'email'):
|
||||
for index, entry in self.database:
|
||||
if entry.get['email'] == data.get['email']:
|
||||
if entry.get('email') == data.get('email'):
|
||||
return index
|
||||
|
||||
@staticmethod
|
||||
|
@ -23,19 +23,22 @@ class DB:
|
|||
return hashed_password
|
||||
|
||||
def add_user(self, data):
|
||||
username = data.get['username']
|
||||
password = data.get['password']
|
||||
email = data.get['email']
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
email = data.get('email')
|
||||
hashed_password = self.hash_password(password)
|
||||
user_data = {"hashed_password": hashed_password, "email": email, "data": None}
|
||||
if username not in self.database:
|
||||
self.database[username] = user_data
|
||||
print("yes")
|
||||
self.save_database()
|
||||
return True
|
||||
print("fail")
|
||||
return False
|
||||
|
||||
def delete_user(self, data):
|
||||
username = self.ensure_username(data)
|
||||
data = data.get['data']
|
||||
data = data.get('data')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
|
@ -45,7 +48,7 @@ class DB:
|
|||
|
||||
def change_data(self, data):
|
||||
username = self.ensure_username(data)
|
||||
data = data.get['data']
|
||||
data = data.get('data')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
|
@ -55,7 +58,7 @@ class DB:
|
|||
|
||||
def update_password(self, data):
|
||||
username = self.ensure_username(data)
|
||||
new_password = data.get['new_password']
|
||||
new_password = data.get('new_password')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
|
@ -66,7 +69,7 @@ class DB:
|
|||
|
||||
def check_credentials(self, data):
|
||||
username = self.ensure_username(data)
|
||||
password = data.get['password']
|
||||
password = data.get('password')
|
||||
if username not in self.database:
|
||||
return False
|
||||
|
||||
|
@ -79,7 +82,7 @@ class DB:
|
|||
if not self.check_credentials(data):
|
||||
return None
|
||||
|
||||
send_back = self.database[username].get['data']
|
||||
send_back = self.database(username).get('data')
|
||||
return send_back
|
||||
|
||||
def save_database(self):
|
||||
|
@ -90,6 +93,7 @@ class DB:
|
|||
|
||||
else:
|
||||
with open("database.json", 'w') as file:
|
||||
print("saving")
|
||||
json.dump(self.database, file)
|
||||
|
||||
def load_database(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue