class Article(db.Model): __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.String(20000), nullable=False) publish_time = db.Column(db.DateTime, default=datetime.now, nullable=False) see_times = db.Column(db.Integer, default=1, nullable=False) # 个人隐私文章flag personal_flag = db.Column(db.Integer, default=0) # 外键连接 一个User对应多个Info, 其中 'user.id ' 中的 user 是表名 user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) # backref 是定义反向引用,可以通过 User 的 instance.infos 访问这个模型所写的所有info user_back = db.relationship('User', backref=db.backref('articles')) tag_back = db.relationship('Tag', secondary=article_tag, passive_deletes=True, backref=db.backref('articles')) category_back = db.relationship( 'Category', secondary=article_category, # 其中 passive_delete 支持多对多表关联删除,否则会报错! passive_deletes=True, backref=db.backref('articles')) def __str__(self): return '%s, %s, %s' % (self.id, self.title, self.content)
class Category(db.Model): __tablename__ = 'category' id = db.Column(db.Integer, primary_key=True, autoincrement=True) category_name = db.Column(db.String(100), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user_back = db.relationship('User', backref=db.backref('categorys')) article_back = db.relationship('Article', secondary=article_category, backref=db.backref('categorys'))
class Store(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), nullable=False) description = db.Column(db.String(120), nullable=False) url = db.Column(db.String(60), unique=True, nullable=False) img = db.Column(db.String(60), nullable=False, default='store.jpg') address = db.Column(db.String(120), nullable=False) location = db.Column(db.String(120), nullable=False) # derived from address items = db.relationship('Item', backref='Store', lazy=True) owner = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) tags = db.Column(db.String(1024), nullable=True) postLimit = db.Column(db.Integer, nullable=False, default=10) # Premium accounts have no post limit and no post expiration views = db.Column(db.Integer, nullable=False, default=0) # on store load raise count lastWeekViews = db.Column(db.Integer, nullable=True) # views from last week clickthroughs = db.Column(db.Integer, nullable=False, default=0) # sum of click throughs from item navigations lastWeekClickthroughs = db.Column(db.Integer, nullable=True) # click throughs from last week numposts = db.Column(db.Integer, nullable=False, default=0) # for getting new posts def __repr__(self): return f"Store('{self.name}, {self.address}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False ) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) image_file = db.Column(db.String(20), nullable=False, default='dflt.jpg') posts = db.relationship('Post', backref='author', lazy=True) likes = db.relationship('Likes', backref='liker', lazy=True) comments = db.relationship('Comment', backref='commentor', lazy=True) friends = db.relationship('Friends', backref='root_friend', lazy=True) lists = db.relationship('List', backref='lists', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class List(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False) tasks = db.relationship('Post', backref='tasks', lazy=True) user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) privacy = db.Column(db.Boolean, nullable=False) def __repr__(self): return f"List('{self.name}')"
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) followed = db.relationship( 'User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') def avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(digest, size) 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 follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): if self.is_following(user): self.followed.remove(user) def is_following(self, user): return self.followed.filter(followers.c.followed_id == user.id).count() > 0 def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter( followers.c.follower_id == self.id) own = Post.query.filter_by(user_id=self.id) return followed.union(own).order_by(Post.timestamp.desc()) def __repr__(self): return '<User {}>'.format(self.username)
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) content = db.Column(db.String(500), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) post = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) author = db.relationship('User', backref='author', lazy=True) def __repr__(self): return f"Comment('{self.content}', '{self.date_posted}')"
class Matiere(db.Model): id = db.Column(db.Integer, primary_key=True) nom = db.Column(db.String(20), nullable=False) semestre = db.Column(db.Integer, nullable=False) niveau = db.Column(db.String(20), nullable=False) specialite = db.Column(db.String(20), nullable=False) module = db.Column(db.String(20), nullable=False) coefficient = db.Column(db.Float, nullable=False) notes = db.relationship('Note', backref='Note_Matiere', lazy=True) def __repr__(self): return f"matiere('{self.id}','{self.nom}','{self.semestre}','{self.niveau}','{self.specialite}','{self.module}','{self.coefficient}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) nom = db.Column(db.String(20), unique=True, nullable=False) prenom = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) niveau = db.Column(db.String(120), nullable=False) specialite = db.Column(db.String(120), nullable=False) role = db.Column(db.String(120), nullable=False) notes = db.relationship('Note', backref='Note_User', lazy=True) def __repr__(self): return f"User('{self.id}','{self.nom}',{self.prenom}', '{self.email}','{self.niveau}','{self.specialite}','{self.role}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(128), nullable=False) image_file = db.Column(db.String(24), nullable=False, default='dflt.jpg') business = db.Column(db.Boolean(), nullable=False) store = db.relationship('Store', backref='Owner', lazy=True) following = db.relationship('Follow', backref='Follower', lazy=True) location = db.Column(db.String(120), nullable=False) #update on new login max_dist = db.Column(db.Integer, nullable=False, default=10) #max distance it will show items from in feed interests = db.Column(db.String(150), nullable=True, default='') # generates over time based on click throughs - used to show users items they like more def __repr__(self): return f"User('{self.username}, {self.business}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.String(500), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) date_due = db.Column(db.DateTime, nullable=True) list = db.Column(db.Integer, db.ForeignKey('list.id'), nullable=False) comments = db.relationship('Comment', backref='comments', lazy=True) likes = db.Column(db.Integer, nullable=False, default=0) private = db.Column(db.Boolean, nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(128), index=True, unique=True) password_hash = db.Column(db.String(128)) comments = db.relationship('Comment', backref='user', lazy='dynamic') created_at = db.Column(db.DateTime, default=get_time) def hash_password(self, password): self.password_hash = sha256_crypt.encrypt(password) def verify_password(self, password): return sha256_crypt.verify(password, self.password_hash) def __repr__(self): return '<User %r>' % (self.username)