views.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: views.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-05-20 13:47:04 (CST)
  9. # Last Update:星期二 2016-6-14 23:20:13 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (Blueprint, render_template, redirect, url_for, request)
  14. from flask.views import MethodView
  15. from flask_login import login_required
  16. from flask_maple.forms import flash_errors
  17. from maple import app
  18. from maple.main.models import SQLData, RedisData
  19. from maple.main.permission import topic_permission, reply_permission
  20. from maple.helpers import is_num
  21. from maple.forums.models import Board
  22. from maple.topic.models import Topic
  23. from maple.topic.forms import TopicForm, ReplyForm
  24. site = Blueprint('topic', __name__)
  25. @site.route('/ask')
  26. @login_required
  27. def ask():
  28. form = TopicForm()
  29. boardId = request.args.get('boardId')
  30. if boardId is not None:
  31. board = Board.query.filter_by(id=boardId).first()
  32. form.category.data = board.id
  33. return render_template('topic/ask.html', form=form)
  34. # @site.route('/topic/<uid>/edit', methods=['POST'])
  35. # def edit(uid):
  36. # topic = Topic.query.filter_by(uid=uid).first_or_404()
  37. # form = TopicForm()
  38. # form.title.data = topic.title
  39. # tags = ''
  40. # leng = 1
  41. # for tag in topic.tags:
  42. # if leng == len(list(topic.tags)):
  43. # tags += tag.tagname
  44. # else:
  45. # tags += tag.tagname + ','
  46. # leng += 1
  47. # form.tags.data = tags
  48. # form.content.data = topic.content
  49. # return jsonify(form=form)
  50. # # return render_template('topic/edit.html', form=form)
  51. class TopicAPI(MethodView):
  52. decorators = [topic_permission]
  53. def template_with_uid(self, form, topic, replies):
  54. data = {'topic': topic, 'replies': replies, 'form': form}
  55. return render_template('topic/content.html', **data)
  56. def template_without_uid(self, topics):
  57. return render_template('topic/topic.html', topics=topics)
  58. def get(self, uid):
  59. page = is_num(request.args.get('page'))
  60. if uid is None:
  61. topics = Topic.query.paginate(page,
  62. app.config['PER_PAGE'],
  63. error_out=True)
  64. return self.template_without_uid(topics)
  65. else:
  66. form = ReplyForm()
  67. topic = Topic.query.filter_by(uid=str(uid)).first_or_404()
  68. replies = topic.replies.paginate(page, 5, True)
  69. RedisData.set_read_count(topic.id)
  70. return self.template_with_uid(form, topic, replies)
  71. def post(self):
  72. form = TopicForm()
  73. if form.validate_on_submit():
  74. SQLData.set_topics(form)
  75. return redirect('/')
  76. else:
  77. if form.errors:
  78. flash_errors(form)
  79. form.title.data = form.title.data
  80. else:
  81. pass
  82. form.title.data = form.title.data
  83. return redirect(url_for('topic.ask'))
  84. def put(self, uid):
  85. form = TopicForm()
  86. if form.validate_on_submit():
  87. pass
  88. def delete(self, uid):
  89. return 'delete'
  90. class ReplyAPI(MethodView):
  91. decorators = [reply_permission]
  92. def post(self, uid):
  93. form = ReplyForm()
  94. topic = Topic.query.filter_by(id=uid).first()
  95. if form.validate_on_submit():
  96. SQLData.set_replies(form, uid)
  97. return redirect('/topic/' + topic.uid)
  98. else:
  99. if form.errors:
  100. flash_errors(form)
  101. else:
  102. pass
  103. return redirect(url_for('topic.topic',
  104. uid=str(topic.uid),
  105. _anchor='comment'))
  106. def put(self, uid):
  107. return 'put'
  108. def delete(self, uid):
  109. return 'delete'
  110. topic_view = TopicAPI.as_view('topic')
  111. site.add_url_rule('/topic',
  112. defaults={'uid': None},
  113. view_func=topic_view,
  114. methods=['GET', ])
  115. site.add_url_rule('/topic', view_func=topic_view, methods=['POST', ])
  116. site.add_url_rule('/topic/<uid>',
  117. view_func=topic_view,
  118. methods=['GET', 'PUT', 'DELETE'])
  119. site.add_url_rule('/reply/<uid>',
  120. view_func=ReplyAPI.as_view('reply'),
  121. methods=['POST', 'PUT'])