class UserInfo(db.Model): """ 用户信息表 """ __tablename__ = 'user_info' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) motoo = db.Column(db.String(255)) introduction = db.Column(db.Text) use_gravatar = db.Column(db.Boolean, default=True) avatar = db.Column(db.String(255)) def __init__(self, user_id): self.user_id = user_id def __repr__(self): return "<UserInfo (%s)>" % self.user_id
class Syntax(db.Model): """语法表""" __tablename__ = 'syntax' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(45), unique=True, nullable=False) # 显示的名字 tag = db.Column(db.String(45), nullable=False) # 给tag用的, 用户不能改 def __init__(self, name, tag): self.name = name self.tag = tag def __repr__(self): return "<Syntax (%s|%s)>" % (self.name, self.tag) @classmethod def get_syntax_list(self): all_syntax = Syntax.query.order_by('name').all() return [(str(one.id), one.name) for one in all_syntax]
class Page(db.Model): """ 静态页面 content使用markdown格式的文本 """ __tablename__ = 'pages' id = db.Column(db.Integer, primary_key=True) slug = db.Column(db.String(255)) title = db.Column(db.String(255)) content = db.Column(db.Text) created_time = db.Column(db.DateTime, default=datetime.now()) modified_time = db.Column(db.DateTime, default=datetime.now()) def __init__(self, slug, title, content): self.slug = slug self.title = title self.content = content def __repr__(self): return "<Page %s|%s>" % (self.id, self.slug)
class Post(db.Model): """ """ __tablename__ = "posts" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(255)) content = db.Column(db.Text) created_time = db.Column(db.DateTime, default=datetime.now()) modified_time = db.Column(db.DateTime, default=datetime.now()) def __init__(self, title, content): self.title = title self.content = content def __repr__(self): return "<Post %s>" % self.id
class MessageTemplate(db.Model): """ 系统消息 """ __tablename__ = 'message_templates' id = db.Column(db.Integer, primary_key=True) used_for = db.Column(db.String(45)) title = db.Column(db.Text) content = db.Column(db.Text) def __init__(self, used_for, title, content): self.used_for = used_for self.title = title self.content = content def __repr__(self): return "<MessageTemplate %s|%s>" % (self.id, self.used_for)
class Paste(db.Model): """ 代码表 """ __tablename__ = 'pastes' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) syntax_id = db.Column(db.Integer, db.ForeignKey('syntax.id'), nullable=False) title = db.Column(db.String(255), nullable=False) # 标题, 默认为"未知标题" content = db.Column(db.Text, nullable=False) # 代码内容, 不能为空 view_num = db.Column(db.Integer, default=0) comment_num = db.Column(db.Integer, default=0) rate_num = db.Column(db.Integer, default=0) description = db.Column(db.Text, nullable=True) created_time = db.Column(db.DateTime, nullable=False) modified_time = db.Column(db.DateTime, nullable=False) revision = db.Column(db.Integer, nullable=False) is_final = db.Column(db.Boolean, default=True, nullable=False) is_private = db.Column(db.Boolean, default=False) is_delete = db.Column(db.Boolean, default=False) user = db.relationship(User, backref=db.backref('pastes')) syntax = db.relationship(Syntax, backref=db.backref('pastes')) followers = db.relationship(User, secondary=follow_user_paste) tags = db.relationship('Tag', secondary=paste_tag, order_by=Tag.name, backref="pastes") comments = db.relationship('PasteComment', order_by="PasteComment.created_time", backref='pastes') def __init__(self, syntax_id, user_id): self.syntax_id = syntax_id self.title = u'未知标题' self.user_id = user_id self.view_num = 0 self.created_time = self.modified_time = datetime.now() self.revision = 0 self.is_private = False def __repr__(self): return "<Paste (%s@%s)>" % (self.title, self.user_id) @property def is_user_favorited(self): """检查用户是否收藏了该代码""" if not getUserObject(): return None return self in getUserObject().favorites def get_related_pastes(self, num): return Paste.query.filter_by(syntax_id=self.syntax_id).filter( Paste.id != self.id).filter(Paste.is_private == False).order_by( 'created_time DESC').all()[:num] @property def is_user_followed(self): return getUserObject() in self.followers def get_sliced_title(self, slice): if len(self.title) <= slice: return self.title return "%s ..." % self.title[:slice]
class User(db.Model): """ 用户表 修改email地址时需要经过验证 """ __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) openid = db.Column(db.Text, nullable=True) email = db.Column(db.String(45), unique=True, nullable=False) # 登陆使用的 nickname = db.Column(db.String(45), unique=True, nullable=False) # 显示时用的 password = db.Column(db.String(45), nullable=True) is_email_verified = db.Column(db.Boolean, nullable=False) slug = db.Column(db.String(45), nullable=True) paste_num = db.Column(db.Integer, nullable=False) created_time = db.Column(db.DateTime, nullable=False) modified_time = db.Column(db.DateTime, nullable=False) last_login_time = db.Column(db.DateTime, default=datetime.now()) privilege = db.Column(db.Integer, default=3) info = db.relationship('UserInfo', uselist=False) favorites = db.relationship('Paste', secondary=favorite_paste_user, order_by='Paste.created_time', backref="users") followers = db.relationship( "User", secondary=follow_user_user, primaryjoin=id == follow_user_user.c.from_user_id, secondaryjoin=id == follow_user_user.c.to_user_id) followed = db.relationship( "User", secondary=follow_user_user, secondaryjoin=id == follow_user_user.c.from_user_id, primaryjoin=id == follow_user_user.c.to_user_id) def __init__(self, nickname, email): self.nickname = nickname self.email = email self.paste_num = 0 self.created_time = self.modified_time = datetime.now() self.is_email_verified = True def __repr__(self): return "<User (%s|%s)>" % (self.nickname, self.email) def set_password(self, password): self.password = hash_password(password) @property def url(self): if self.slug: return url_for('userview.view', slug=self.slug) return url_for('userview.view', user_id=self.id) @property def is_user_followed(self): return getUserObject() in self.followers @property def unread_messages(self): return Message.query.filter_by(to_user_id=self.id).all() @property def followed_users(self): return self.followed def check_privilege(self, privilege): return self.privilege >= privilege def get_avatar_url(self, size=128): if self.info.avatar: return self.info.avatar else: return "http://www.gravatar.com/avatar/%s?size=%s&d=%s/static/images/avatar/default.jpg" % ( hashlib.md5(self.email).hexdigest(), size, request.url_root)
class Tag(db.Model): """ 标签表 要求: 1. 以小写存储 2. 空格要替换成为'-' 3. '和"都去掉 """ __tablename__ = 'tags' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(45), unique=True, nullable=False) description = db.Column(db.Text, nullable=True) times = db.Column(db.Integer) created_time = db.Column(db.DateTime, default=datetime.now()) modified_time = db.Column(db.DateTime, default=datetime.now()) followers = db.relationship('User', secondary=follow_user_tag) def __init__(self, name): self.name = name.replace(' ', '-').replace('"', '').replace("'", '') self.times = 1 def __repr__(self): return "Tag <%s>" % self.name @classmethod def getTags(self, num): tags = Tag.query.all()[:num] return [tag.name for tag in tags] def is_focused(self): return getUserObject() in self.followers @classmethod def updateTags(self, model, tags=[]): """ model: 要创建tag的model tags: 页面传过来的tag, list形式 这里的逻辑有些复杂, 有必要写一个详细的注释 准则: 1. 检查tag是否超过3个 2. 不管什么大小写, 不能有重复的 """ old_tags = [tag.name for tag in model.tags] tags_to_add = set(tags) - set(old_tags) for tag_left in tags_to_add: for tag_right in old_tags: if tag_left.lower() == tag_right.lower(): tags_to_add.remove(tag_left) tags_to_del = set(old_tags) - set(tags) for tag in tags_to_add: t = Tag.query.filter_by(name='%s' % strip_tag(tag)).first() if not t: t = Tag(strip_tag(tag)) db.session.add(t) else: t.times = t.times + 1 db.session.add(t) model.tags.append(t) for tag in tags_to_del: t = Tag.query.filter_by(name='%s' % strip_tag(tag)).first() if t: model.tags.remove(t) t.times = t.times - 1 db.session.add(t) db.session.add(model) @property def is_user_followed(self): return getUserObject() in self.followers