Emails and CouchDB
This commit is contained in:
parent
5d8a90e5a2
commit
879d991340
2 changed files with 31 additions and 15 deletions
43
py/db.py
43
py/db.py
|
@ -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,12 +83,28 @@ class DB:
|
||||||
return send_back
|
return send_back
|
||||||
|
|
||||||
def save_database(self):
|
def save_database(self):
|
||||||
with open("database.json", 'w') as file:
|
if os.environ.get('PRODUCTION') == "YES":
|
||||||
json.dump(self.database, file)
|
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)
|
||||||
|
|
||||||
def load_database(self):
|
def load_database(self):
|
||||||
try:
|
if os.environ.get('PRODUCTION') == "YES":
|
||||||
with open("database.json", 'r') as file:
|
server = pycouchdb.Server("http://admin:admin@localhost:5984/")
|
||||||
self.database = json.load(file)
|
db = server.database("interstellar_ai")
|
||||||
except FileNotFoundError:
|
if db:
|
||||||
pass
|
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
|
||||||
|
|
|
@ -10,3 +10,4 @@ PocketSphinx
|
||||||
google-cloud-speech
|
google-cloud-speech
|
||||||
google-generativeai
|
google-generativeai
|
||||||
python-weather
|
python-weather
|
||||||
|
pycouchdb
|
Loading…
Reference in a new issue