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

Converting api:playlists

This commit is contained in:
spl0k 2014-04-09 20:41:07 +02:00
parent f9cd584a65
commit 54e4a77ae5
2 changed files with 25 additions and 24 deletions

View File

@ -19,22 +19,22 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from flask import request from flask import request
from sqlalchemy import or_, func from storm.expr import Or
import uuid import uuid
from web import app from web import app, store
from db import Playlist, User, Track, session from db import Playlist, User, Track
from . import get_entity from . import get_entity
@app.route('/rest/getPlaylists.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/getPlaylists.view', methods = [ 'GET', 'POST' ])
def list_playlists(): def list_playlists():
query = Playlist.query.filter(or_(Playlist.user_id == request.user.id, Playlist.public == True)).order_by(func.lower(Playlist.name)) query = store.find(Playlist, Or(Playlist.user_id == request.user.id, Playlist.public == True)).order_by(Playlist.name)
username = request.args.get('username') username = request.args.get('username')
if username: if username:
if not request.user.admin: if not request.user.admin:
return request.error_formatter(50, 'Restricted to admins') return request.error_formatter(50, 'Restricted to admins')
query = Playlist.query.join(User).filter(User.name == username).order_by(func.lower(Playlist.name)) query = store.find(Playlist, Playlist.user_id == User.id, User.name == username).order_by(Playlist.name)
return request.formatter({ 'playlists': { 'playlist': [ p.as_subsonic_playlist(request.user) for p in query ] } }) return request.formatter({ 'playlists': { 'playlist': [ p.as_subsonic_playlist(request.user) for p in query ] } })
@ -61,30 +61,32 @@ def create_playlist():
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
if playlist_id: if playlist_id:
playlist = Playlist.query.get(playlist_id) playlist = store.get(Playlist, playlist_id)
if not playlist: if not playlist:
return request.error_formatter(70, 'Unknwon playlist') return request.error_formatter(70, 'Unknwon playlist')
if playlist.user_id != request.user.id and not request.user.admin: if playlist.user_id != request.user.id and not request.user.admin:
return request.error_formatter(50, "You're not allowed to modify a playlist that isn't yours") return request.error_formatter(50, "You're not allowed to modify a playlist that isn't yours")
playlist.tracks = [] playlist.tracks.clear()
if name: if name:
playlist.name = name playlist.name = name
elif name: elif name:
playlist = Playlist(user = request.user, name = name) playlist = Playlist()
session.add(playlist) playlist.user_id = request.user.id
playlist.name = name
store.add(playlist)
else: else:
return request.error_formatter(10, 'Missing playlist id or name') return request.error_formatter(10, 'Missing playlist id or name')
for sid in songs: for sid in songs:
track = Track.query.get(sid) track = store.get(Track, sid)
if not track: if not track:
return request.error_formatter(70, 'Unknown song') return request.error_formatter(70, 'Unknown song')
playlist.tracks.append(track) playlist.tracks.add(track)
session.commit() store.commit()
return request.formatter({}) return request.formatter({})
@app.route('/rest/deletePlaylist.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/deletePlaylist.view', methods = [ 'GET', 'POST' ])
@ -96,8 +98,8 @@ def delete_playlist():
if res.user_id != request.user.id and not request.user.admin: if res.user_id != request.user.id and not request.user.admin:
return request.error_formatter(50, "You're not allowed to delete a playlist that isn't yours") return request.error_formatter(50, "You're not allowed to delete a playlist that isn't yours")
session.delete(res) store.remove(res)
session.commit() store.commit()
return request.formatter({}) return request.formatter({})
@app.route('/rest/updatePlaylist.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/updatePlaylist.view', methods = [ 'GET', 'POST' ])
@ -125,21 +127,20 @@ def update_playlist():
if public: if public:
playlist.public = public in (True, 'True', 'true', 1, '1') playlist.public = public in (True, 'True', 'true', 1, '1')
tracks = list(playlist.tracks)
for sid in to_add: for sid in to_add:
track = Track.query.get(sid) track = store.get(Track, sid)
if not track: if not track:
return request.error_formatter(70, 'Unknown song') return request.error_formatter(70, 'Unknown song')
if track not in playlist.tracks: if track not in playlist.tracks:
playlist.tracks.append(track) playlist.tracks.add(track)
offset = 0
for idx in to_remove: for idx in to_remove:
idx = idx - offset if idx < 0 or idx >= len(tracks):
if idx < 0 or idx >= len(playlist.tracks):
return request.error_formatter(0, 'Index out of range') return request.error_formatter(0, 'Index out of range')
playlist.tracks.pop(idx) playlist.tracks.remove(tracks[idx])
offset += 1
session.commit() store.commit()
return request.formatter({}) return request.formatter({})

4
db.py
View File

@ -353,8 +353,8 @@ class Playlist(object):
'name': self.name if self.user_id == user.id else '[%s] %s' % (self.user.name, self.name), 'name': self.name if self.user_id == user.id else '[%s] %s' % (self.user.name, self.name),
'owner': self.user.name, 'owner': self.user.name,
'public': self.public, 'public': self.public,
'songCount': len(self.tracks), 'songCount': self.tracks.count(),
'duration': sum(map(lambda t: t.duration, self.tracks)), 'duration': self.tracks.find().sum(Track.duration),
'created': self.created.isoformat() 'created': self.created.isoformat()
} }
if self.comment: if self.comment: