96 lines
2.8 KiB
Python
96 lines
2.8 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
|
|
import pandas as pd
|
|
|
|
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_member_by_uid(self, user_uid):
|
|
"""Get a user by uid"""
|
|
#user = db.members.find_one({"uid": user_uid, "active": True})
|
|
GLOBAL_AVG = "SELECT username,email,account_name,firstname,lastname FROM members WHERE uid::text = '" + user_uid + "'"
|
|
#print(GLOBAL_AVG)
|
|
with db:
|
|
with db.cursor() as cursor:
|
|
cursor.execute(GLOBAL_AVG)
|
|
account = cursor.fetchall()
|
|
#return jsonify(hello="ameye world")
|
|
# Convert to DataFrame
|
|
df = pd.DataFrame(account, columns=[desc[0] for desc in db.description])
|
|
print(df)
|
|
if not account:
|
|
return
|
|
return account
|
|
|
|
|
|
# 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()
|
|
|
|
if not member:
|
|
return
|
|
#member["_id"] = member[0]
|
|
#str(member[0])
|
|
return member[0]
|
|
#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}")
|
|
|
|
# print( member[3] )
|
|
# print(self.encrypt_password(password))
|
|
|
|
if not member or not check_password_hash(member[3], password):
|
|
return
|
|
#member.pop(3)
|
|
#member[3]=''
|
|
return member
|