Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def related_tags(self, tags, max=37):
- from fuku.model.post import Post
- if isinstance(tags, basestring):
- tags = self.parse(tags)
- if tags:
- joins, cause, having = self.__make_query(tags)
- in_q = sql.select([TagPost.post_id.label('id')],
- from_obj=joins). \
- group_by(TagPost.post_id)
- if cause: in_q = in_q.where(sql.and_(*cause))
- if having: in_q = in_q.having(having)
- # Do something, this thing is really hard to understand.
- # ...such as document how it actually works.
- q = sql.select([Tag.id, Tag.name,
- sql.func.count(Tag.name).label('count')],
- from_obj=in_q.alias('posts'). \
- join(TagPost.__table__,
- TagPost.post_id==Post.id)). \
- where(Tag.id==TagPost.tag_id). \
- group_by(Tag.name, Tag.id). \
- limit(max).order_by(sql.desc('count'))
- return meta.Session.execute(q).fetchall()
Add Comment
Please, Sign In to add comment