2012-10-13 09:29:48 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
2012-11-17 01:09:20 +00:00
|
|
|
from flask import Flask, request, session, flash, render_template, redirect, url_for
|
2012-10-13 09:29:48 +00:00
|
|
|
import os.path
|
2012-10-13 10:29:37 +00:00
|
|
|
import uuid
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.secret_key = '?9huDM\\H'
|
|
|
|
|
2012-10-13 10:29:37 +00:00
|
|
|
import db
|
2012-10-13 12:53:09 +00:00
|
|
|
from scanner import Scanner
|
2012-10-13 09:29:48 +00:00
|
|
|
|
2012-11-16 15:59:09 +00:00
|
|
|
@app.before_request
|
2012-11-17 01:09:20 +00:00
|
|
|
def init_and_login_check():
|
2012-11-16 15:59:09 +00:00
|
|
|
if request.path.startswith('/rest/'):
|
|
|
|
return
|
|
|
|
|
2012-11-17 01:09:20 +00:00
|
|
|
admin_count = db.User.query.filter(db.User.admin == True).count()
|
|
|
|
if admin_count == 0 and request.endpoint != 'add_user':
|
2012-11-16 15:59:09 +00:00
|
|
|
flash('Not configured. Please create the first admin user')
|
|
|
|
return redirect(url_for('add_user'))
|
|
|
|
|
2012-11-17 01:09:20 +00:00
|
|
|
if not (admin_count == 0 and request.endpoint == 'add_user') and not session.get('userid') and request.endpoint != 'login':
|
|
|
|
flash('Please login')
|
|
|
|
return redirect(url_for('login', returnUrl = request.url[len(request.url_root)-1:]))
|
|
|
|
|
2012-10-13 09:29:48 +00:00
|
|
|
@app.teardown_request
|
|
|
|
def teardown(exception):
|
2012-10-13 10:29:37 +00:00
|
|
|
db.session.remove()
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
2012-11-16 15:59:09 +00:00
|
|
|
return render_template('home.html', folders = db.Folder.query.filter(db.Folder.root == True).all(),
|
2012-10-13 12:53:09 +00:00
|
|
|
artists = db.Artist.query.order_by(db.Artist.name).all(),
|
2012-10-21 14:25:16 +00:00
|
|
|
albums = db.Album.query.join(db.Album.artist).order_by(db.Artist.name, db.Album.name).all())
|
2012-10-13 10:29:37 +00:00
|
|
|
|
|
|
|
@app.route('/resetdb')
|
|
|
|
def reset_db():
|
|
|
|
db.recreate_db()
|
|
|
|
return redirect(url_for('index'))
|
2012-10-13 09:29:48 +00:00
|
|
|
|
|
|
|
@app.route('/addfolder', methods = [ 'GET', 'POST' ])
|
|
|
|
def add_folder():
|
|
|
|
if request.method == 'GET':
|
|
|
|
return render_template('addfolder.html')
|
|
|
|
|
|
|
|
error = False
|
|
|
|
(name, path) = map(request.form.get, [ 'name', 'path' ])
|
|
|
|
if name in (None, ''):
|
|
|
|
flash('The name is required.')
|
|
|
|
error = True
|
2012-10-21 14:18:35 +00:00
|
|
|
elif db.Folder.query.filter(db.Folder.name == name and db.Folder.root).first():
|
2012-10-13 09:29:48 +00:00
|
|
|
flash('There is already a folder with that name. Please pick another one.')
|
|
|
|
error = True
|
|
|
|
if path in (None, ''):
|
|
|
|
flash('The path is required.')
|
|
|
|
error = True
|
|
|
|
else:
|
|
|
|
path = os.path.abspath(path)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
flash("The path '%s' doesn't exists or isn't a directory" % path)
|
|
|
|
error = True
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = db.Folder.query.filter(db.Folder.path == path).first()
|
2012-10-13 09:29:48 +00:00
|
|
|
if folder:
|
2012-10-21 14:18:35 +00:00
|
|
|
flash("This path is already registered")
|
2012-10-13 09:29:48 +00:00
|
|
|
error = True
|
|
|
|
if error:
|
|
|
|
return render_template('addfolder.html')
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = db.Folder(root = True, name = name, path = path)
|
2012-10-13 10:29:37 +00:00
|
|
|
db.session.add(folder)
|
|
|
|
db.session.commit()
|
2012-10-13 09:29:48 +00:00
|
|
|
flash("Folder '%s' created. You should now run a scan" % name)
|
|
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2012-10-13 10:29:37 +00:00
|
|
|
@app.route('/delfolder/<id>')
|
|
|
|
def del_folder(id):
|
2012-10-13 10:31:49 +00:00
|
|
|
try:
|
|
|
|
idid = uuid.UUID(id)
|
|
|
|
except ValueError:
|
|
|
|
flash('Invalid folder id')
|
2012-10-13 12:53:09 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = db.Folder.query.get(idid)
|
2012-10-13 12:53:09 +00:00
|
|
|
if folder is None:
|
2012-10-13 10:31:49 +00:00
|
|
|
flash('No such folder')
|
2012-10-13 12:53:09 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2012-10-14 11:07:02 +00:00
|
|
|
# delete associated tracks and prune empty albums/artists
|
|
|
|
for artist in db.Artist.query.all():
|
|
|
|
for album in artist.albums[:]:
|
2012-10-21 14:18:35 +00:00
|
|
|
for track in filter(lambda t: t.root_folder.id == folder.id, album.tracks):
|
2012-10-14 11:07:02 +00:00
|
|
|
album.tracks.remove(track)
|
|
|
|
db.session.delete(track)
|
|
|
|
if len(album.tracks) == 0:
|
|
|
|
artist.albums.remove(album)
|
|
|
|
db.session.delete(album)
|
|
|
|
if len(artist.albums) == 0:
|
|
|
|
db.session.delete(artist)
|
2012-10-21 14:18:35 +00:00
|
|
|
|
|
|
|
def cleanup_folder(folder):
|
|
|
|
for f in folder.children:
|
|
|
|
cleanup_folder(f)
|
|
|
|
db.session.delete(folder)
|
|
|
|
|
|
|
|
cleanup_folder(folder)
|
2012-10-14 11:07:02 +00:00
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
db.session.commit()
|
|
|
|
flash("Deleted folder '%s'" % folder.name)
|
|
|
|
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
@app.route('/scan')
|
|
|
|
@app.route('/scan/<id>')
|
|
|
|
def scan_folder(id = None):
|
|
|
|
s = Scanner(db.session)
|
|
|
|
if id is None:
|
2012-10-21 14:18:35 +00:00
|
|
|
for folder in db.Folder.query.filter(db.Folder.root == True).all():
|
2012-10-13 12:53:09 +00:00
|
|
|
s.scan(folder)
|
|
|
|
s.prune(folder)
|
2012-11-11 20:39:26 +00:00
|
|
|
s.check_cover_art(folder)
|
2012-10-13 12:53:09 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
idid = uuid.UUID(id)
|
|
|
|
except ValueError:
|
|
|
|
flash('Invalid folder id')
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2012-10-21 14:18:35 +00:00
|
|
|
folder = db.Folder.query.get(idid)
|
|
|
|
if folder is None or not folder.root:
|
2012-10-13 12:53:09 +00:00
|
|
|
flash('No such folder')
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
s.scan(folder)
|
|
|
|
s.prune(folder)
|
2012-11-11 20:39:26 +00:00
|
|
|
s.check_cover_art(folder)
|
2012-10-13 12:53:09 +00:00
|
|
|
|
|
|
|
added, deleted = s.stats()
|
|
|
|
db.session.commit()
|
2012-10-13 10:31:49 +00:00
|
|
|
|
2012-10-13 12:53:09 +00:00
|
|
|
flash('Added: %i artists, %i albums, %i tracks' % (added[0], added[1], added[2]))
|
|
|
|
flash('Deleted: %i artists, %i albums, %i tracks' % (deleted[0], deleted[1], deleted[2]))
|
2012-10-13 10:29:37 +00:00
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
2012-11-16 15:59:09 +00:00
|
|
|
import user
|
|
|
|
|
2012-10-13 09:29:48 +00:00
|
|
|
import api.system
|
|
|
|
import api.browse
|
2012-10-14 17:04:39 +00:00
|
|
|
import api.user
|
2012-10-20 16:05:37 +00:00
|
|
|
import api.albums_songs
|
2012-10-20 18:05:39 +00:00
|
|
|
import api.media
|
2012-10-13 09:29:48 +00:00
|
|
|
|