Guest User

Untitled

a guest
Jul 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. def related_tags(self, tags, max=37):
  2. from fuku.model.post import Post
  3. if isinstance(tags, basestring):
  4. tags = self.parse(tags)
  5. if tags:
  6. joins, cause, having = self.__make_query(tags)
  7. in_q = sql.select([TagPost.post_id.label('id')],
  8. from_obj=joins). \
  9. group_by(TagPost.post_id)
  10. if cause: in_q = in_q.where(sql.and_(*cause))
  11. if having: in_q = in_q.having(having)
  12. # Do something, this thing is really hard to understand.
  13. # ...such as document how it actually works.
  14. q = sql.select([Tag.id, Tag.name,
  15. sql.func.count(Tag.name).label('count')],
  16. from_obj=in_q.alias('posts'). \
  17. join(TagPost.__table__,
  18. TagPost.post_id==Post.id)). \
  19. where(Tag.id==TagPost.tag_id). \
  20. group_by(Tag.name, Tag.id). \
  21. limit(max).order_by(sql.desc('count'))
  22. return meta.Session.execute(q).fetchall()
Add Comment
Please, Sign In to add comment