views.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #*************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: academy.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2016-02-07 12:34:33
  7. #*************************************************************************
  8. #!/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from flask import render_template, Blueprint, flash, request, g, abort, jsonify,\
  11. redirect, url_for, session, current_app
  12. from flask_login import logout_user, current_user, login_required
  13. from flask_principal import AnonymousIdentity, \
  14. identity_changed
  15. from werkzeug.security import generate_password_hash
  16. from maple.question.models import Questions, Replies, Collector
  17. from maple.user.models import User, UserSetting,Role,Permission
  18. from maple.user.forms import SettingForm, NewPasswdForm, PrivacyForm
  19. from maple.group.models import Message
  20. from maple.main.permissions import own_permission
  21. from maple.forms.forms import return_errors
  22. from maple import redis_data, db
  23. site = Blueprint('user', __name__)
  24. @site.url_value_preprocessor
  25. def pull_user_url(endpoint, values):
  26. url = values.pop('user_url')
  27. g.user_url = url
  28. @site.url_defaults
  29. def add_user_url(endpoint, values):
  30. if 'user_url' in values or not g.user_url:
  31. return
  32. values['user_url'] = g.user_url
  33. @site.route('')
  34. def index():
  35. user = User.query.filter_by(name=g.user_url).first_or_404()
  36. if g.user is not None and g.user.is_authenticated:
  37. user_count = redis_data.hget('user:%s' % str(current_user.id), 'topic')
  38. if not user_count:
  39. user_count = 0
  40. else:
  41. user_count = int(user_count)
  42. return render_template('user/user.html',
  43. user_count=user_count,
  44. category='',
  45. user=user)
  46. else:
  47. return render_template('user/user.html', user=user, category='')
  48. @site.route('/<category>', defaults={'number': 1})
  49. @site.route('/<category>&page=<int:number>')
  50. def category(category, number):
  51. user = User.query.filter_by(name=g.user_url).first_or_404()
  52. user_count = redis_data.hget('user:%s' % str(user.id), 'topic')
  53. if not user_count:
  54. user_count = 0
  55. else:
  56. user_count = int(user_count)
  57. return render_template('user/user.html',
  58. user=user,
  59. category=category,
  60. user_count=user_count)
  61. @site.route('/settings', methods=['GET', 'POST'])
  62. @login_required
  63. @own_permission
  64. def setting():
  65. '''用户设置'''
  66. error = None
  67. form = SettingForm()
  68. passwd_form = NewPasswdForm()
  69. mode = request.args.get('mode')
  70. if mode == 'setting':
  71. if form.validate_on_submit() and request.method == "POST":
  72. introduce = form.introduce.data
  73. school = form.school.data
  74. word = form.word.data
  75. current_user.infor.introduce = introduce
  76. current_user.infor.school = school
  77. current_user.infor.word = word
  78. db.session.commit()
  79. flash('资料更新成功')
  80. return jsonify(judge=True, error=error)
  81. else:
  82. if form.errors:
  83. return return_errors(form)
  84. else:
  85. pass
  86. return redirect(url_for('user.setting'))
  87. elif mode == 'password':
  88. if passwd_form.validate_on_submit() and request.method == "POST":
  89. user = User.query.filter_by(name=current_user.name).first()
  90. passwd = passwd_form.passwd.data
  91. rpasswd = passwd_form.rpasswd.data
  92. if not User.check_password(user.passwd, passwd):
  93. error = u'密码错误'
  94. return jsonify(judge=False, error=error)
  95. else:
  96. user.passwd = generate_password_hash(rpasswd)
  97. db.session.commit()
  98. logout_user()
  99. session.clear()
  100. for key in ('identity.id', 'identity.auth_type'):
  101. session.pop(key, None)
  102. identity_changed.send(current_app._get_current_object(),
  103. identity=AnonymousIdentity())
  104. flash('密码修改成功,请重新登陆')
  105. return jsonify(judge=True, error=error)
  106. else:
  107. if passwd_form.passwd.errors:
  108. error = passwd_form.passwd.errors
  109. return jsonify(judge=False, error=error)
  110. elif passwd_form.npasswd.errors:
  111. error = passwd_form.npasswd.errors
  112. return jsonify(judge=False, error=error)
  113. else:
  114. return redirect(url_for('user.setting'))
  115. else:
  116. form.school.data = current_user.infor.school
  117. form.word.data = current_user.infor.word
  118. form.introduce.data = current_user.infor.introduce
  119. return render_template('user/user_settings.html',
  120. category=category,
  121. passwd_form=passwd_form,
  122. form=form)
  123. @site.route('/settings/privacy', methods=['GET', 'POST'])
  124. @login_required
  125. @own_permission
  126. def privacy():
  127. error = None
  128. form = PrivacyForm()
  129. if form.validate_on_submit() and request.method == "POST":
  130. online_status = form.online_status.data
  131. topic_list = form.topic_list.data
  132. rep_list = form.rep_list.data
  133. ntb_list = form.ntb_list.data
  134. collect_list = form.collect_list.data
  135. current_user.setting.online_status = online_status
  136. current_user.setting.topic_list = topic_list
  137. current_user.setting.rep_list = rep_list
  138. current_user.setting.ntb_list = ntb_list
  139. current_user.setting.collect_list = collect_list
  140. db.session.commit()
  141. flash('更新成功')
  142. return jsonify(judge=True, error=error)
  143. else:
  144. if form.errors:
  145. return return_errors(form)
  146. else:
  147. pass
  148. form.online_status.data = current_user.setting.online_status
  149. form.topic_list.data = current_user.setting.topic_list
  150. form.rep_list.data = current_user.setting.rep_list
  151. form.ntb_list.data = current_user.setting.ntb_list
  152. form.collect_list.data = current_user.setting.collect_list
  153. return render_template('user/user_privacy.html', form=form)
  154. @site.route('/daily')
  155. @login_required
  156. @own_permission
  157. def daily():
  158. '''签到'''
  159. user = 'user' + ':' + 'daily' + ':' + str(current_user.id)
  160. if redis_data.exists(user):
  161. flash('你已签到,不能重复签到')
  162. return redirect(url_for('user.index', user_url=current_user.name))
  163. else:
  164. from datetime import date, timedelta
  165. from time import mktime
  166. today = date.today() + timedelta(days=1)
  167. a = mktime(today.timetuple())
  168. b = a + 28800
  169. pipe = redis_data.pipeline()
  170. pipe.set(user, '1')
  171. pipe.expireat(user, int(b))
  172. pipe.execute()
  173. current_user.infor.score += 10
  174. db.session.commit()
  175. flash('签到成功')
  176. return redirect(url_for('user.index', user_url=current_user.name))
  177. @site.route('/notices')
  178. @login_required
  179. @own_permission
  180. def notice():
  181. '''未读提醒'''
  182. user = 'user:%s' % str(current_user.id)
  183. redis_data.hset(user, 'notice', 0)
  184. messages = Message.query.filter_by(rece_user=current_user.name).all()
  185. return render_template('user/user_notice.html', messages=messages)