class Dislikes(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) def __repr__(self): return f"Like('{self.user_id}')"
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 Note(db.Model): id = db.Column(db.Integer, primary_key=True) note = db.Column(db.Float, nullable=False) nature = db.Column(db.String(20), nullable=False) matiere_id = db.Column(db.Integer, db.ForeignKey('matiere.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"note('{self.id}','{self.note}','{self.nature}','{self.matiere_id}','{self.user_id}')"
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 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 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 Friends(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) friend_id = db.Column(db.Integer) def __repr__(self): return f"User('{self.friend_id}')"
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 Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) language = db.Column(db.String(5)) def __repr__(self): return '<Post {}>'.format(self.body)
class Follow(db.Model): id = db.Column(db.Integer, primary_key=True) user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) store = db.Column(db.Integer) storeName = db.Column(db.String) last_seen = db.Column(db.Integer) def __repr__(self): return f"Follow('{self.user}, {self.store}')"
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 Item(db.Model): id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(60), nullable=False) type = db.Column(db.String(60), nullable=True) # not used img = db.Column(db.String(60), nullable=False, default='item.jpg') img_width = db.Column(db.Integer, nullable=False) img_height = db.Column(db.Integer, nullable=False) store = db.Column(db.Integer, db.ForeignKey('store.id'), nullable=False) tags = db.Column(db.String(60), nullable=True) metatags = db.Column(db.String(1024), nullable=True) time_left = db.Column(db.Integer, nullable=False, default=7) # If items set to disappear weekly location = db.Column(db.String(120), nullable=False) lat = db.Column(db.Float, nullable=False) lng = db.Column(db.Float, nullable=False) # same as parent store location views = db.Column(db.Integer, nullable=False, default=0) # on item view clickthroughs = db.Column(db.Integer, nullable=False, default=0) # on click to navigate @hybrid_method def lat_dist(self, lat, n): return (abs(self.lat - lat) < n) @lat_dist.expression def lat_dist(cls, lat, n): return (func.abs(cls.lat - lat) < n) @hybrid_method def lng_dist(self, lng, n): return (abs(self.lng - lng) < n) @lng_dist.expression def lng_dist(cls, lng, n): return func.abs(cls.lng - lng) < n def __repr__(self): return f"Item('{self.description}, {self.type}, {self.time_left}')"
class Comment(db.Model): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(256)) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) created_at = db.Column(db.DateTime, default=get_time) def object(self): return { 'userId': self.user_id, 'commentId': self.id, 'text': self.text } def __repr__(self): return '<Comment %r>' % (self.native_text)
self.email = kwargs.get('email') self.username = kwargs.get('username') self.password = generate_password_hash( kwargs.get('password')) # 对密码部分进行hash加密 # 检查密码的函数 def check_password(self, password): return check_password_hash(self.password, password) # Article_Tag的中间表 article_tag = db.Table( 'article_tag', db.Column('article_id', db.Integer, db.ForeignKey('article.id'), primary_key=True), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)) # Article_Category的中间表 article_category = db.Table( 'article_category', db.Column('article_id', db.Integer, db.ForeignKey('article.id'), primary_key=True), db.Column('category_id', db.Integer, db.ForeignKey('category.id'), primary_key=True))
from mainapp import db from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin from mainapp import login from hashlib import md5 followers = db.Table('followers', db.Column('follower_id', db.Integer, db.ForeignKey('user.id')), db.Column('followed_id', db.Integer, db.ForeignKey('user.id')) ) 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)