interstellar_ai/py/db.py

145 lines
4.5 KiB
Python
Raw Normal View History

2024-09-20 11:15:42 +02:00
import hashlib
2024-09-20 16:06:40 +02:00
import json
2024-09-25 11:21:05 +02:00
import os
import pycouchdb
2024-09-20 11:15:42 +02:00
class DB:
def __init__(self):
self.database = {}
2024-09-25 09:45:04 +02:00
def ensure_username(self, data):
2024-09-30 16:13:04 +02:00
if 'username' in data:
2024-09-30 15:26:31 +02:00
return data.get('username')
2024-09-30 16:13:04 +02:00
elif 'email' in data:
2024-09-25 11:21:05 +02:00
for index, entry in self.database:
2024-09-30 15:26:31 +02:00
if entry.get('email') == data.get('email'):
2024-09-25 11:21:05 +02:00
return index
2024-09-20 11:15:42 +02:00
2024-09-20 11:34:21 +02:00
@staticmethod
def hash_password(password):
2024-09-20 11:15:42 +02:00
salt = "your_secret_salt"
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
return hashed_password
2024-09-20 11:34:21 +02:00
def add_user(self, data):
2024-09-30 15:26:31 +02:00
username = data.get('username')
password = data.get('password')
email = data.get('email')
2024-09-20 11:34:21 +02:00
hashed_password = self.hash_password(password)
2024-10-02 14:14:20 +02:00
user_data = {"hashed_password": hashed_password, "email": email, "settings": None, "history": None}
2024-09-25 09:45:04 +02:00
if username not in self.database:
self.database[username] = user_data
2024-09-30 15:26:31 +02:00
self.save_database()
2024-09-25 09:45:04 +02:00
return True
return False
def delete_user(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return False
del self.database[username]
self.save_database()
return True
2024-09-20 11:15:42 +02:00
2024-09-20 11:34:21 +02:00
def update_password(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-30 15:26:31 +02:00
new_password = data.get('new_password')
2024-09-20 11:34:21 +02:00
if not self.check_credentials(data):
2024-09-20 11:15:42 +02:00
return False
2024-09-20 11:34:21 +02:00
hashed_new_password = self.hash_password(new_password)
2024-09-20 11:15:42 +02:00
self.database[username].update({"hashed_password": hashed_new_password})
2024-09-20 16:06:40 +02:00
self.save_database()
2024-09-20 11:15:42 +02:00
return True
2024-09-20 11:34:21 +02:00
def check_credentials(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-30 15:26:31 +02:00
password = data.get('password')
2024-09-20 11:15:42 +02:00
if username not in self.database:
return False
stored_hashed_password = self.database[username]["hashed_password"]
2024-09-20 11:34:21 +02:00
entered_hashed_password = self.hash_password(password)
2024-09-30 16:13:04 +02:00
print(stored_hashed_password == entered_hashed_password)
2024-09-20 11:15:42 +02:00
return stored_hashed_password == entered_hashed_password
2024-10-02 14:14:20 +02:00
def change_settings(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return False
self.database[username]['settings'] = data.get('data')
self.save_database()
return True
def get_settings(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return None
send_back = self.database[username].get('settings')
return send_back
def change_history(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return False
self.database[username]['history'] = data.get('data')
self.save_database()
return True
def get_history(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 11:34:21 +02:00
if not self.check_credentials(data):
2024-09-20 11:15:42 +02:00
return None
2024-10-02 14:14:20 +02:00
send_back = self.database[username].get('history')
2024-09-20 11:34:21 +02:00
return send_back
2024-10-01 15:34:43 +02:00
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
2024-09-20 16:06:40 +02:00
def save_database(self):
2024-09-25 11:21:05 +02:00
if os.environ.get('PRODUCTION') == "YES":
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
db = server.database("interstellar_ai")
db.save(self.database)
else:
with open("database.json", 'w') as file:
2024-09-30 15:26:31 +02:00
print("saving")
2024-09-25 11:21:05 +02:00
json.dump(self.database, file)
2024-09-20 16:06:40 +02:00
def load_database(self):
2024-09-25 11:21:05 +02:00
if os.environ.get('PRODUCTION') == "YES":
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
db = server.database("interstellar_ai")
if db:
self.database = db
else:
server.create("interstellar_ai")
db = server.database("interstellar_ai")
db.save(self.database)
else:
try:
with open("database.json", 'r') as file:
self.database = json.load(file)
except FileNotFoundError:
pass