Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
user.py 1.72 KB
Copy Edit Web IDE Raw Blame History
Mendalas authored 2019-06-12 18:24 . add api
from flask_sqlalchemy import SQLAlchemy
from api import app
from passlib.apps import custom_app_context as pwd_context
from itsdangerous import (TimedJSONWebSignatureSerializer
as Serializer, BadSignature, SignatureExpired)
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'users'
phone = db.Column(db.String(32), primary_key=True)
username = db.Column(db.String(128))
password_hash = db.Column(db.String(128))
position = db.Column(db.String(255))
eid = db.Column(db.String(255))
right = db.Column(db.Integer)
def hash_password(self, password):
self.password_hash = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
def generate_auth_token(self, expiration=600):
s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
return s.dumps({'phone': self.phone})
@staticmethod
def verify_auth_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None # valid token, but expired
except BadSignature:
return None # invalid token
user = User.query.get(data['phone'])
print(user.username)
return user
def to_json(self):
return {
"phone": self.phone,
"username": self.username,
"position": self.position,
"eid": self.eid,
"right": self.right
}
if __name__ == '__main__':
db.drop_all()
db.create_all()
user = User(phone=123,username="admin")
user.hash_password("root")
db.session.add(user)
db.session.commit()

Comment ( 0 )

Sign in for post a comment