1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Users import/export to keep a backup in case of DB schema change

This commit is contained in:
Alban 2013-06-07 17:18:13 +02:00
parent e89b296857
commit 197722c3ed
3 changed files with 46 additions and 3 deletions

9
templates/importusers.html Executable file
View File

@ -0,0 +1,9 @@
{% extends "layout.html" %}
{% block body %}
<h2>Import users</h2>
<p><strong>Warning:</strong> this will overwrite any existing users, even you!</p>
<form enctype="multipart/form-data" method="post">
<input type="file" name="file" />
<input type="submit" />
</form>
{% endblock %}

View File

@ -7,6 +7,6 @@
<tr><td>{{ user.name }}</td><td>{{ user.mail }}</td><td>{{ user.admin }}</td><td><a href="{{ url_for('del_user', uid = user.id) }}">X</a></td></tr> <tr><td>{{ user.name }}</td><td>{{ user.mail }}</td><td>{{ user.admin }}</td><td><a href="{{ url_for('del_user', uid = user.id) }}">X</a></td></tr>
{% endfor %} {% endfor %}
</table> </table>
<a href="{{ url_for('add_user') }}">Add</a> <a href="{{ url_for('add_user') }}">Add</a> - <a href="{{ url_for('export_users') }}">Export</a> - <a href="{{ url_for('import_users') }}">Import</a>
{% endblock %} {% endblock %}

38
user.py
View File

@ -1,10 +1,11 @@
# coding: utf-8 # coding: utf-8
from flask import request, session, flash, render_template, redirect, url_for from flask import request, session, flash, render_template, redirect, url_for, make_response
from web import app from web import app
from user_manager import UserManager from user_manager import UserManager
from db import User, session as db_sess from db import User, session as db_sess
import uuid, csv
import config import config
from lastfm import LastFm from lastfm import LastFm
@ -16,7 +17,7 @@ def check_admin():
if request.endpoint == 'add_user' and User.query.filter(User.admin == True).count() == 0: if request.endpoint == 'add_user' and User.query.filter(User.admin == True).count() == 0:
return return
if request.endpoint in ('user_index', 'add_user', 'del_user') and not UserManager.get(session.get('userid'))[1].admin: if request.endpoint in ('user_index', 'add_user', 'del_user', 'export_users', 'import_users', 'do_user_import') and not UserManager.get(session.get('userid'))[1].admin:
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/user') @app.route('/user')
@ -107,6 +108,39 @@ def del_user(uid):
return redirect(url_for('user_index')) return redirect(url_for('user_index'))
@app.route('/user/export')
def export_users():
resp = make_response('\n'.join([ '%s,%s,%s,%s,"%s",%s,%s,%s' % (u.id, u.name, u.mail, u.password, u.salt, u.admin, u.lastfm_session, u.lastfm_status)
for u in User.query.all() ]))
resp.headers['Content-disposition'] = 'attachment;filename=users.csv'
resp.headers['Content-type'] = 'text/csv'
return resp
@app.route('/user/import')
def import_users():
return render_template('importusers.html')
@app.route('/user/import', methods = [ 'POST' ])
def do_user_import():
if not request.files['file']:
return render_template('importusers.html')
users = []
reader = csv.reader(request.files['file'])
for id, name, mail, password, salt, admin, lfmsess, lfmstatus in reader:
mail = None if mail == 'None' else mail
admin = bool(admin)
lfmsess = None if lfmsess == 'None' else lfmsess
lfmstatus = bool(lfmstatus)
users.append(User(id = uuid.UUID(id), name = name, password = password, salt = salt, admin = admin, lastfm_session = lfmsess, lastfm_status = lfmstatus))
User.query.delete()
for u in users:
db_sess.add(u)
db_sess.commit()
return redirect(url_for('user_index'))
@app.route('/user/lastfm/link') @app.route('/user/lastfm/link')
def lastfm_reg(): def lastfm_reg():
token = request.args.get('token') token = request.args.get('token')