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.

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