Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. user = UserModel.select().where(UserModel.chat == chat)
  2. user_shows = ShowsModel.select(ShowsModel.content).distinct().where(ShowsModel.user == user)
  3. content = (ContentModel.select(ContentModel.id).
  4. where((ContentModel.type != 'text') &
  5. (ContentModel.id.not_in(user_shows) &
  6. (ContentModel.reply.is_null())) &
  7. (ContentModel.user != user)).
  8. order_by(-ContentModel.time).
  9. limit(500)).alias('content')
  10. shows = (ShowsModel.select(ShowsModel.id, ShowsModel.content, ShowsModel.reaction).where(
  11. ShowsModel.content.in_(content)))
  12.  
  13. count = fn.COUNT(ShowsModel.id).alias('count')
  14. reactions = (shows.select(ShowsModel.content, ShowsModel.reaction, count).
  15. group_by(ShowsModel.content, ShowsModel.reaction))
  16.  
  17. rnull_shows = (reactions.select(ShowsModel.content, count).where(ShowsModel.reaction.is_null())).alias('r_null')
  18. r1_shows = (reactions.select(ShowsModel.content, count).where(ShowsModel.reaction == 1)).alias('r1')
  19. r2_shows = (reactions.select(ShowsModel.content, count).where(ShowsModel.reaction == 2)).alias('r2')
  20. r3_shows = (reactions.select(ShowsModel.content, count).where(ShowsModel.reaction == 3)).alias('r3')
  21. r4_shows = (reactions.select(ShowsModel.content, count).where(ShowsModel.reaction == 4)).alias('r4')
  22. content_values = (
  23. ContentModel.select(ContentModel.id,
  24. fn.COALESCE(r1_shows.c.count, 0).alias('r1_count'),
  25. fn.COALESCE(r2_shows.c.count, 0).alias('r2_count'),
  26. fn.COALESCE(r3_shows.c.count, 0).alias('r3_count'),
  27. fn.COALESCE(r4_shows.c.count, 0).alias('r4_count'),
  28. fn.COALESCE(rnull_shows.c.count, 0).alias('rnull_count')).
  29. join(content, on=(content.c.id == ContentModel.id)).
  30. join(r1_shows, JOIN.FULL_OUTER, on=(ContentModel.id == r1_shows.c.content_id)).
  31. join(r2_shows, JOIN.FULL_OUTER, on=(ContentModel.id == r2_shows.c.content_id)).
  32. join(r3_shows, JOIN.FULL_OUTER, on=(ContentModel.id == r3_shows.c.content_id)).
  33. join(r4_shows, JOIN.FULL_OUTER, on=(ContentModel.id == r4_shows.c.content_id)).
  34. join(rnull_shows, JOIN.FULL_OUTER, on=(ContentModel.id == rnull_shows.c.content_id))
  35. ).alias('content_values')
  36.  
  37. metric = (
  38. content_values.c.r1_count * (-5) +
  39. content_values.c.r2_count * (-3) +
  40. content_values.c.r3_count * (-1) +
  41. content_values.c.r4_count * (+1)
  42. )
  43. top_metric = (ContentModel.select(ContentModel.id, metric.alias('metric')).
  44. join(content_values, on=(content_values.c.id == ContentModel.id)).
  45. order_by(-metric).
  46. limit(100))
  47.  
  48. query = (ContentModel.select().
  49. join(top_metric, on=(top_metric.c.id == ContentModel.id)).
  50. order_by(fn.Random()))
  51.  
  52. query.first()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement