From 6b2a5f5cecc78808ad5f80aa94d9249bcf4a2508 Mon Sep 17 00:00:00 2001 From: Alban Date: Thu, 22 Nov 2012 16:36:03 +0100 Subject: [PATCH] Beginning og LastFM support: one can link an account --- db.py | 2 ++ templates/profile.html | 33 ++++++++++++++++++++----------- user.py | 45 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/db.py b/db.py index f84a340..93c9c24 100755 --- a/db.py +++ b/db.py @@ -54,6 +54,8 @@ class User(Base): password = Column(String(40)) salt = Column(String(6)) admin = Column(Boolean, default = False) + lastfm_session = Column(String(32), nullable = True) + lastfm_status = Column(Boolean, default = True) # True: ok/unlinked, False: invalid session class Folder(Base): __tablename__ = 'folder' diff --git a/templates/profile.html b/templates/profile.html index 4ca32db..ba2281f 100755 --- a/templates/profile.html +++ b/templates/profile.html @@ -1,11 +1,22 @@ -{% extends "layout.html" %} -{% block body %} -

{{ user.name }}

- -{% endblock %} - +{% extends "layout.html" %} +{% block body %} +

{{ user.name }}

+ +{% endblock %} + diff --git a/user.py b/user.py index fcb28e7..348e54b 100755 --- a/user.py +++ b/user.py @@ -1,10 +1,12 @@ # coding: utf-8 from flask import request, session, flash, render_template, redirect, url_for +import requests, hashlib from web import app from user_manager import UserManager from db import User, session as db_sess +import config @app.before_request def check_admin(): @@ -23,7 +25,7 @@ def user_index(): @app.route('/user/me') def user_profile(): - return render_template('profile.html', user = UserManager.get(session.get('userid'))[1]) + return render_template('profile.html', user = UserManager.get(session.get('userid'))[1], api_key = config.get('LASTFM_KEY')) @app.route('/user/changemail', methods = [ 'GET', 'POST' ]) def change_mail(): @@ -105,6 +107,47 @@ def del_user(uid): return redirect(url_for('user_index')) +@app.route('/user/lastfm/link') +def lastfm_reg(): + token = request.args.get('token') + if token in ('', None): + flash('Missing LastFM auth token') + return redirect(url_for('user_profile')) + + p = { + 'api_key': config.get('LASTFM_KEY'), + 'method': 'auth.getSession', + 'token': token + } + sig_str = '' + for k, v in sorted(p.iteritems()): + sig_str += k + v + sig = hashlib.md5(sig_str + config.get('LASTFM_SECRET')).hexdigest() + + p['api_sig'] = sig + p['format'] = 'json' + + r = requests.get('http://ws.audioscrobbler.com/2.0/', params = p) + if 'error' in r.json: + flash('Error %i: %s' % (r.json['error'], r.json['message'])) + else: + user = UserManager.get(session.get('userid'))[1] + user.lastfm_session = r.json['session']['key'] + user.lastfm_status = True + db_sess.commit() + flash('Successfully linked LastFM account') + + return redirect(url_for('user_profile')) + +@app.route('/user/lastfm/unlink') +def lastfm_unreg(): + user = UserManager.get(session.get('userid'))[1] + user.lastfm_session = None + user.lastfm_status = True + db_sess.commit() + flash('Unliked LastFM account') + return redirect(url_for('user_profile')) + @app.route('/user/login', methods = [ 'GET', 'POST']) def login(): return_url = request.args.get('returnUrl') or url_for('index')