1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06:17 +00:00

User object and name stored in the request object to provide easy access

This commit is contained in:
spl0k 2013-06-13 18:17:33 +02:00
parent afe167d58a
commit 20e09a6321
5 changed files with 31 additions and 56 deletions

View File

@ -39,8 +39,12 @@ def authorize():
error = request.error_formatter(40, 'Unauthorized'), 401 error = request.error_formatter(40, 'Unauthorized'), 401
if request.authorization and UserManager.try_auth(request.authorization.username, request.authorization.password)[0] == UserManager.SUCCESS: if request.authorization:
return 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' ]) (username, decoded_pass) = map(request.args.get, [ 'u', 'p' ])
if not username or not decoded_pass: if not username or not decoded_pass:
@ -49,9 +53,13 @@ def authorize():
if decoded_pass.startswith('enc:'): if decoded_pass.startswith('enc:'):
decoded_pass = hexdecode(decoded_pass[4:]) 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 return error
request.username = username
request.user = user
@app.after_request @app.after_request
def set_content_type(response): def set_content_type(response):
if not request.path.startswith('/rest/'): if not request.path.startswith('/rest/'):

View File

@ -44,16 +44,13 @@ def rand_songs():
@app.route('/rest/getAlbumList.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/getAlbumList.view', methods = [ 'GET', 'POST' ])
def album_list(): 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: try:
size = int(size) if size else 10 size = int(size) if size else 10
offset = int(offset) if offset else 0 offset = int(offset) if offset else 0
except: except:
return request.error_formatter(0, 'Invalid parameter format') return request.error_formatter(0, 'Invalid parameter format')
if not username:
username = request.authorization.username
query = Folder.query.filter(Folder.tracks.any()) query = Folder.query.filter(Folder.tracks.any())
if ltype == 'random': if ltype == 'random':
albums = query.all() albums = query.all()
@ -71,7 +68,7 @@ def album_list():
elif ltype == 'recent': elif ltype == 'recent':
query = query.join(Track, Folder.tracks).group_by(Folder.id).order_by(desc(func.max(Track.last_play))) query = query.join(Track, Folder.tracks).group_by(Folder.id).order_by(desc(func.max(Track.last_play)))
elif ltype == 'starred': 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': elif ltype == 'alphabeticalByName':
query = query.order_by(Folder.name) query = query.order_by(Folder.name)
elif ltype == 'alphabeticalByArtist': elif ltype == 'alphabeticalByArtist':
@ -88,16 +85,13 @@ def album_list():
@app.route('/rest/getAlbumList2.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/getAlbumList2.view', methods = [ 'GET', 'POST' ])
def album_list_id3(): 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: try:
size = int(size) if size else 10 size = int(size) if size else 10
offset = int(offset) if offset else 0 offset = int(offset) if offset else 0
except: except:
return request.error_formatter(0, 'Invalid parameter format') return request.error_formatter(0, 'Invalid parameter format')
if not username:
username = request.authorization.username
query = Album.query query = Album.query
if ltype == 'random': if ltype == 'random':
albums = query.all() albums = query.all()
@ -113,7 +107,7 @@ def album_list_id3():
elif ltype == 'recent': elif ltype == 'recent':
query = query.join(Track, Album.tracks).group_by(Album.id).order_by(desc(func.max(Track.last_play))) query = query.join(Track, Album.tracks).group_by(Album.id).order_by(desc(func.max(Track.last_play)))
elif ltype == 'starred': 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': elif ltype == 'alphabeticalByName':
query = query.order_by(Album.name) query = query.order_by(Album.name)
elif ltype == 'alphabeticalByArtist': elif ltype == 'alphabeticalByArtist':
@ -142,29 +136,21 @@ def now_playing():
@app.route('/rest/getStarred.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/getStarred.view', methods = [ 'GET', 'POST' ])
def get_starred(): def get_starred():
username = request.args.get('u')
if not username:
username = request.authorization.username
return request.formatter({ return request.formatter({
'starred': { '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()) ], '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 == 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 == username) ] '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' ]) @app.route('/rest/getStarred2.view', methods = [ 'GET', 'POST' ])
def get_starred_id3(): def get_starred_id3():
username = request.args.get('u')
if not username:
username = request.authorization.username
return request.formatter({ return request.formatter({
'starred2': { 'starred2': {
'artist': [ sa.starred.as_subsonic_artist() for sa in StarredArtist.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 == 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 == username) ] 'song': [ st.starred.as_subsonic_child() for st in StarredTrack.query.join(User).filter(User.name == request.username) ]
} }
}) })

View File

@ -12,22 +12,17 @@ from db import *
def star(): def star():
id, albumId, artistId = map(request.args.getlist, [ 'id', 'albumId', 'artistId' ]) 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): def try_star(ent, starred_ent, eid):
try: try:
uid = uuid.UUID(eid) uid = uuid.UUID(eid)
except: except:
return 2, request.error_formatter(0, 'Invalid %s id' % ent.__name__) 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__) return 2, request.error_formatter(0, '%s already starred' % ent.__name__)
e = ent.query.get(uid) e = ent.query.get(uid)
if e: if e:
session.add(starred_ent(user = user, starred = e)) session.add(starred_ent(user = request.user, starred = e))
else: else:
return 1, request.error_formatter(70, 'Unknown %s id' % ent.__name__) return 1, request.error_formatter(70, 'Unknown %s id' % ent.__name__)
@ -59,18 +54,13 @@ def star():
def unstar(): def unstar():
id, albumId, artistId = map(request.args.getlist, [ 'id', 'albumId', 'artistId' ]) 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): def try_unstar(ent, eid):
try: try:
uid = uuid.UUID(eid) uid = uuid.UUID(eid)
except: except:
return request.error_formatter(0, 'Invalid id') 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 return None
for eid in id: for eid in id:
@ -100,7 +90,7 @@ def scrobble():
if not status: if not status:
return res return res
t, submission, u = map(request.args.get, [ 'time', 'submission', 'u' ]) t, submission = map(request.args.get, [ 'time', 'submission' ])
if t: if t:
try: try:
@ -110,11 +100,7 @@ def scrobble():
else: else:
t = int(time()) t = int(time())
if u: lfm = LastFm(request.user, app.logger)
user = User.query.filter(User.name == u).one()
else:
user = User.query.filter(User.name == request.authorization.username).one()
lfm = LastFm(user, app.logger)
if submission in (None, '', True, 'true', 'True', 1, '1'): if submission in (None, '', True, 'true', 'True', 1, '1'):
lfm.scrobble(res, t) lfm.scrobble(res, t)

View File

@ -15,7 +15,7 @@ def stream_media():
if not status: if not status:
return res 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: if maxBitRate:
try: try:
@ -31,15 +31,10 @@ def stream_media():
# TODO transcode # TODO transcode
pass 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.play_count = res.play_count + 1
res.last_play = now() res.last_play = now()
user.last_play = res request.user.last_play = res
user.last_play_date = now() request.user.last_play_date = now()
session.commit() session.commit()
if estimateContentLength == 'true': if estimateContentLength == 'true':

View File

@ -20,8 +20,8 @@ def user_info():
'email': user.mail, 'email': user.mail,
'scrobblingEnabled': user.lastfm_session is not None and user.lastfm_status, 'scrobblingEnabled': user.lastfm_session is not None and user.lastfm_status,
'adminRole': user.admin, 'adminRole': user.admin,
'settingsRole': False, 'settingsRole': user.admin,
'downloadRole': False, 'downloadRole': True,
'uploadRole': False, 'uploadRole': False,
'playlistRole': False, 'playlistRole': False,
'coverArtRole': False, 'coverArtRole': False,