forked from React-Group/interstellar_ai
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 json
|
||||
import os
|
||||
import pycouchdb
|
||||
|
||||
|
||||
class DB:
|
||||
def __init__(self):
|
||||
self.database = {}
|
||||
self.emails = []
|
||||
|
||||
def ensure_username(self, data):
|
||||
if hasattr(data, 'username'):
|
||||
return data.get['username']
|
||||
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
|
||||
def hash_password(password):
|
||||
|
@ -24,10 +27,9 @@ class DB:
|
|||
password = data.get['password']
|
||||
email = data.get['email']
|
||||
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:
|
||||
self.database[username] = user_data
|
||||
self.emails[email] = username
|
||||
return True
|
||||
return False
|
||||
|
||||
|
@ -38,9 +40,6 @@ class DB:
|
|||
return False
|
||||
|
||||
del self.database[username]
|
||||
for i in self.emails:
|
||||
if i == username:
|
||||
del i
|
||||
self.save_database()
|
||||
return True
|
||||
|
||||
|
@ -84,12 +83,28 @@ class DB:
|
|||
return send_back
|
||||
|
||||
def save_database(self):
|
||||
with open("database.json", 'w') as file:
|
||||
json.dump(self.database, file)
|
||||
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)
|
||||
|
||||
def load_database(self):
|
||||
try:
|
||||
with open("database.json", 'r') as file:
|
||||
self.database = json.load(file)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
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
|
||||
|
|
|
@ -9,4 +9,5 @@ SpeechRecognition
|
|||
PocketSphinx
|
||||
google-cloud-speech
|
||||
google-generativeai
|
||||
python-weather
|
||||
python-weather
|
||||
pycouchdb
|
Loading…
Reference in a new issue