def show_index(): msg = None error = None if request.method == 'POST': if request.form['action'] == 'login': user = SoundBubbleUser.get(request.form['username']) if (user and user.password == request.form['password']): login_user(user, remember=True) else: error = 'Invalid username or password.' elif request.form['action'] == 'logout': logout_user() elif request.form['action'] == 'add_music' and current_user.is_authenticated: audio_file = request.files.get('song', None) if audio_file and audio.is_allowed_audio_file(audio_file.filename): filename = secure_filename(audio_file.filename) filepath = os.path.join(app.config['MUSIC_DIR'], filename) audio_file.save(filepath) data = audio.add_new_song(filename) msg = 'Added {} to the playlist.'.format(data['title']) elif request.form['action'] == 'add_artwork' and current_user.is_authenticated: artwork_file = request.files.get('artwork', None) if artwork_file and audio.is_allowed_artwork_file(artwork_file.filename): filename = secure_filename(artwork_file.filename) filepath = os.path.join(app.config['TMP_DIR'], filename) artwork_file.save(filepath) song_title = audio.current_song['title'] audio.change_album_artwork(audio.current_song, filepath) msg = 'Updated artwork for {}.'.format(song_title) return render_template('index.html', error=error, message=msg)
def load_user(id): return SoundBubbleUser.get(id)
import time from sb_user import SoundBubbleUser from audio_manager import AudioManager from flask import Flask, request, g, redirect, url_for, \ abort, render_template, flash from flask.ext.login import LoginManager, current_user, login_user, logout_user from flask.ext.socketio import SocketIO, emit from werkzeug import secure_filename import os.path app = Flask(__name__) app.config.from_object('config') socket = SocketIO(app) SoundBubbleUser.register_users({ app.config['USERNAME']: app.config['PASSWORD'] }) login_manager = LoginManager() login_manager.init_app(app) audio = AudioManager(app.config) @login_manager.user_loader def load_user(id): return SoundBubbleUser.get(id)