From 20e09a6321e43ef56e16b765a30d0a25476910dc Mon Sep 17 00:00:00 2001 From: spl0k Date: Thu, 13 Jun 2013 18:17:33 +0200 Subject: [PATCH] User object and name stored in the request object to provide easy access --- api/__init__.py | 14 +++++++++++--- api/albums_songs.py | 34 ++++++++++------------------------ api/annotation.py | 24 +++++------------------- api/media.py | 11 +++-------- api/user.py | 4 ++-- 5 files changed, 31 insertions(+), 56 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 57565bb..8cc3961 100755 --- a/api/__init__.py +++ b/api/__init__.py @@ -39,8 +39,12 @@ def authorize(): error = request.error_formatter(40, 'Unauthorized'), 401 - if request.authorization and UserManager.try_auth(request.authorization.username, request.authorization.password)[0] == UserManager.SUCCESS: - return + if request.authorization: + status, user = UserManager.try_auth(request.authorization.username, request.authorization.password) + if status == UserManager.SUCCESS: + request.username = request.authorization.username + request.user = user + return (username, decoded_pass) = map(request.args.get, [ 'u', 'p' ]) if not username or not decoded_pass: @@ -49,9 +53,13 @@ def authorize(): if decoded_pass.startswith('enc:'): decoded_pass = hexdecode(decoded_pass[4:]) - if UserManager.try_auth(username, decoded_pass)[0] != UserManager.SUCCESS: + status, user = UserManager.try_auth(username, decoded_pass) + if status != UserManager.SUCCESS: return error + request.username = username + request.user = user + @app.after_request def set_content_type(response): if not request.path.startswith('/rest/'): diff --git a/api/albums_songs.py b/api/albums_songs.py index e675343..54669bf 100755 --- a/api/albums_songs.py +++ b/api/albums_songs.py @@ -44,16 +44,13 @@ def rand_songs(): @app.route('/rest/getAlbumList.view', methods = [ 'GET', 'POST' ]) def album_list(): - username, ltype, size, offset = map(request.args.get, [ 'u', 'type', 'size', 'offset' ]) + ltype, size, offset = map(request.args.get, [ 'type', 'size', 'offset' ]) try: size = int(size) if size else 10 offset = int(offset) if offset else 0 except: return request.error_formatter(0, 'Invalid parameter format') - if not username: - username = request.authorization.username - query = Folder.query.filter(Folder.tracks.any()) if ltype == 'random': albums = query.all() @@ -71,7 +68,7 @@ def album_list(): elif ltype == 'recent': query = query.join(Track, Folder.tracks).group_by(Folder.id).order_by(desc(func.max(Track.last_play))) elif ltype == 'starred': - query = query.join(StarredFolder).join(User).filter(User.name == username) + query = query.join(StarredFolder).join(User).filter(User.name == request.username) elif ltype == 'alphabeticalByName': query = query.order_by(Folder.name) elif ltype == 'alphabeticalByArtist': @@ -88,16 +85,13 @@ def album_list(): @app.route('/rest/getAlbumList2.view', methods = [ 'GET', 'POST' ]) def album_list_id3(): - username, ltype, size, offset = map(request.args.get, [ 'u', 'type', 'size', 'offset' ]) + ltype, size, offset = map(request.args.get, [ 'type', 'size', 'offset' ]) try: size = int(size) if size else 10 offset = int(offset) if offset else 0 except: return request.error_formatter(0, 'Invalid parameter format') - if not username: - username = request.authorization.username - query = Album.query if ltype == 'random': albums = query.all() @@ -113,7 +107,7 @@ def album_list_id3(): elif ltype == 'recent': query = query.join(Track, Album.tracks).group_by(Album.id).order_by(desc(func.max(Track.last_play))) elif ltype == 'starred': - query = query.join(StarredAlbum).join(User).filter(User.name == username) + query = query.join(StarredAlbum).join(User).filter(User.name == request.username) elif ltype == 'alphabeticalByName': query = query.order_by(Album.name) elif ltype == 'alphabeticalByArtist': @@ -142,29 +136,21 @@ def now_playing(): @app.route('/rest/getStarred.view', methods = [ 'GET', 'POST' ]) def get_starred(): - username = request.args.get('u') - if not username: - username = request.authorization.username - return request.formatter({ 'starred': { - 'artist': [ { 'id': sf.starred.name, 'name': sf.starred_id } for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == username).filter(~ Folder.tracks.any()) ], - 'album': [ sf.starred.as_subsonic_child() for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == username).filter(Folder.tracks.any()) ], - 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == username) ] + 'artist': [ { 'id': sf.starred.name, 'name': sf.starred_id } for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(~ Folder.tracks.any()) ], + 'album': [ sf.starred.as_subsonic_child() for sf in StarredFolder.query.join(User).join(Folder).filter(User.name == request.username).filter(Folder.tracks.any()) ], + 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == request.username) ] } }) @app.route('/rest/getStarred2.view', methods = [ 'GET', 'POST' ]) def get_starred_id3(): - username = request.args.get('u') - if not username: - username = request.authorization.username - return request.formatter({ 'starred2': { - 'artist': [ sa.starred.as_subsonic_artist() for sa in StarredArtist.query.join(User).filter(User.name == username) ], - 'album': [ sa.starred.as_subsonic_album() for sa in StarredAlbum.query.join(User).filter(User.name == username) ], - 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == username) ] + 'artist': [ sa.starred.as_subsonic_artist() for sa in StarredArtist.query.join(User).filter(User.name == request.username) ], + 'album': [ sa.starred.as_subsonic_album() for sa in StarredAlbum.query.join(User).filter(User.name == request.username) ], + 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == request.username) ] } }) diff --git a/api/annotation.py b/api/annotation.py index 0af3771..84693c9 100755 --- a/api/annotation.py +++ b/api/annotation.py @@ -12,22 +12,17 @@ from db import * def star(): id, albumId, artistId = map(request.args.getlist, [ 'id', 'albumId', 'artistId' ]) - username = request.args.get('u') - if not username: - username = request.authorization.username - user = User.query.filter(User.name == username).one() - def try_star(ent, starred_ent, eid): try: uid = uuid.UUID(eid) except: return 2, request.error_formatter(0, 'Invalid %s id' % ent.__name__) - if starred_ent.query.filter(starred_ent.user_id == user.id).filter(starred_ent.starred_id == uid).first(): + if starred_ent.query.filter(starred_ent.user_id == request.user.id).filter(starred_ent.starred_id == uid).first(): return 2, request.error_formatter(0, '%s already starred' % ent.__name__) e = ent.query.get(uid) if e: - session.add(starred_ent(user = user, starred = e)) + session.add(starred_ent(user = request.user, starred = e)) else: return 1, request.error_formatter(70, 'Unknown %s id' % ent.__name__) @@ -59,18 +54,13 @@ def star(): def unstar(): id, albumId, artistId = map(request.args.getlist, [ 'id', 'albumId', 'artistId' ]) - username = request.args.get('u') - if not username: - username = request.authorization.username - user = User.query.filter(User.name == username).one() - def try_unstar(ent, eid): try: uid = uuid.UUID(eid) except: return request.error_formatter(0, 'Invalid id') - ent.query.filter(ent.user_id == user.id).filter(ent.starred_id == uid).delete() + ent.query.filter(ent.user_id == request.user.id).filter(ent.starred_id == uid).delete() return None for eid in id: @@ -100,7 +90,7 @@ def scrobble(): if not status: return res - t, submission, u = map(request.args.get, [ 'time', 'submission', 'u' ]) + t, submission = map(request.args.get, [ 'time', 'submission' ]) if t: try: @@ -110,11 +100,7 @@ def scrobble(): else: t = int(time()) - if u: - user = User.query.filter(User.name == u).one() - else: - user = User.query.filter(User.name == request.authorization.username).one() - lfm = LastFm(user, app.logger) + lfm = LastFm(request.user, app.logger) if submission in (None, '', True, 'true', 'True', 1, '1'): lfm.scrobble(res, t) diff --git a/api/media.py b/api/media.py index 0292694..a2479ed 100755 --- a/api/media.py +++ b/api/media.py @@ -15,7 +15,7 @@ def stream_media(): if not status: return res - maxBitRate, format, timeOffset, size, estimateContentLength, u = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength', 'u' ]) + maxBitRate, format, timeOffset, size, estimateContentLength = map(request.args.get, [ 'maxBitRate', 'format', 'timeOffset', 'size', 'estimateContentLength' ]) if maxBitRate: try: @@ -31,15 +31,10 @@ def stream_media(): # TODO transcode pass - if u: - user = User.query.filter(User.name == u).one() - else: - user = User.query.filter(User.name == request.authorization.username).one() - res.play_count = res.play_count + 1 res.last_play = now() - user.last_play = res - user.last_play_date = now() + request.user.last_play = res + request.user.last_play_date = now() session.commit() if estimateContentLength == 'true': diff --git a/api/user.py b/api/user.py index a24f72f..c023174 100755 --- a/api/user.py +++ b/api/user.py @@ -20,8 +20,8 @@ def user_info(): 'email': user.mail, 'scrobblingEnabled': user.lastfm_session is not None and user.lastfm_status, 'adminRole': user.admin, - 'settingsRole': False, - 'downloadRole': False, + 'settingsRole': user.admin, + 'downloadRole': True, 'uploadRole': False, 'playlistRole': False, 'coverArtRole': False,