Emails and CouchDB

This commit is contained in:
Patrick_Pluto 2024-09-25 11:21:05 +02:00
parent 5d8a90e5a2
commit 879d991340
2 changed files with 31 additions and 15 deletions

View file

@ -1,17 +1,20 @@
import hashlib import hashlib
import json import json
import os
import pycouchdb
class DB: class DB:
def __init__(self): def __init__(self):
self.database = {} self.database = {}
self.emails = []
def ensure_username(self, data): def ensure_username(self, data):
if hasattr(data, 'username'): if hasattr(data, 'username'):
return data.get['username'] return data.get['username']
elif hasattr(data, 'email'): elif hasattr(data, 'email'):
return self.emails[data.get['username']] for index, entry in self.database:
if entry.get['email'] == data.get['email']:
return index
@staticmethod @staticmethod
def hash_password(password): def hash_password(password):
@ -24,10 +27,9 @@ class DB:
password = data.get['password'] password = data.get['password']
email = data.get['email'] email = data.get['email']
hashed_password = self.hash_password(password) hashed_password = self.hash_password(password)
user_data = {"hashed_password": hashed_password} user_data = {"hashed_password": hashed_password, "email": email, "data": None}
if username not in self.database: if username not in self.database:
self.database[username] = user_data self.database[username] = user_data
self.emails[email] = username
return True return True
return False return False
@ -38,9 +40,6 @@ class DB:
return False return False
del self.database[username] del self.database[username]
for i in self.emails:
if i == username:
del i
self.save_database() self.save_database()
return True return True
@ -84,10 +83,26 @@ class DB:
return send_back return send_back
def save_database(self): def save_database(self):
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: with open("database.json", 'w') as file:
json.dump(self.database, file) json.dump(self.database, file)
def load_database(self): def load_database(self):
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: try:
with open("database.json", 'r') as file: with open("database.json", 'r') as file:
self.database = json.load(file) self.database = json.load(file)

View file

@ -10,3 +10,4 @@ PocketSphinx
google-cloud-speech google-cloud-speech
google-generativeai google-generativeai
python-weather python-weather
pycouchdb