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.

167 lines
5.8 KiB

from flask import render_template, flash, redirect, g, request, url_for, session
from datetime import datetime
from functools import wraps
from app import app, db, oid
from models import User, Event
from utils import get_steam_userinfo
from board import registerUserForumId
from teamspeak import registerUserTeamspeakId
from forms import SettingsForm, EventForm, EnableStatsForm
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
if g.user:
g.user.last_seen = datetime.utcnow()
def login_required(f):
def decorated_function(*args, **kwargs):
if g.user is None:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
def flash_form_errors(form):
for field, errors in form.errors.items():
for error in errors:
flash(u"Error in the %s field - %s" % (getattr(form,field).label.text, error), 'danger')
# Application routes
def index():
active = Event.query.filter(Event.start_time <= datetime.utcnow(), Event.end_time > datetime.utcnow()).all()
upcoming = Event.query.filter(Event.start_time > datetime.utcnow()).limit(2).all()
channels = User.get_streaming_users()
return render_template("index.html", active_events=active, upcoming_events=upcoming, streamers=channels)
def login():
if g.user is not None:
return redirect(oid.get_next_url())
return oid.try_login('')
def create_or_login(resp):
match = app.config['STEAM_ID_RE'].search(resp.identity_url)
g.user = User.get_or_create(
steamdata = get_steam_userinfo(g.user.steam_id)
g.user.nickname = steamdata['personaname'].encode('utf-8', 'replace')
g.user.avatar = steamdata['avatar']
session['user_id'] =
flash("You are logged in as {}".format(g.user.nickname), 'success')
return redirect(oid.get_next_url())
def logout():
session.pop('user_id', None)
g.user = None
flash("You have been logged out.", 'success')
return redirect(url_for('index'))
# Teamspeak statistics page
def teamspeak():
return render_template('teamspeak.html')
# Friends of doobs page
def friends():
return render_template('friends.html')
# User profile page
def user_profile(userid):
user = User.query.filter_by(id=userid).first_or_404()
return render_template('profile.html', user=user)
# User settings page
@app.route('/settings', methods=['POST', 'GET'])
def user_settings():
user = User.query.filter_by(
form = SettingsForm(obj=user)
if form.validate_on_submit():
if user.bio_text == '':
user.bio_text = None
flash('Settings updated!', 'success')
return render_template('settings.html', form=form)
# Enable user statistics page
@app.route('/settings/enable_stats', methods=['POST', 'GET'])
def enable_statistics():
# TODO: update user_settings to use g.user, avoid extra queries
form = EnableStatsForm(obj=g.user)
if form.validate_on_submit():
forum_data = registerUserForumId(g.user,,
if forum_data:
flash('Forum account \''+forum_data['forum_name']+'\' linked!', 'success')
flash('Forum account credentials invalid', 'danger')
if registerUserTeamspeakId(g.user,
flash('Teamspeak account linked successfully!', 'success')
flash('Teamspeak ID not found in client history', 'danger')
return render_template('enable_stats.html', form=form)
# Events list
@app.route('/events', methods=['GET'])
def list_events():
now = datetime.utcnow()
active = Event.query.filter(Event.start_time <= now, Event.end_time > now).all()
upcoming = Event.query.filter(Event.start_time > now).all()
expired = Event.query.filter(Event.end_time < now).all()
return render_template('list_events.html', active=active, upcoming=upcoming, expired=expired)
# Show event info
@app.route('/event/<int:eventid>', methods=['GET'])
def show_event(eventid):
event = Event.query.filter_by(id=eventid).first_or_404()
return render_template('show_event.html', event=event)
# Event creation page
@app.route('/event/edit', methods=['POST', 'GET'])
def event_edit():
if not g.user.admin:
flash('Access Denied: You cannot create/edit events.', 'danger')
return redirect(url_for('index'))
eventid = request.args.get('eventid')
event = Event.get_or_create(eventid)
form = EventForm(obj=event)
if form.validate_on_submit():
flash('New event created!', 'success') if eventid is None else flash('Event updated successfully!', 'success')
return redirect(url_for('show_event',
return render_template('edit_event.html', event=event, form=form)
# Event deletion call
@app.route('/event/<int:eventid>/delete', methods=['GET'])
def event_delete(eventid):
if g.user.admin:
event = Event.query.filter_by(id=eventid).first_or_404()
flash('Info: Event "{}" deleted successfully.'.format(, 'success')
flash('Access Denied: You cannot delete events.', 'danger')
return redirect(url_for('index'))