DotaNoobs main site.
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.

177 lines
6.1 KiB

  1. from flask import render_template, flash, redirect, g, request, url_for, session
  2. from datetime import datetime
  3. from functools import wraps
  4. from app import app, db, oid, cache
  5. from models import User, Event
  6. from utils import get_steam_userinfo
  7. from board import registerUserForumId
  8. from teamspeak import registerUserTeamspeakId
  9. from forms import SettingsForm, EventForm, EnableStatsForm
  10. @app.before_request
  11. def before_request():
  12. g.user = None
  13. if 'user_id' in session:
  14. g.user = User.query.get(session['user_id'])
  15. if g.user:
  16. g.user.last_seen = datetime.utcnow()
  17. db.session.commit()
  18. def login_required(f):
  19. @wraps(f)
  20. def decorated_function(*args, **kwargs):
  21. if g.user is None:
  22. return redirect(url_for('login', next=request.url))
  23. return f(*args, **kwargs)
  24. return decorated_function
  25. def flash_form_errors(form):
  26. for field, errors in form.errors.items():
  27. for error in errors:
  28. flash(u"Error in the %s field - %s" % (getattr(form,field).label.text, error), 'danger')
  29. #
  30. # Application routes
  31. #
  32. @app.route('/')
  33. def index():
  34. active = Event.query.filter(Event.start_time <= datetime.utcnow(), Event.end_time > datetime.utcnow()).all()
  35. upcoming = Event.query.filter(Event.start_time > datetime.utcnow()).limit(2).all()
  36. return render_template("index.html", active_events=active, upcoming_events=upcoming)
  37. @app.route('/login')
  38. @oid.loginhandler
  39. def login():
  40. if g.user is not None:
  41. return redirect(oid.get_next_url())
  42. return oid.try_login('http://steamcommunity.com/openid')
  43. @oid.after_login
  44. def create_or_login(resp):
  45. match = app.config['STEAM_ID_RE'].search(resp.identity_url)
  46. g.user = User.get_or_create(match.group(1))
  47. steamdata = get_steam_userinfo(g.user.steam_id)
  48. g.user.nickname = steamdata['personaname']
  49. g.user.avatar = steamdata['avatar']
  50. db.session.commit()
  51. session['user_id'] = g.user.id
  52. flash("You are logged in as {}".format(g.user.nickname), 'success')
  53. return redirect(oid.get_next_url())
  54. @app.route('/logout')
  55. def logout():
  56. session.pop('user_id', None)
  57. g.user = None
  58. flash("You have been logged out.", 'success')
  59. return redirect(url_for('index'))
  60. # Teamspeak statistics page
  61. @app.route('/teamspeak')
  62. def teamspeak():
  63. return render_template('teamspeak.html')
  64. # Friends of doobs page
  65. @app.route('/friends')
  66. def friends():
  67. return render_template('friends.html')
  68. # User profile page
  69. @app.route('/user/<int:userid>')
  70. def user_profile(userid):
  71. user = User.query.filter_by(id=userid).first_or_404()
  72. return render_template('profile.html', user=user)
  73. # User random a-z challenge progress page
  74. @app.route('/user/<int:userid>/random', methods=['POST', 'GET'])
  75. def user_random_hero(userid):
  76. user = User.query.filter_by(id=userid).first_or_404()
  77. if request.method == 'POST':
  78. if request.form.get('skip', False):
  79. user.random_skip()
  80. elif request.form.get('completed', False):
  81. user.random_success()
  82. return render_template('hero_random.html', user=user)
  83. # User settings page
  84. @app.route('/settings', methods=['POST', 'GET'])
  85. @login_required
  86. def user_settings():
  87. user = User.query.filter_by(id=g.user.id).first_or_404()
  88. form = SettingsForm(obj=user)
  89. if form.validate_on_submit():
  90. form.populate_obj(user)
  91. if user.bio_text == '':
  92. user.bio_text = None
  93. db.session.commit()
  94. flash('Settings updated!', 'success')
  95. return render_template('settings.html', form=form)
  96. # Enable user statistics page
  97. @app.route('/settings/enable_stats', methods=['POST', 'GET'])
  98. @login_required
  99. def enable_statistics():
  100. # TODO: update user_settings to use g.user, avoid extra queries
  101. form = EnableStatsForm(obj=g.user)
  102. if form.validate_on_submit():
  103. forum_data = registerUserForumId(g.user, form.forum_username.data, form.forum_password.data)
  104. if forum_data:
  105. flash('Forum account \''+forum_data['forum_name']+'\' linked!', 'success')
  106. else:
  107. flash('Forum account credentials invalid', 'danger')
  108. if registerUserTeamspeakId(g.user, form.teamspeak_id.data):
  109. flash('Teamspeak account linked successfully!', 'success')
  110. else:
  111. flash('Teamspeak ID not found in client history', 'danger')
  112. return render_template('enable_stats.html', form=form)
  113. # Events list
  114. @app.route('/events', methods=['GET'])
  115. def list_events():
  116. now = datetime.utcnow()
  117. active = Event.query.filter(Event.start_time <= now, Event.end_time > now).all()
  118. upcoming = Event.query.filter(Event.start_time > now).all()
  119. expired = Event.query.filter(Event.end_time < now).all()
  120. return render_template('list_events.html', active=active, upcoming=upcoming, expired=expired)
  121. # Show event info
  122. @app.route('/event/<int:eventid>', methods=['GET'])
  123. def show_event(eventid):
  124. event = Event.query.filter_by(id=eventid).first_or_404()
  125. return render_template('show_event.html', event=event)
  126. # Event creation page
  127. @app.route('/event/edit', methods=['POST', 'GET'])
  128. @login_required
  129. def event_edit():
  130. if not g.user.admin:
  131. flash('Access Denied: You cannot create/edit events.', 'danger')
  132. return redirect(url_for('index'))
  133. eventid = request.args.get('eventid')
  134. event = Event.get_or_create(eventid)
  135. form = EventForm(obj=event)
  136. if form.validate_on_submit():
  137. form.populate_obj(event)
  138. db.session.add(event)
  139. db.session.commit()
  140. flash('New event created!', 'success') if eventid is None else flash('Event updated successfully!', 'success')
  141. return redirect(url_for('show_event', eventid=event.id))
  142. else:
  143. flash_form_errors(form)
  144. return render_template('edit_event.html', event=event, form=form)
  145. # Event deletion call
  146. @app.route('/event/<int:eventid>/delete', methods=['GET'])
  147. @login_required
  148. def event_delete(eventid):
  149. if g.user.admin:
  150. event = Event.query.filter_by(id=eventid).first_or_404()
  151. flash('Info: Event "{}" deleted successfully.'.format(event.name), 'success')
  152. db.session.delete(event)
  153. db.session.commit()
  154. else:
  155. flash('Access Denied: You cannot delete events.', 'danger')
  156. return redirect(url_for('index'))