From 197722c3ed53600dab2c98f2e25ef7ce120fc21e Mon Sep 17 00:00:00 2001 From: Alban Date: Fri, 7 Jun 2013 17:18:13 +0200 Subject: [PATCH] Users import/export to keep a backup in case of DB schema change --- templates/importusers.html | 9 +++++++++ templates/users.html | 2 +- user.py | 38 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100755 templates/importusers.html diff --git a/templates/importusers.html b/templates/importusers.html new file mode 100755 index 0000000..e55bc5d --- /dev/null +++ b/templates/importusers.html @@ -0,0 +1,9 @@ +{% extends "layout.html" %} +{% block body %} +

Import users

+

Warning: this will overwrite any existing users, even you!

+
+ + +
+{% endblock %} diff --git a/templates/users.html b/templates/users.html index 7fb0892..8b32434 100755 --- a/templates/users.html +++ b/templates/users.html @@ -7,6 +7,6 @@ {{ user.name }}{{ user.mail }}{{ user.admin }}X {% endfor %} -Add +Add - Export - Import {% endblock %} diff --git a/user.py b/user.py index 26ed09b..39a49ff 100755 --- a/user.py +++ b/user.py @@ -1,10 +1,11 @@ # 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 user_manager import UserManager from db import User, session as db_sess +import uuid, csv import config 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: 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')) @app.route('/user') @@ -107,6 +108,39 @@ def del_user(uid): 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') def lastfm_reg(): token = request.args.get('token')