flaskr.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. """
  2. Simple flask thing
  3. """
  4. import random
  5. import string
  6. from datetime import datetime
  7. from flask import render_template, request, redirect, \
  8. flash, url_for, abort, json
  9. from flask_breadcrumbs import Breadcrumbs, register_breadcrumb
  10. from flask_menu import Menu, register_menu
  11. from flask_login import login_required, login_user, logout_user
  12. from rival_regions_wrapper.wrapper import Profile, Conference, LanguageChat
  13. from app import app, db, rrclient, alt_rrclient
  14. from app.models import User, Log, Key
  15. Menu(app=app)
  16. Breadcrumbs(app=app)
  17. @register_breadcrumb(app, '.login', 'Login')
  18. @app.route("/login", methods=["GET", "POST"])
  19. def login():
  20. """Handle login page and data"""
  21. if request.method == 'POST':
  22. email = request.form['email']
  23. password = request.form['password']
  24. user = User.query.filter(User.email == email).first()
  25. if user:
  26. if user.check_password(password):
  27. login_user(user, remember=True)
  28. flash('Successfully loggend in.', 'success')
  29. if request.args.get("next"):
  30. return redirect(request.args.get('next'))
  31. return redirect(url_for('index'))
  32. flash('Password Incorrect.', 'warning')
  33. return render_template('site/login.html', login_email=email)
  34. flash('Email not found.', 'warning')
  35. return render_template('site/login.html')
  36. @app.route("/register", methods=["POST"])
  37. def register():
  38. """Register a new user"""
  39. name = request.form['name'] if 'name' in request.form else None
  40. email = request.form['email'] if 'email' in request.form else None
  41. password = request.form['password'] if 'email' in request.form else None
  42. if name is None:
  43. flash('Fill in the name.', 'warning')
  44. return render_template('login.j2')
  45. if email is None:
  46. flash('Fill in the email.', 'warning')
  47. return render_template(
  48. 'login.j2',
  49. name=name
  50. )
  51. if password is None:
  52. flash('Fill in the password.', 'warning')
  53. return render_template(
  54. 'login.j2',
  55. name=name,
  56. email=email
  57. )
  58. user = User.query.filter(User.email == email).first()
  59. if user is not None:
  60. flash('Email already taken.', 'warning')
  61. return render_template(
  62. 'login.j2',
  63. name=name,
  64. )
  65. user = User()
  66. user.name = name
  67. user.email = email
  68. user.password = password
  69. db.session.add(user)
  70. db.session.commit()
  71. login_user(user)
  72. flash('Successfully registered account "%s".' % (user.name), 'success')
  73. if request.args.get("next") is not None:
  74. return redirect(request.args.get("next"))
  75. return redirect(url_for('index'))
  76. @app.route("/logout")
  77. @login_required
  78. def logout():
  79. """Logout function for users"""
  80. logout_user()
  81. flash('succesfully logged out', 'success')
  82. return redirect(url_for('login'))
  83. @app.route('/')
  84. @register_menu(app, '.', 'Home')
  85. @register_breadcrumb(app, '.', 'Home')
  86. def index():
  87. """Show homepage"""
  88. # users = User.query.count()
  89. return render_template('site/index.html')
  90. # return render_template('site/index.html', users=users)
  91. @app.route('/users')
  92. @register_menu(app, 'users', 'Users')
  93. @register_breadcrumb(app, '.users', 'Users')
  94. @login_required
  95. def user_index():
  96. """Show users"""
  97. users = User.query.all()
  98. return render_template('user/index.html', users=users)
  99. def user_overview_dlc(*args, **kwargs):
  100. """Generate dynamic_list for user"""
  101. id = request.view_args['id']
  102. user = User.query.get(id)
  103. return [{'text': user.email, 'url': user.name}]
  104. @app.route('/user/<int:id>')
  105. @register_breadcrumb(app, '.users.id', '',
  106. dynamic_list_constructor=user_overview_dlc)
  107. @login_required
  108. def user_overview(id):
  109. """Show user overview"""
  110. id = int(id)
  111. user = User.query.get(id)
  112. return render_template('user/overview.html', user=user)
  113. @app.route('/user/<int:id>/generate_key')
  114. @login_required
  115. def user_generate_key(id):
  116. """Generate new key for user"""
  117. user = User.query.get(id)
  118. key = Key()
  119. key.key = ''.join(random.sample(
  120. string.ascii_letters + string.digits, 32
  121. ))
  122. key.user_id = user.id
  123. db.session.add(key)
  124. db.session.commit()
  125. flash('Succesfully generated key', 'success')
  126. return redirect(url_for('user_overview', id=user.id))
  127. @app.route('/user/<int:user_id>/key/<int:key_id>/activate')
  128. @login_required
  129. def user_toogle_key(user_id, key_id):
  130. """Activate key"""
  131. user = User.query.get(user_id)
  132. key = Key.query.get(key_id)
  133. key.active = not key.active
  134. db.session.add(key)
  135. db.session.commit()
  136. if key.active:
  137. flash('Activated key', 'success')
  138. else:
  139. flash('Deactivated key', 'success')
  140. return redirect(url_for('user_overview', id=user.id))
  141. @app.route('/api/authenticated', methods=["POST"])
  142. def api_authenticated():
  143. """Check key"""
  144. if 'Authorization' not in request.headers:
  145. return abort(403)
  146. authorization = request.headers['authorization']
  147. key = Key.query.filter(Key.key == authorization).count()
  148. if key:
  149. return json.dumps(True)
  150. return json.dumps(False)
  151. @app.route('/api/request/<path:url_path>', methods=["GET"])
  152. def api_get(url_path):
  153. """Check key"""
  154. if 'Authorization' not in request.headers:
  155. return abort(403)
  156. authorization = request.headers['authorization']
  157. key = Key.query.filter(Key.key == authorization).first()
  158. if not key or not key.active:
  159. return abort(403)
  160. log = Log()
  161. log.date_time = datetime.now()
  162. log.key_id = key.id
  163. log.request_type = 'GET'
  164. log.request_url = url_path
  165. db.session.add(log)
  166. db.session.commit()
  167. alt = request.args.get('alt')
  168. if alt and alt.lower() == 'true':
  169. result = alt_rrclient.get(url_path)
  170. else:
  171. result = rrclient.get(url_path)
  172. log.succes = True
  173. db.session.commit()
  174. return result
  175. @app.route('/api/request/send_chat/<string:language>', methods=["POST"])
  176. def api_send_chat(language):
  177. """Check key"""
  178. if 'Authorization' not in request.headers:
  179. return abort(403)
  180. authorization = request.headers['authorization']
  181. key = Key.query.filter(Key.key == authorization).first()
  182. if not key or not key.active:
  183. return abort(403)
  184. if 'message' not in request.form:
  185. return abort(400)
  186. message = request.form['message']
  187. log = Log()
  188. log.date_time = datetime.now()
  189. log.key_id = key.id
  190. log.request_type = 'CHAT'
  191. log.request_url = language
  192. db.session.add(log)
  193. db.session.commit()
  194. alt = request.args.get('alt')
  195. if alt and alt.lower() == 'true':
  196. LanguageChat(alt_rrclient, language).message(message)
  197. else:
  198. LanguageChat(rrclient, language).message(message)
  199. log.succes = True
  200. db.session.commit()
  201. return json.dumps(True)
  202. @app.route('/api/request/send_personal_message/<int:user_id>', methods=["POST"])
  203. def api_send_personal_message(user_id):
  204. """Send personal message to player"""
  205. if 'Authorization' not in request.headers:
  206. return abort(403)
  207. authorization = request.headers['authorization']
  208. key = Key.query.filter(Key.key == authorization).first()
  209. if not key or not key.active:
  210. return abort(403)
  211. if 'message' not in request.form:
  212. return abort(400)
  213. message = request.form['message']
  214. log = Log()
  215. log.date_time = datetime.now()
  216. log.key_id = key.id
  217. log.request_type = 'PM'
  218. log.request_url = user_id
  219. db.session.add(log)
  220. db.session.commit()
  221. alt = request.args.get('alt')
  222. if alt and alt.lower() == 'true':
  223. Profile(alt_rrclient, profile_id).message(message)
  224. else:
  225. Profile(rrclient, profile_id).message(message)
  226. log.succes = True
  227. db.session.commit()
  228. return json.dumps(True)
  229. @app.route('/api/request/send_conference_message/<int:conference_id>', methods=["POST"])
  230. def api_send_conference_message(conference_id):
  231. """Check key"""
  232. if 'Authorization' not in request.headers:
  233. return abort(403)
  234. authorization = request.headers['authorization']
  235. key = Key.query.filter(Key.key == authorization).first()
  236. if not key or not key.active:
  237. return abort(403)
  238. if 'message' not in request.form:
  239. return abort(400)
  240. message = request.form['message']
  241. log = Log()
  242. log.date_time = datetime.now()
  243. log.key_id = key.id
  244. log.request_type = 'CM'
  245. log.request_url = conference_id
  246. db.session.add(log)
  247. db.session.commit()
  248. alt = request.args.get('alt')
  249. if alt and alt.lower() == 'true':
  250. Conference(alt_rrclient, conference_id).message(message)
  251. else:
  252. Conference(rrclient, conference_id).message(message)
  253. log.succes = True
  254. db.session.commit()
  255. return json.dumps(True)
  256. @app.route('/api/request/send_conference_notification/<int:conference_id>', methods=["POST"])
  257. def api_send_conference_notification(conference_id):
  258. """Check key"""
  259. if 'Authorization' not in request.headers:
  260. return abort(403)
  261. authorization = request.headers['authorization']
  262. key = Key.query.filter(Key.key == authorization).first()
  263. if not key or not key.active:
  264. return abort(403)
  265. if 'message' not in request.form:
  266. return abort(400)
  267. message = request.form['message']
  268. if 'sound' in request.form:
  269. sound = request.form['sound'].lower() == 'true'
  270. else:
  271. sound = False
  272. log = Log()
  273. log.date_time = datetime.now()
  274. log.key_id = key.id
  275. log.request_type = 'CM'
  276. log.request_url = conference_id
  277. db.session.add(log)
  278. db.session.commit()
  279. alt = request.args.get('alt')
  280. if alt and alt.lower() == 'true':
  281. Conference(alt_rrclient, conference_id).notification(message, sound)
  282. else:
  283. Conference(rrclient, conference_id).notification(message, sound)
  284. log.succes = True
  285. db.session.commit()
  286. return json.dumps(True)
  287. @app.route('/api/request/<path:url_path>', methods=["POST"])
  288. def api_post(url_path):
  289. """Check key"""
  290. if 'Authorization' not in request.headers:
  291. return abort(403)
  292. authorization = request.headers['authorization']
  293. key = Key.query.filter(Key.key == authorization).first()
  294. if not key or not key.active:
  295. return abort(403)
  296. log = Log()
  297. log.date_time = datetime.now()
  298. log.key_id = key.id
  299. log.request_type = 'POST'
  300. log.request_url = url_path
  301. db.session.add(log)
  302. db.session.commit()
  303. data = request.get_json()
  304. if not data:
  305. data = {}
  306. alt = request.args.get('alt')
  307. if alt and alt.lower() == 'true':
  308. result = alt_rrclient.post(url_path, data=data)
  309. else:
  310. result = rrclient.post(url_path, data=data)
  311. log.succes = True
  312. db.session.commit()
  313. return result