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 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 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 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 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 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 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)
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(100), unique=True, nullable=False) username = db.Column(db.String(100), unique=True, nullable=False) password = db.Column(db.String(100), nullable=False) allow_times = db.Column(db.Integer, default=0, nullable=False) # 容错率为5 # 目的是在User对象初始化的时候进行操作 def __init__(self, *args, **kwargs): 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)
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, 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): __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)
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 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 Player(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) password = db.Column(db.String(255)) iaCode = db.Column(db.Text) def __init__(self, name, password): m = hashlib.md5() m.update(password) password_hashed = m.hexdigest() self.iaCode = 'return direction' self.name = name self.password = password_hashed def __repr__(self): return '<Player %d>' % self.id def serialize(self): """Return object data in easily serializeable format""" return { 'id': self.id, 'name': 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)
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)
def __init__(self, *args, **kwargs): 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))
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 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}')"