建立User class & 驗證user

\user.py

from common.database import Database

class User(object):
 def __init__(self,name,account,password): #def __init__() 類別建構子
 self.name=name
 self.account=account
 self.password=password 
#self.接著的變數 整個類別當中都可以使用,沒有self就只有效在函式內

@staticmethod
 def is_login_valid(account,password): #有效 驗證
 user_data = Database.find_one(collection=’users’, query={“account”: account})
 if user_data is None:
 return False
 if user_data[‘password’]!=password:
 return False 
 return True

def save_to_db(self):
 Database.insert(collection=’users’,data=self.json())

def json(self): #轉成json格式
 return{
 “name”:self.name,
 “account”:self.account,
 “password”:self.password
 }

def find_user_data(account):
 user_data=Database.find_one(collection=’users’,query={“account”:account})
 return user_data

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.