华清大学特普通奖学金初选报名系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. from flask import Flask, request, render_template, send_from_directory, abort, redirect, session
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_limiter import Limiter
  4. from flask_limiter.util import get_remote_address
  5. import re
  6. import random
  7. from datetime import datetime
  8. from dateutil.tz import tzlocal
  9. import html2text
  10. from config_fudan import C
  11. app = Flask(__name__)
  12. app.config.from_object('config_fudan.C')
  13. app.secret_key = C.session_key
  14. limiter = Limiter(
  15. app,
  16. key_func=get_remote_address,
  17. default_limits=["50 / minute"],
  18. )
  19. db = SQLAlchemy(app)
  20. class Candidate(db.Model):
  21. id = db.Column(db.Integer, primary_key=True)
  22. content = db.Column(db.String(4000))
  23. private = db.Column(db.String(1000))
  24. time = db.Column(db.DateTime)
  25. likeNum = db.Column(db.Integer, default=0)
  26. # always increment 1 for the id of a new record
  27. __table_args__ = { 'sqlite_autoincrement': True }
  28. class Like(db.Model):
  29. id = db.Column(db.Integer, primary_key=True)
  30. cid = db.Column(db.Integer) # (member) id of class Candidate
  31. uid = db.Column(db.Integer) # id of user
  32. db.create_all()
  33. @app.route('/img/<path:path>')
  34. def send_img(path):
  35. return send_from_directory('static/img', path)
  36. @app.route('/ordinary/set_session')
  37. @limiter.limit("2 / hour; 1 / 5 minute")
  38. def set_session():
  39. if 'uid' not in session:
  40. session['uid'] = random.randint(0, 2000000000)
  41. session.permanent = True
  42. return redirect('.')
  43. @app.route('/ordinary/')
  44. def can_list():
  45. key = request.args.get('key')
  46. sort_by = request.args.get('sort_by', 'time')
  47. if 'uid' not in session:
  48. return redirect('set_session')
  49. uid = session['uid']
  50. q = Candidate.query
  51. q = q.order_by(db.desc('likeNum')) if sort_by=='likeNum' else q.order_by(db.desc('id'))
  52. pag = q.paginate(max_per_page=100)
  53. def check_like(c):
  54. c.liked = 'liked' if Like.query.filter_by(uid=uid, cid=c.id).count() else 'like'
  55. return c
  56. pag.items = map(check_like, pag.items)
  57. vs = [{
  58. 'name': name,
  59. 'ques': ques,
  60. 'hint': hint
  61. } for name, ques, hint, ans in C.verify
  62. ]
  63. return render_template('list.html', pagination=pag, vs=vs, showPrivate=(key==C.key), sort_by=sort_by, key=key, text1=C.text1, text2=C.text2)
  64. @app.route('/ordinary/new', methods=['POST'])
  65. @limiter.limit("5 / hour; 1 / 2 second")
  66. def new_one():
  67. content = request.form.get('text')
  68. private = request.form.get('privateText')
  69. for name, ques, hint, ans in C.verify:
  70. if request.form.get(name) != ans:
  71. return '''<html>
  72. <head>
  73. <meta charset='UTF-8'>
  74. <meta name='viewport' content='width=device-width initial-scale=1'>
  75. <title></title>
  76. </head>
  77. <body>
  78. <h1></h1>
  79. <a href="##" onclick="window.history.back()">退</a>
  80. </body>
  81. </html>
  82. ''', 401
  83. if not content or len(content)>4000: abort(422)
  84. if private and len(private)>1000: abort(422)
  85. if not Candidate.query.filter_by(content=content).first():
  86. c = Candidate(
  87. content=content,
  88. private=private,
  89. time=datetime.now()
  90. )
  91. db.session.add(c)
  92. db.session.commit()
  93. return redirect(".")
  94. @limiter.limit("100 / hour")
  95. @app.route('/ordinary/<int:id>/like', methods=['POST'])
  96. def like(id):
  97. c = Candidate.query.get(id)
  98. if not c:
  99. abort(404)
  100. uid = session['uid']
  101. if not uid: abort(401)
  102. if Like.query.filter_by(uid=uid, cid=id).first():
  103. return '点赞过了', 403
  104. l = Like(uid=uid, cid=id)
  105. c.likeNum += 1
  106. db.session.add(l)
  107. db.session.commit()
  108. return str(c.likeNum)
  109. @app.route('/ordinary/<int:id>/delete', methods=['POST'])
  110. def delete(id):
  111. key = request.form.get('key')
  112. if key != C.key:
  113. abort(401)
  114. c = Candidate.query.get(id)
  115. if not c:
  116. abort(404)
  117. db.session.delete(c)
  118. db.session.commit()
  119. return redirect('..')
  120. if __name__ == '__main__':
  121. app.run(debug=True)