fixed some backend and database stuff
This commit is contained in:
parent
7c2f179c8b
commit
e594ecd7c2
6 changed files with 567 additions and 525 deletions
|
@ -97,6 +97,7 @@ class API:
|
|||
@self.app.route('/interstellar_ai/db', methods=['POST'])
|
||||
def db_manipulate():
|
||||
sent_data = request.get_json()
|
||||
print(sent_data)
|
||||
action = sent_data.get('action')
|
||||
if action == "create_account":
|
||||
return jsonify({'status': 200, 'response': self.db.add_user(sent_data)})
|
||||
|
@ -110,6 +111,10 @@ class API:
|
|||
return jsonify({'status': 200, 'response': self.db.check_credentials(sent_data)})
|
||||
elif action == "delete_account":
|
||||
return jsonify({'status': 200, 'response': self.db.delete_user(sent_data)})
|
||||
elif action == "get_email":
|
||||
return jsonify({'status': 200, 'response': self.db.get_email(sent_data)})
|
||||
elif action == "get_name":
|
||||
return jsonify({'status': 200, 'response': self.db.get_name(sent_data)})
|
||||
|
||||
return jsonify({'status': 401, 'response': "Invalid action"})
|
||||
|
||||
|
|
28
py/db.py
28
py/db.py
|
@ -9,13 +9,9 @@ class DB:
|
|||
self.database = {}
|
||||
|
||||
def ensure_username(self, data):
|
||||
print(data)
|
||||
print(self.database)
|
||||
if 'username' in data:
|
||||
print("usr")
|
||||
return data.get('username')
|
||||
elif 'email' in data:
|
||||
print("email")
|
||||
for index, entry in self.database:
|
||||
if entry.get('email') == data.get('email'):
|
||||
return index
|
||||
|
@ -34,15 +30,12 @@ class DB:
|
|||
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')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
|
@ -52,11 +45,10 @@ class DB:
|
|||
|
||||
def change_data(self, data):
|
||||
username = self.ensure_username(data)
|
||||
data = data.get('data')
|
||||
if not self.check_credentials(data):
|
||||
return False
|
||||
|
||||
self.database[username]['data'] = data
|
||||
self.database[username]['data'] = data.get('data')
|
||||
self.save_database()
|
||||
return True
|
||||
|
||||
|
@ -75,8 +67,6 @@ class DB:
|
|||
username = self.ensure_username(data)
|
||||
password = data.get('password')
|
||||
if username not in self.database:
|
||||
print("no username")
|
||||
print(username)
|
||||
return False
|
||||
|
||||
stored_hashed_password = self.database[username]["hashed_password"]
|
||||
|
@ -92,6 +82,22 @@ class DB:
|
|||
send_back = self.database[username].get('data')
|
||||
return send_back
|
||||
|
||||
def get_email(self, data):
|
||||
username = self.ensure_username(data)
|
||||
if not self.check_credentials(data):
|
||||
return None
|
||||
|
||||
send_back = self.database[username].get('email')
|
||||
return send_back
|
||||
|
||||
def get_name(self, data):
|
||||
username = self.ensure_username(data)
|
||||
if not self.check_credentials(data):
|
||||
return None
|
||||
|
||||
send_back = self.ensure_username(data)
|
||||
return send_back
|
||||
|
||||
def save_database(self):
|
||||
if os.environ.get('PRODUCTION') == "YES":
|
||||
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue