views.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 18:04:43 (CST)
  9. # Last Update:星期二 2016-6-14 23:20:13 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (Blueprint, render_template, request, redirect, url_for,
  14. jsonify)
  15. from flask.views import MethodView
  16. from flask_maple.forms import flash_errors
  17. from flask_login import current_user
  18. from flask_maple.forms import return_errors
  19. from maple import app, db
  20. from maple.main.permission import follow_permission, collect_permission
  21. from maple.helpers import is_num
  22. from maple.topic.models import Topic, Reply, Collect, Tags
  23. from maple.forums.models import Notice
  24. from maple.user.models import User
  25. from maple.mine.forms import CollectForm
  26. site = Blueprint('mine', __name__)
  27. class CollectAPI(MethodView):
  28. decorators = [collect_permission]
  29. def template_with_uid(self, topics, collect):
  30. form = CollectForm()
  31. form.name.data = collect.name
  32. form.description.data = collect.description
  33. form.is_privacy.data = 0 if collect.is_privacy else 1
  34. data = {'topics': topics, 'collect': collect, 'form': form}
  35. return render_template('mine/collect.html', **data)
  36. def template_without_uid(self, collects):
  37. form = CollectForm()
  38. data = {'collects': collects, 'form': form}
  39. return render_template('mine/collect_list.html', **data)
  40. def get(self, uid):
  41. page = is_num(request.args.get('page'))
  42. if uid is None:
  43. topics = current_user.collects.paginate(page,
  44. app.config['PER_PAGE'],
  45. error_out=True)
  46. return self.template_without_uid(topics)
  47. else:
  48. collect = Collect.query.filter_by(id=uid).first()
  49. topics = collect.topics.paginate(page, 10, True)
  50. return self.template_with_uid(topics, collect)
  51. def post(self):
  52. form = CollectForm()
  53. if form.validate_on_submit():
  54. collect = Collect()
  55. collect.name = form.name.data
  56. collect.description = form.description.data
  57. collect.is_privacy = True if form.is_privacy.data == 0 else False
  58. collect.author = current_user
  59. current_user.following_collects.append(collect)
  60. db.session.add(collect)
  61. db.session.commit()
  62. return redirect(url_for('mine.collect'))
  63. else:
  64. if form.errors:
  65. flash_errors(form)
  66. return redirect(url_for('mine.collect'))
  67. def put(self, uid):
  68. form = CollectForm()
  69. if form.validate_on_submit():
  70. collect = Collect.query.filter_by(id=uid).first_or_404()
  71. collect.name = form.name.data
  72. collect.description = form.description.data
  73. collect.is_privacy = True if form.is_privacy.data == 0 else False
  74. db.session.commit()
  75. return jsonify(judge=True)
  76. else:
  77. if form.errors:
  78. return return_errors(form)
  79. return jsonify(judge=False)
  80. def delete(self, uid):
  81. collect = Collect.query.filter_by(id=uid).first_or_404()
  82. db.session.delete(collect)
  83. db.session.commit()
  84. return jsonify(judge=True)
  85. @site.route('/collect/following')
  86. def collect_following():
  87. return redirect(url_for('mine.follow', type='collect'))
  88. @site.route('/add-to-collect', methods=['POST'])
  89. def add_collect():
  90. form = request.form.getlist('add-to-collect')
  91. topicId = request.args.get('topicId')
  92. topic = Topic.query.filter_by(uid=topicId).first_or_404()
  93. for id in form:
  94. collect = Collect.query.filter_by(id=id).first_or_404()
  95. collect.topics.append(topic)
  96. db.session.commit()
  97. return redirect(url_for('topic.topic', uid=topic.uid))
  98. class LikeAPI(MethodView):
  99. def get(self, uid):
  100. if uid is None:
  101. page = is_num(request.args.get('page'))
  102. replies = current_user.likes.paginate(page,
  103. app.config['PER_PAGE'],
  104. error_out=True)
  105. return render_template('mine/reply.html', replies=replies)
  106. else:
  107. return redirect(url_for('topic.reply', rid=uid))
  108. def post(self, uid):
  109. tid = request.args.get('tid')
  110. reply = Reply.query.filter_by(id=uid).first_or_404()
  111. current_user.likes.append(reply)
  112. db.session.commit()
  113. return redirect(url_for('topic.topic', uid=tid))
  114. def delete(self, uid):
  115. reply = Reply.query.filter_by(id=uid).first_or_404()
  116. db.session.delete(reply)
  117. db.session.commit()
  118. return 's'
  119. class FollowAPI(MethodView):
  120. decorators = [follow_permission]
  121. def template_without_uid(self, topics):
  122. return render_template('mine/follow_list.html', follows=topics)
  123. def get(self, type):
  124. page = is_num(request.args.get('page'))
  125. if type == 'tag':
  126. return render_template('user/following_tag.html',
  127. following_type=type)
  128. elif type == 'user':
  129. return render_template('user/following_user.html',
  130. following_type=type)
  131. elif type == 'collect':
  132. return render_template('user/following_collect.html',
  133. following_type=type)
  134. else:
  135. return render_template('user/following_topic.html',
  136. following_type=type)
  137. def post(self):
  138. data = request.get_json()
  139. type = data['type']
  140. id = data['id']
  141. if type == 'tag':
  142. tag = Tags.query.filter_by(id=id).first()
  143. current_user.following_tags.append(tag)
  144. db.session.commit()
  145. return jsonify(judge=True)
  146. elif type == 'topic':
  147. topic = Topic.query.filter_by(id=id).first()
  148. current_user.following_topics.append(topic)
  149. db.session.commit()
  150. return jsonify(judge=True)
  151. elif type == 'user':
  152. user = User.query.filter_by(id=id).first()
  153. current_user.following_users.append(user)
  154. db.session.commit()
  155. return jsonify(judge=True)
  156. elif type == 'collect':
  157. collect = Collect.query.filter_by(id=id).first()
  158. current_user.following_collects.append(collect)
  159. db.session.commit()
  160. return jsonify(judge=True)
  161. else:
  162. pass
  163. return jsonify(judge=False)
  164. def delete(self):
  165. data = request.get_json()
  166. type = data['type']
  167. id = data['id']
  168. if type == 'tag':
  169. tag = Tags.query.filter_by(id=id).first()
  170. current_user.following_tags.remove(tag)
  171. db.session.commit()
  172. return jsonify(judge=True)
  173. elif type == 'topic':
  174. topic = Topic.query.filter_by(id=id).first()
  175. current_user.following_topics.remove(topic)
  176. db.session.commit()
  177. return jsonify(judge=True)
  178. elif type == 'user':
  179. pass
  180. elif type == 'collect':
  181. collect = Collect.query.filter_by(id=id).first()
  182. current_user.following_collects.remove(collect)
  183. db.session.commit()
  184. return jsonify(judge=True)
  185. else:
  186. pass
  187. return jsonify(judge=False)
  188. class NoticeAPI(MethodView):
  189. def template_without_uid(self, notices):
  190. return render_template('topic/topic_good.html', notices=notices)
  191. def get(self, uid):
  192. if uid is None:
  193. page = is_num(request.args.get('page'))
  194. notices = Notice.query.filter_by(
  195. user=current_user.username).paginate(page,
  196. app.config['PER_PAGE'],
  197. error_out=True)
  198. return self.template_without_uid(notices)
  199. else:
  200. return redirect(url_for('topic.topic', uid=uid))
  201. def post(self, uid):
  202. topic = Topic.query.filter_by(uid=uid).first()
  203. topic.is_good = True
  204. db.session.commit()
  205. def put(self, uid):
  206. topic = Topic.query.filter_by(uid=uid).first()
  207. topic.is_good = False
  208. db.session.commit()
  209. def register_api(view, endpoint, url):
  210. view_func = view.as_view(endpoint)
  211. site.add_url_rule(url,
  212. defaults={'uid': None},
  213. view_func=view_func,
  214. methods=['GET', 'POST', 'DELETE'])
  215. def register_draft(view, endpoint, url):
  216. view_func = view.as_view(endpoint)
  217. site.add_url_rule(url,
  218. defaults={'uid': None},
  219. view_func=view_func,
  220. methods=['GET', 'POST'])
  221. site.add_url_rule('%s/<int:uid>' % url,
  222. view_func=view_func,
  223. methods=['GET', 'PUT', 'DELETE'])
  224. collect_view = CollectAPI.as_view('collect')
  225. site.add_url_rule('/collect',
  226. defaults={'uid': None},
  227. view_func=collect_view,
  228. methods=['GET', ])
  229. site.add_url_rule('/collect', view_func=collect_view, methods=['POST', ])
  230. site.add_url_rule('/collect/<uid>',
  231. view_func=collect_view,
  232. methods=['GET', 'PUT', 'DELETE'])
  233. follow_view = FollowAPI.as_view('follow')
  234. site.add_url_rule('/follow',
  235. defaults={'type': 'topics'},
  236. view_func=follow_view,
  237. methods=['GET', ])
  238. site.add_url_rule('/follow', view_func=follow_view, methods=['POST', 'DELETE'])
  239. site.add_url_rule('/follow/<type>', view_func=follow_view, methods=['GET'])
  240. register_api(LikeAPI, 'like', '/likes')
  241. # register_api(FollowAPI, 'follow', '/follows')
  242. # register_api(DraftAPI, 'draft', '/draft')
  243. # register_api(CollectAPI, 'collect', '/collects')
  244. # register_api(LikeAPI, 'like', '/likes')
  245. # register_api(FollowAPI, 'follow', '/follows')
  246. # register_api(InviteAPI, 'invite', '/invites')