interstellar_ai/py/db.py

147 lines
4.4 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-10-07 15:01:06 +02:00
if "username" in data:
return data.get("username")
elif "email" in data:
for index, entry in self.database.items():
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-10-07 15:01:06 +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-07 15:01:06 +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-10-07 15:01:06 +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-10-07 15:01:06 +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-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
2024-10-07 15:01:06 +02:00
self.database[username]["settings"] = data.get("data")
2024-10-02 14:14:20 +02:00
self.save_database()
return True
def get_settings(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return None
2024-10-07 15:01:06 +02:00
send_back = self.database[username].get("settings")
2024-10-02 14:14:20 +02:00
return send_back
def change_history(self, data):
username = self.ensure_username(data)
if not self.check_credentials(data):
return False
2024-10-07 15:01:06 +02:00
self.database[username]["history"] = data.get("data")
2024-10-02 14:14:20 +02:00
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-07 15:01:06 +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
2024-10-07 15:01:06 +02:00
send_back = self.database[username].get("email")
2024-10-01 15:34:43 +02:00
return send_back
def get_name(self, 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-10-07 15:01:06 +02:00
if os.environ.get("PRODUCTION") == "YES":
2024-09-25 11:21:05 +02:00
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
db = server.database("interstellar_ai")
db.save(self.database)
else:
2024-10-07 15:01:06 +02:00
with open("database.json", "w") as file:
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-10-07 15:01:06 +02:00
if os.environ.get("PRODUCTION") == "YES":
2024-09-25 11:21:05 +02:00
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:
2024-10-07 15:01:06 +02:00
with open("database.json", "r") as file:
2024-09-25 11:21:05 +02:00
self.database = json.load(file)
except FileNotFoundError:
pass