class User(Base): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True) password = db.Column(db.String()) email = db.Column(db.String(64)) tweet = relationship('Tweet') # def __init__(self, username, password,email): # self.username = username # self.password = password # self.email = email def __repr__(self): return '<User %r>' % self.username def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password) @staticmethod def get_by_username(username): return db.session.query(User).filter( User.username == username ).first() @staticmethod def get_by_id(user_id): return db.session.query(User).filter( User.id == user_id ).first() @staticmethod def get_user_list(): return db.session.query(User).all() @staticmethod def authenticate(username, password): user = User.get_by_username(username) if user: if user.check_password(password): return user @staticmethod def identify(payload): user_id = payload['identity'] return User.get_by_id(user_id)
class Event(Base): __tablename__ = 'event' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100)) eventtype = db.Column(db.Integer) eventdesc = db.Column(db.String(100)) eventdate = db.Column(db.String(100)) startingtime = db.Column(db.String(100)) endingtime = db.Column(db.String(100)) saleroles = db.Column(db.Integer) saleslimit = db.Column(db.Integer) salespermember = db.Column(db.Integer) # tweet = relationship('Tweet') def __repr__(self): return "id={}, username={}".format(self.id, self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return db.session.query(Event).filter( Event.username == username).first() @staticmethod def get_by_id(user_id): return db.session.query(User).filter(User.id == user_id).first() @staticmethod def get_user_list(): return db.session.query(Event).all() @staticmethod def authenticate(username, password): user = User.get_by_username(username) if user: # check password if user.check_password(password): return user @staticmethod def identity(payload): user_id = payload['identity'] user = User.get_by_id(user_id) return user
class EventType(Base): __tablename__ = 'eventtype' id = db.Column(db.Integer, primary_key=True) eventdesc = db.Column(db.String(128)) def __repr__(self): return "id={}, eventtypedesc={}".format(self.id, self.eventtypedesc)
class UserType(Base): __tablename__ = 'usertype' id = db.Column(db.Integer, primary_key=True) roledesc = db.Column(db.String(128)) def __repr__(self): return "id={}, roledesc={}".format(self.id, self.roledesc)
class User(Base): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(64)) tweet = relationship('Tweet') #设置关联对象 def __repr__(self): return "id={}, username={}, password_hash={}, email={}".format( self.id, self.username, self.password_hash, self.email) def set_password(self, password): #转化密码为哈希值 self.password_hash = generate_password_hash(password) def check_password(self, password): #检查哈希值密码是否正确 return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return db.session.query(User).filter(User.username == username).first() @staticmethod def get_by_id(user_id): return db.session.query(User).filter(User.id == user_id).first() @staticmethod def get_user_list(): return db.session.query(User).all() @staticmethod def authenticate(username, password): user = User.get_by_username(username) if user: # check password if user.check_password(password): return user @staticmethod def identity(payload): user_id = payload['identity'] user = User.get_by_id(user_id) return user
class EventTypeMapping(Base): __tablename__ = 'eventtypemapping' id = db.Column(db.Integer, primary_key=True) eventdesc = db.Column(db.String(128)) eventid = db.Column(db.Integer, db.ForeignKey("event.id", ondelete="CASCADE"), nullable=False) eventtypeid = db.Column(db.Integer, db.ForeignKey("eventtype.id", ondelete="CASCADE"), nullable=False)
class UserTypeMapping(Base): __tablename__ = 'usertypemapping' id = db.Column(db.Integer, primary_key=True) roledesc = db.Column(db.String(128)) userid = db.Column(db.Integer, db.ForeignKey("user.id", ondelete="CASCADE"), nullable=False) usertypeid = db.Column(db.Integer, db.ForeignKey("usertype.id", ondelete="CASCADE"), nullable=False)
class Tweet(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, ForeignKey('user.id')) body = db.Column(db.String(140)) created_at = db.Column(db.DateTime, server_default=func.now()) def __repr__(self): return "user_id={}, tweet={}".format( self.user_id, self.body )
class Users(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(64)) def __repr__(self): return "id={}, username={}".format( self.id, self.username ) def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def generate_token(self): """ Generates the token""" try: payload = { 'exp': datetime.utcnow() + timedelta(minutes=5), 'iat': datetime.utcnow(), 'sub': self.username } jwt_token = jwt.encode( payload, key='SECRET', algorithm='HS256' ) return jwt_token except Exception as e: return str(e)
class Tweet(Base): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, ForeignKey('user.id')) #使用外键,关联user.py中的id body = db.Column(db.String(140)) created_at = db.Column(db.DateTime, server_default=func.now()) def __repr__(self): return "user_id={}, tweet={}".format(self.user_id, self.body) def as_dict(self): t = {c.name: getattr(self, c.name) for c in self.__table__.columns} t['created_at'] = t['created_at'].isoformat() return t
class Demo(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(64), unique=True) tweet = relationship('Tweet') def __repr__(self): return "id={}, username={}".format( self.id, self.username ) def as_dict(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash,password) def add(self): db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def update(self): db.session.commit() @staticmethod def get_by_username(username): return db.session.query(User).filter( User.username == username ).first() @staticmethod def get_by_id(user_id): return db.session.query(User).filter( User.id == user_id ).first() @staticmethod def get_user_list(): return db.session.query(User).all() @staticmethod def authenticate(username,password): user = User.get_by_username(username) if user: #check password if user.check_password(password): return user @staticmethod def identity(payload): user_id = payload['identity'] user = User.get_by_id(user_id) return user