class User(db.Model): __table_args__ = {'mysql_collate': 'utf8_general_ci'} id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(32)) salt = db.Column(db.String(32)) head_url = db.Column(db.String(256)) images = db.relationship('Image', backref='user', lazy='dynamic') def __init__(self, username, password, salt=''): self.username = username self.password = password self.salt = salt self.head_url = 'http://images.nowcoder.com/head/' + str(random.randint(0, 1000)) + 't.png' def __repr__(self): return ('<User %d %s>' % (self.id, self.username)).encode('gbk') # Flask Login接口 def is_authenticated(self): print('is_authenticated') return True def is_active(self): print('is_active') return True def is_anonymous(self): print('is_anonymous') return False def get_id(self): print('get_id') return self.id
class User(db.Model): __tablename = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(32)) salt = db.Column(db.String(32)) head_url = db.Column(db.String(256)) images = db.relationship('Image', backref='user', lazy='dynamic') def __init__(self, username, password, salt=''): self.username = username self.password = password self.salt = salt self.head_url = '/static/image/head.jpeg' def __repr__(self): return '[User %d %s]' % (self.id, self.username) @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): return self.id
class User(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(32)) salt = db.Column(db.String(32)) head_url = db.Column(db.String(256)) images = db.relationship('Image', backref='user', lazy='dynamic') def __init__(self, username, password, salt=''): self.username = username self.password = password self.salt = salt self.head_url = 'http://images.nowcoder.com/head/' + str( random.randint(0, 1000)) + 'm.png' def __repr__(self): return '<User %d %s>' % (self.id, self.username) @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): return self.id
class User(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(80), unique=True) # 用户名 password = db.Column(db.String(32)) # 密码 salt = db.Column(db.String(32)) # 密码加盐 head_url = db.Column(db.String(256)) # 头像的url地址 images = db.relationship('Image', backref='user', lazy='dynamic') # backref:反向联结(一对多) # 构造函数 def __init__(self, username, password, salt=''): self.username = username self.password = password self.salt = salt self.head_url = "http://images.nowcoder.com/head/" + str(random.randint(0, 1000)) + 'm.png' # 返回一个描述对象 def __repr__(self): return '<User %d %s>' % (self.id, self.username) # Flask Login接口 @property def is_authenticated(self): print 'is_authenticated' return True @property def is_active(self): print 'is_active' return True @property def is_anonymous(self): print 'is_anonymous' return False def get_id(self): print 'get_id' return self.id
class Image(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) url = db.Column(db.String(512)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) create_date = db.Column(db.DateTime) comments = db.relationship('Comment') def __init__(self, url, user_id): self.url = url self.user_id = user_id self.create_date = datetime.datetime.now() def __repr__(self): return '<Image %d %s>' % (self.id, self.url)
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column('user_id', db.Integer, primary_key=True) username = db.Column('username', db.String(30), unique=True, index=True) password = db.Column('password', db.Text) email = db.Column('email', db.String(50), unique=True, index=True) registered_on = db.Column('registered_on', db.DateTime) avatar = db.Column('avatar', db.String(200)) def __init__(self, username, email, password): self.username = username self.salting(password) self.email = email self.registered_on = datetime.utcnow() def __repr__(self): return f"<id.{self.id}> {self.username}" def salting(self, password): self.password = generate_password_hash(password) def salt_tasting(self, password): return check_password_hash(self.password, password) def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.id)
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.String(1024)) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) status = db.Column(db.Integer, default=0) # 0 正常 1 被删除 user = db.relationship('User') def __init__(self, content, image_id, user_id): self.content = content self.image_id = image_id self.user_id = user_id def __repr__(self): return '<Comment %d %s>' % (self.id, self.content)
class Comment(db.Model): __table_args__ = {'mysql_collate': 'utf8_general_ci'} id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.String(1024)) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) status = db.Column(db.Integer, default=0) # 0 正常 1删除 user = db.relationship('User') def __init__(self, content, image_id, user_id): self.content = content self.image_id = image_id self.user_id = user_id def __repr__(self): return ('<Comment%d %s>' % (self.id, self.content)).encode('gbk')
class Image(db.Model, UserMixin): __tablename__ = 'images' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'),nullable=False) image_name = db.Column(db.String(), nullable=False) def __init__(self, user_id, image_name): self.user_id = user_id self.image_name = image_name @hybrid_property def image_url(self): return f"{app.config['S3_LOCATION']}{self.image_name}" @staticmethod def url(image_name): return app.config['S3_LOCATION'] + image_name
class Image(db.Model): ''' 图片 ''' __tablename__ = 'image' id = db.Column(db.Integer, primary_key=True, autoincrement=True) url = db.Column(db.String(512)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) created_date = db.Column(db.DateTime) likecount = db.Column(db.Integer) # dislikecount=db.Column(db.Integer) 不统计踩的数目 comments = db.relationship('Comment') def __init__(self, url, user_id): self.url = url self.user_id = user_id self.created_date = datetime.now() self.likecount = 0 self.dislikecount = 0 def __repr__(self): return '<Image%d %s>' % (self.id, self.url)
class Donation(db.Model): __tablename__ = 'donations' id = db.Column(db.Integer, primary_key=True) sender_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) receiver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) image_id = db.Column(db.Integer, db.ForeignKey('images.id'), nullable=False) amount = db.Column(db.Numeric(), nullable=False) currency = db.Column(db.String(3), nullable=False, default='USD') donor = db.relationship("User", foreign_keys=[sender_id], back_populates="donations_out") receiver = db.relationship("User", foreign_keys=[receiver_id], back_populates="donations_in") def __init__(self, sender_id, receiver_id, image_id, amount, currency='USD'): self.sender_id = sender_id self.receiver_id = receiver_id self.image_id = image_id self.amount = amount self.currency = currency def __repr__(self): return f"Donation of {self.amount} has been made to {self.receiver_id} by {self.sender_id} for {self.image_id}"
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) full_name = db.Column(db.Text, nullable=False) email = db.Column(db.Text, nullable=False, index=True, unique=True) username = db.Column(db.String(50), nullable=False, index=True, unique=True) password = db.Column(db.String(255), nullable=False, server_default='') profile_picture_name = db.Column( db.Text, nullable=False, server_default='generic_profile_pic.png') is_private = db.Column(db.Boolean, nullable=False, server_default='False') images = db.relationship("Image", backref="users", lazy=True, order_by="desc(Image.id)", cascade="delete, delete-orphan") donations_out = db.relationship("Donation", foreign_keys=[ Donation.sender_id], back_populates="donor", lazy='dynamic', cascade="delete, delete-orphan") donations_in = db.relationship("Donation", foreign_keys=[ Donation.receiver_id], back_populates="receiver", lazy='dynamic', cascade="delete, delete-orphan") followers = db.relationship("Users_Users", foreign_keys=[ Users_Users.followed_id], back_populates="follower", lazy='dynamic', cascade="delete, delete-orphan") following = db.relationship("Users_Users", foreign_keys=[ Users_Users.follower_id], back_populates="following", lazy='dynamic', cascade="delete, delete-orphan") pending_in = db.relationship("Users_Users", foreign_keys=[ Users_Users.follower_id], back_populates="following", lazy='dynamic', cascade="delete, delete-orphan") def __init__(self, full_name, email, username, password): self.full_name = full_name self.email = email self.username = username self.password = password def get_user_id(self): return self.id def __repr__(self): return f"User {self.full_name} has email {self.email} and username {self.username}" @validates('email') @validation_preparation def validate_email(self, key, email): if not email: self.validation_errors.append('No email provided') if (not self.email == email): if User.query.filter_by(email=email).first(): self.validation_errors.append('Email is already in use') return email @validates('username') @validation_preparation def validate_username(self, key, username): if not username: self.validation_errors.append('No username provided') if (not self.username == username): if User.query.filter_by(username=username).first(): self.validation_errors.append('Username is already in use') if len(username) < 5 or len(username) > 20: self.validation_errors.append( 'Username must be between 5 and 20 characters') return username @validates('password') @validation_preparation def validate_password(self, key, password): if not password: self.validation_errors.append('Password not provided') if len(password) < 8 or len(password) > 50: self.validation_errors.append( 'Password must be between 8 and 50 characters') return generate_password_hash(password) @hybrid_property def profile_image_url(self): return f'{S3_LOCATION}{self.profile_picture_name}' @hybrid_property def donated_out(self): return self.donations_out.all() @hybrid_property def donated_in(self): return self.donations_in.all() @hybrid_property def is_followed_by(self): follower_usernames = [] follower_list = self.followers.all() for follower in follower_list: follower_usernames.append( User.query.get(follower.follower_id).username) return follower_usernames @hybrid_property def is_following_usernames(self): following_usernames = [] following_list = self.following.all() for following in following_list: following_usernames.append( User.query.get(following.followed_id).username) return following_usernames @hybrid_property def is_following_ids(self): following_ids = [] following_list = self.following.all() for following in following_list: following_ids.append( User.query.get(following.followed_id).id) return following_ids