interstellar_ai/py/db.py

111 lines
3.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):
if hasattr(data, 'username'):
return data.get['username']
elif hasattr(data, 'email'):
2024-09-25 11:21:05 +02:00
for index, entry in self.database:
if entry.get['email'] == data.get['email']:
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):
username = data.get['username']
password = data.get['password']
2024-09-25 09:45:04 +02:00
email = data.get['email']
2024-09-20 11:34:21 +02:00
hashed_password = self.hash_password(password)
2024-09-25 11:21:05 +02:00
user_data = {"hashed_password": hashed_password, "email": email, "data": None}
2024-09-25 09:45:04 +02:00
if username not in self.database:
self.database[username] = user_data
return True
return False
def delete_user(self, data):
username = self.ensure_username(data)
data = data.get['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 15:26:18 +02:00
def change_data(self, data):
2024-09-25 09:45:04 +02:00
username = self.ensure_username(data)
2024-09-20 15:26:18 +02:00
data = data.get['data']
if not self.check_credentials(data):
return False
self.database[username]['data'] = data
2024-09-20 16:06:40 +02:00
self.save_database()
2024-09-20 15:26:18 +02:00
return True
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-20 11:34:21 +02:00
new_password = data.get['new_password']
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-20 11:34:21 +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-09-20 15:26:18 +02:00
def get_data(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-09-20 15:26:18 +02:00
send_back = self.database[username].get['data']
2024-09-20 11:34:21 +02:00
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:
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