87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
"""Application Members Models"""
|
|
import os
|
|
# bson,
|
|
from dotenv import load_dotenv
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
import psycopg2
|
|
from psycopg2.extras import NamedTupleCursor
|
|
|
|
load_dotenv()
|
|
|
|
# DATABASE_URL=os.environ.get('DATABASE_URL') or 'mongodb://localhost:27017/myDatabase'
|
|
# print(DATABASE_URL)
|
|
# client = MongoClient(DATABASE_URL)
|
|
# db = client.myDatabase
|
|
|
|
dataUrl = os.getenv("DATABASE_URL")
|
|
db = psycopg2.connect(dataUrl)
|
|
|
|
|
|
class Members:
|
|
"""User Model"""
|
|
def __init__(self):
|
|
return
|
|
|
|
|
|
|
|
# def get_by_id(self, user_id):
|
|
# """Get a user by id"""
|
|
# user = db.users.find_one({"_id": bson.ObjectId(user_id), "active": True})
|
|
# if not user:
|
|
# return
|
|
# user["_id"] = str(user["_id"])
|
|
# user.pop("password")
|
|
# return user
|
|
|
|
def get_by_username(self, username):
|
|
"""Get a user by username"""
|
|
print(db)
|
|
# sqv = "SELECT * FROM members WHERE id=%s"
|
|
# x=["1"]
|
|
sqv = "SELECT * FROM members WHERE username='"+username+"'"
|
|
with db:
|
|
#with db.cursor(cursor_factory=NamedTupleCursor) as cursor:
|
|
with db.cursor() as cursor:
|
|
cursor.execute(sqv)
|
|
#cursor.execute(sqv, x)
|
|
member = cursor.fetchall()
|
|
|
|
column_names = [row[0] for row in cursor]
|
|
|
|
print("Column names:\n")
|
|
for i in column_names:
|
|
print(i)
|
|
# cursor.execute(sqv,x)
|
|
# result = cursor.fetchall()
|
|
#
|
|
# member = db.members.find_one({"username": username, "active": True})
|
|
# print(member)
|
|
if not member:
|
|
return
|
|
#member["_id"] = member[0]
|
|
#str(member[0])
|
|
return member
|
|
#return user
|
|
|
|
def encrypt_password(self, password):
|
|
"""Encrypt password"""
|
|
return generate_password_hash(password)
|
|
|
|
def login(self, username, password):
|
|
"""Login a user"""
|
|
member = self.get_by_username(username)
|
|
print('yyyyyyyyyyy---mmmmmm')
|
|
print( member )
|
|
print('yyyyyyyyyyy---xxxxxxxxx')
|
|
|
|
for column in member:
|
|
print(f"{column[0]}\t\t{column[1]}")
|
|
|
|
# print( member["password"] )
|
|
|
|
|
|
if not member or not check_password_hash(member["password"], password):
|
|
return
|
|
member.pop("password")
|
|
return member
|