1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-09 19:52:16 +00:00

Some stuff

- We don't drop and create the database when we reload the server
- User delete method
This commit is contained in:
Alban 2012-10-13 12:29:37 +02:00
parent 6375266fa8
commit 03a6dfd106
3 changed files with 47 additions and 19 deletions

7
db.py
View File

@ -36,10 +36,10 @@ class UUID(types.TypeDecorator):
return Column(UUID, primary_key = True, default = uuid.uuid4)
engine = create_engine(config.get('DATABASE_URI'), convert_unicode = True)
db_session = scoped_session(sessionmaker(autocommit = False, autoflush = False, bind = engine))
session = scoped_session(sessionmaker(autocommit = False, autoflush = False, bind = engine))
Base = declarative_base()
Base.query = db_session.query_property()
Base.query = session.query_property()
class User(Base):
__tablename__ = 'users'
@ -59,6 +59,9 @@ class MusicFolder(Base):
path = Column(String)
def init_db():
Base.metadata.create_all(bind = engine)
def recreate_db():
Base.metadata.drop_all(bind = engine)
Base.metadata.create_all(bind = engine)

View File

@ -2,18 +2,18 @@
{% block body %}
<h2>Users</h2>
<table>
<tr><th>Name</th><th>EMail</th></tr>
<tr><th>Name</th><th>EMail</th><th></th></tr>
{% for user in users %}
<tr><td>{{ user.name }}</td><td>{{ user.mail }}</td></tr>
<tr><td>{{ user.name }}</td><td>{{ user.mail }}</td><td><a href="{{ url_for('del_user', id = user.id) }}">X</a></td></tr>
{% endfor %}
</table>
<a href="{{ url_for('add_user') }}">Add</a>
<h2>Music folders</h2>
<table>
<tr><th>Name</th><th>Path</th></tr>
<tr><th>Name</th><th>Path</th><th></th></tr>
{% for folder in folders %}
<tr><td>{{ folder.name }}</td><td>{{ folder.path }}</td></tr>
<tr><td>{{ folder.name }}</td><td>{{ folder.path }}</td><td><a href="{{ url_for('del_folder', id = folder.id) }}">X</a></td></tr>
{% endfor %}
</table>
<a href="{{ url_for('add_folder') }}">Add</a>

51
web.py
View File

@ -1,18 +1,19 @@
# coding: utf-8
from flask import Flask, request, flash, render_template, redirect, url_for
from sqlalchemy.orm.exc import NoResultFound
import string, random, hashlib
import os.path
import uuid
app = Flask(__name__)
app.secret_key = '?9huDM\\H'
from db import db_session
from db import User, MusicFolder
import db
@app.teardown_request
def teardown(exception):
db_session.remove()
db.session.remove()
@app.route('/')
def index():
@ -21,7 +22,12 @@ def index():
flash('Not configured. Please create the first admin user')
return redirect(url_for('add_user'))
"""
return render_template('home.html', users = User.query.all(), folders = MusicFolder.query.all())
return render_template('home.html', users = db.User.query.all(), folders = db.MusicFolder.query.all())
@app.route('/resetdb')
def reset_db():
db.recreate_db()
return redirect(url_for('index'))
@app.route('/adduser', methods = [ 'GET', 'POST' ])
def add_user():
@ -33,7 +39,7 @@ def add_user():
if name in (None, ''):
flash('The name is required.')
error = True
elif User.query.filter(User.name == name).first():
elif db.User.query.filter(db.User.name == name).first():
flash('There is already a user with that name. Please pick another one.')
error = True
if passwd in (None, ''):
@ -47,13 +53,28 @@ def add_user():
salt = ''.join(random.choice(string.printable.strip()) for i in xrange(6))
crypt = hashlib.sha1(salt + passwd).hexdigest()
user = User(name = name, mail = mail, password = crypt, salt = salt)
db_session.add(user)
db_session.commit()
user = db.User(name = name, mail = mail, password = crypt, salt = salt)
db.session.add(user)
db.session.commit()
flash("User '%s' successfully added" % name)
return redirect(url_for('index'))
@app.route('/deluser/<id>')
def del_user(id):
try:
idid = uuid.UUID(id)
user = db.User.query.filter(db.User.id == uuid.UUID(id)).one()
db.session.delete(user)
db.session.commit()
flash("Deleted user '%s'" % user.name)
except ValueError:
flash('Invalid user id')
except NoResultFound:
flash('No such user')
return redirect(url_for('index'))
@app.route('/addfolder', methods = [ 'GET', 'POST' ])
def add_folder():
if request.method == 'GET':
@ -64,7 +85,7 @@ def add_folder():
if name in (None, ''):
flash('The name is required.')
error = True
elif MusicFolder.query.filter(MusicFolder.name == name).first():
elif db.MusicFolder.query.filter(db.MusicFolder.name == name).first():
flash('There is already a folder with that name. Please pick another one.')
error = True
if path in (None, ''):
@ -75,20 +96,24 @@ def add_folder():
if not os.path.isdir(path):
flash("The path '%s' doesn't exists or isn't a directory" % path)
error = True
folder = MusicFolder.query.filter(MusicFolder.name == name).first()
folder = db.MusicFolder.query.filter(db.MusicFolder.name == name).first()
if folder:
flash("This path is already registered with the name '%s'" % folder.name)
error = True
if error:
return render_template('addfolder.html')
folder = MusicFolder(name = name, path = path)
db_session.add(folder)
db_session.commit()
folder = db.MusicFolder(name = name, path = path)
db.session.add(folder)
db.session.commit()
flash("Folder '%s' created. You should now run a scan" % name)
return redirect(url_for('index'))
@app.route('/delfolder/<id>')
def del_folder(id):
return redirect(url_for('index'))
import api.system
import api.browse