orderby.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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-25 18:30:13 (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. topic_base = Topic.query.join(Topic.board).filter(
  49. Topic.publish > time, Topic.is_top == False)
  50. if t3 == 0:
  51. if type == 'parent_b':
  52. topics = topic_base.filter(Board.parent_board == uid).paginate(
  53. page, 20, True)
  54. elif type == 'child_b':
  55. topics = topic_base.filter(Board.id == uid).paginate(page, 20,
  56. True)
  57. elif type == 'tags':
  58. topics = Topic.query.join(Topic.tags).filter(
  59. Topic.publish > time, Topic.is_top == False, Tags.tagname
  60. == uid).order_by(Topic.publish.desc()).paginate(page, 20,
  61. True)
  62. else:
  63. topics = None
  64. return topics
  65. else:
  66. if type == 'parent_b':
  67. topics = topic_base.filter(Board.parent_board == uid).order_by(
  68. Topic.publish.asc()).paginate(page, 20, True)
  69. elif type == 'child_b':
  70. topics = topic_base.filter(Board.id == uid).order_by(
  71. Topic.publish.asc()).paginate(page, 20, True)
  72. elif type == 'tags':
  73. topics = Topic.query.join(Topic.tags).filter(
  74. Topic.publish > time, Topic.is_top == False,
  75. Tags.tagname ==
  76. uid).order_by(Topic.publish.asc()).paginate(page, 20, True)
  77. else:
  78. topics = None
  79. return topics
  80. # 作者
  81. else:
  82. topic_base = Topic.query.join(Topic.board).filter(
  83. Topic.publish > time, Topic.is_top == False)
  84. if t3 == 0:
  85. if type == 'parent_b':
  86. topics = topic_base.filter(Board.parent_board == uid).order_by(
  87. Topic.author_id.desc()).paginate(page, 20, True)
  88. elif type == 'child_b':
  89. topics = topic_base.filter(Board.id == uid).order_by(
  90. Topic.author_id.desc()).paginate(page, 20, True)
  91. elif type == 'tags':
  92. topics = Topic.query.join(Topic.tags).filter(
  93. Topic.publish > time, Topic.is_top == False, Tags.tagname
  94. == uid).order_by(Topic.author_id.desc()).paginate(page, 20,
  95. True)
  96. else:
  97. topics = None
  98. return topics
  99. else:
  100. if type == 'parent_b':
  101. topics = topic_base.filter(Board.parent_board == uid).order_by(
  102. Topic.author_id.asc()).paginate(page, 20, True)
  103. elif type == 'child_b':
  104. topics = topic_base.filter(Board.id == uid).order_by(
  105. Topic.author_id.asc()).paginate(page, 20, True)
  106. elif type == 'tags':
  107. topics = Topic.query.join(Topic.tags).filter(
  108. Topic.publish > time, Topic.is_top == False, Tags.tagname
  109. == uid).order_by(Topic.author_id.asc()).paginate(page, 20,
  110. True)
  111. else:
  112. topics = None
  113. return topics