class Link(db.Model): __tablename__ = "links" PER_PAGE = 80 id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(50), nullable=False) link = db.Column(db.String(100), nullable=False) logo = db.Column(db.String(100)) description = db.Column(db.Unicode(100)) email = db.Column(db.String(50)) passed = db.Column(db.Boolean, default=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) class Permissions(object): def __init__(self, obj): self.obj = obj def __init__(self, *args, **kwargs): super(Link, self).__init__(*args, **kwargs) def __str__(self): return self.name def __repr__(self): return "<%s>" % self @cached_property def permissions(self): return self.Permissions(self) @cached_property def json(self): return dict(id=self.id, name=self.name, url=self.link, logo=self.logo, description=self.description, created_date=self.created_date) @cached_property def item(self): return storage(self.json)
class Tag(db.Model): __tablename__ = "tags" query_class = TagQuery id = db.Column(db.Integer, primary_key=True) slug = db.Column(db.Unicode(80), unique=True) posts = db.dynamic_loader(Post, secondary=post_tags, query_class=PostQuery) _name = db.Column("name", db.Unicode(80), unique=True) def __init__(self, *args, **kwargs): super(Tag, self).__init__(*args, **kwargs) def __str__(self): return self.name def __repr__(self): return "<%s>" % self def _get_name(self): return self._name def _set_name(self, name): self._name = name.lower().strip() self.slug = slugify(name) name = db.synonym("_name", descriptor=property(_get_name, _set_name)) @cached_property def url(self): return route.url_for('tag', self.slug) num_posts = db.column_property( db.select([db.func.count(post_tags.c.post_id)]).\ where(db.and_(post_tags.c.tag_id==id, Post.id==post_tags.c.post_id)).as_scalar())
class City(db.Model): __tablename__ = 'city' __table_args__ = ( db.UniqueConstraint('name'), ) id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(50), nullable=False) name_pinyin = db.Column(db.String(50)) acts = db.relationship("Act", backref='city', order_by='desc(Act.start_date)') #usercity = db.relationship("Author", backref='city', order_by='desc(MbaUser.creation_date)') @classmethod def _find_or_create(cls, name): with db.session.no_autoflush: obj = db.session.query(City).filter_by(name=name).first() if obj is None: obj = City(name=name) return obj
class Post(db.Model): __tablename__ = 'posts' PER_PAGE = 40 query_class = PostQuery id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey(User.id, ondelete='CASCADE'), nullable=False) _title = db.Column("title", db.Unicode(100), index=True) _slug = db.Column("slug", db.Unicode(50), unique=True, index=True) content = db.Column(db.UnicodeText) created_date = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) post_type = db.Column(db.String(32), nullable=False) _tags = db.Column("tags", db.Unicode(100), index=True) author = db.relation(User, innerjoin=True, lazy="joined") __mapper_args__ = {'order_by': id.desc(), 'polymorphic_on': post_type, 'polymorphic_identity': 'posts'} class Permissions(object): def __init__(self, obj): self.obj = obj @cached_property def edit(self): return Permission(UserNeed(self.obj.author_id)) @cached_property def delete(self): return Permission(UserNeed(self.obj.author_id)) def __init__(self, *args, **kwargs): super(Post, self).__init__(*args, **kwargs) def __str__(self): return self.title def __repr__(self): return "<%s>" % self @cached_property def permissions(self): return self.Permissions(self) def _get_title(self): return self._title def _set_title(self, title): self._title = title.lower().strip() if self.slug is None: self.slug = slugify(title)[:50] title = db.synonym("_title", descriptor=property(_get_title, _set_title)) def _get_slug(self): return self._slug def _set_slug(self, slug): if slug: self._slug = slugify(slug) slug = db.synonym("_slug", descriptor=property(_get_slug, _set_slug)) def _get_tags(self): return self._tags def _set_tags(self, tags): self._tags = tags if self.id: # ensure existing tag references are removed d = db.delete(post_tags, post_tags.c.post_id==self.id) db.engine.execute(d) for tag in set(self.taglist): slug = slugify(tag) tag_obj = Tag.query.filter(Tag.slug==slug).first() if tag_obj is None: tag_obj = Tag(name=tag, slug=slug) db.session.add(tag_obj) tag_obj.posts.append(self) tags = db.synonym("_tags", descriptor=property(_get_tags, _set_tags)) @cached_property def url(self): return route.url_for('post_view', self.created_date.year, self.created_date.month, self.created_date.day, self.slug.encode('utf8')) @cached_property def taglist(self): if self.tags is None: return [] tags = [t.strip() for t in self.tags.split(",")] return [t for t in tags if t] @cached_property def linked_taglist(self): return [(tag, route.url_for('tag', slugify(tag))) \ for tag in self.taglist] @cached_property def prev_post(self): prev_post = Post.query.filter(Post.created_date < self.created_date) \ .first() return prev_post @cached_property def next_post(self): next_post = Post.query.filter(Post.created_date > self.created_date) \ .order_by('created_date').first() return next_post @cached_property def summary(self): s = re.findall(r'(<hr>)', self.content) if not s: return self.content p = s[0] return endtags(self.content.split(p)[0]) @property def comments(self): """ Returns comments in tree. Each parent comment has a "comments" attribute appended and a "depth" attribute. """ comments = Comment.query.filter(Comment.post_id==self.id).all() def _get_comments(parent, depth): parent.comments = [] parent.depth = depth for comment in comments: if comment.parent_id == parent.id: parent.comments.append(comment) _get_comments(comment, depth + 1) parents = [c for c in comments if c.parent_id is None] for parent in parents: _get_comments(parent, 0) return parents @cached_property def json(self): return dict(id=self.id, title=self.title, content=self.content, author=self.author.username)
class Comment(db.Model): __tablename__ = "comments" query_class = CommentQuery PER_PAGE = 40 id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey(Post.id, ondelete='CASCADE'), nullable=False) author_id = db.Column(db.Integer, db.ForeignKey(User.id, ondelete='CASCADE')) parent_id = db.Column(db.Integer, db.ForeignKey("comments.id", ondelete='CASCADE')) email = db.Column(db.String(50)) nickname = db.Column(db.Unicode(50)) website = db.Column(db.String(100)) comment = db.Column(db.UnicodeText) created_date = db.Column(db.DateTime, default=datetime.utcnow) ip = db.Column(db.String(20)) _author = db.relation(User, backref="posts", lazy="joined") post = db.relation(Post, innerjoin=True, lazy="joined") parent = db.relation('Comment', lazy="joined", remote_side=[id]) __mapper_args__ = {'order_by' : id.asc()} class Permissions(object): def __init__(self, obj): self.obj = obj @cached_property def edit(self): return Permission(UserNeed(self.obj.author_id)) @cached_property def reply(self): return Permission(UserNeed(self.obj.post.author_id)) @cached_property def delete(self): return admin & moderator def __init__(self, *args, **kwargs): super(Comment, self).__init__(*args, **kwargs) def __str__(self): return self.comment def __repr__(self): return "<%s>" % self @cached_property def permissions(self): return self.Permissions(self) def _get_author(self): if self._author: self._author.website = None return self._author return storage(email = self.email, nickname = self.nickname, website = self.website) def _set_author(self, author): self._author = author author = db.synonym("_author", descriptor=property(_get_author, _set_author)) @cached_property def url(self): return "%s#comment-%s" % (self.post.url, self.id) @cached_property def markdown(self): return markdown(self.comment or '') @cached_property def json(self): return dict(id=self.id, author=self.author, url=self.url, comment=self.comment, created_date=self.created_date) @cached_property def item(self): return storage(self.json)