orderby.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: orderby.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-05 10:15:58 (CST)
  9. # Last Update:星期二 2016-6-14 23:20:14 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import request
  14. from maple.forums.models import Board
  15. from maple.topic.models import Tags, Topic
  16. from datetime import datetime, timedelta
  17. def form_judge(form):
  18. '''
  19. 0: all topic
  20. 1: one day
  21. 2: one week
  22. 3: one month
  23. 0: time
  24. 1: author
  25. 0: desc
  26. 1: asc
  27. '''
  28. t1 = form.display.data
  29. t2 = form.sort.data
  30. t3 = form.st.data
  31. data = form_sort(t1, t2, t3)
  32. return data
  33. def form_sort(t1, t2, t3):
  34. orderby = request.get_json()
  35. type = orderby['type']
  36. uid = orderby['uid']
  37. page = int(orderby['page'])
  38. if t1 == 0:
  39. time = datetime.now() - timedelta(days=365)
  40. elif t1 == 1:
  41. time = datetime.now() - timedelta(days=1)
  42. elif t1 == 2:
  43. time = datetime.now() - timedelta(days=7)
  44. else:
  45. time = datetime.now() - timedelta(days=30)
  46. # 发表时间
  47. if t2 == 0:
  48. if t3 == 0:
  49. if type == 'parent_b':
  50. topics = Topic.query.join(Topic.board).filter(
  51. Topic.publish > time, Board.parent_board ==
  52. uid).order_by(Topic.publish.desc()).paginate(page, 20,
  53. True)
  54. elif type == 'child_b':
  55. topics = Topic.query.join(Topic.board).filter(
  56. Topic.publish > time,
  57. Board.id == uid).order_by(Topic.publish.desc()).paginate(
  58. page, 20, True)
  59. elif type == 'tags':
  60. topics = Topic.query.join(Topic.tags).filter(
  61. Topic.publish > time, Tags.tagname ==
  62. uid).order_by(Topic.publish.desc()).paginate(page, 20,
  63. True)
  64. else:
  65. topics = None
  66. return topics
  67. else:
  68. if type == 'parent_b':
  69. topics = Topic.query.join(Topic.board).filter(
  70. Topic.publish > time, Board.parent_board ==
  71. uid).order_by(Topic.publish.asc()).paginate(page, 20, True)
  72. elif type == 'child_b':
  73. topics = Topic.query.join(Topic.board).filter(
  74. Topic.publish > time,
  75. Board.id == uid).order_by(Topic.publish.asc()).paginate(
  76. page, 20, True)
  77. elif type == 'tags':
  78. topics = Topic.query.join(Topic.tags).filter(
  79. Topic.publish > time, Tags.tagname ==
  80. uid).order_by(Topic.publish.asc()).paginate(page, 20, True)
  81. else:
  82. topics = None
  83. return topics
  84. # 作者
  85. else:
  86. if t3 == 0:
  87. if type == 'parent_b':
  88. topics = Topic.query.join(Topic.board).filter(
  89. Topic.publish > time, Board.parent_board ==
  90. uid).order_by(Topic.author_id.desc()).paginate(page, 20,
  91. True)
  92. elif type == 'child_b':
  93. topics = Topic.query.join(Topic.board).filter(
  94. Topic.publish > time,
  95. Board.id == uid).order_by(Topic.author_id.desc()).paginate(
  96. page, 20, True)
  97. elif type == 'tags':
  98. topics = Topic.query.join(Topic.tags).filter(
  99. Topic.publish > time, Tags.tagname ==
  100. uid).order_by(Topic.author_id.desc()).paginate(page, 20,
  101. True)
  102. else:
  103. topics = None
  104. return topics
  105. else:
  106. if type == 'parent_b':
  107. topics = Topic.query.join(Topic.board).filter(
  108. Topic.publish > time, Board.parent_board ==
  109. uid).order_by(Topic.author_id.asc()).paginate(page, 20,
  110. True)
  111. elif type == 'child_b':
  112. topics = Topic.query.join(Topic.board).filter(
  113. Topic.publish > time,
  114. Board.id == uid).order_by(Topic.author_id.asc()).paginate(
  115. page, 20, True)
  116. elif type == 'tags':
  117. topics = Topic.query.join(Topic.tags).filter(
  118. Topic.publish > time, Tags.tagname ==
  119. uid).order_by(Topic.author_id.asc()).paginate(page, 20,
  120. True)
  121. else:
  122. topics = None
  123. return topics