From 584f1cf90c29bc7e3dfbcad6b34bbb104a06a743 Mon Sep 17 00:00:00 2001 From: Alban Date: Sun, 11 Nov 2012 00:01:52 +0100 Subject: [PATCH] Implemented getMusicDirectory.view --- api/browse.py | 26 +++++++++++++++++++++++++- db.py | 15 +++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/api/browse.py b/api/browse.py index 15f016c..3b6af3c 100755 --- a/api/browse.py +++ b/api/browse.py @@ -79,7 +79,31 @@ def list_indexes(): 'name': a.name } for a in sorted(v, key = lambda a: a.name.lower()) ] } for k, v in sorted(indexes.iteritems()) ], - 'child': [ c.as_subsonic_child() for c in sorted(childs, key = lambda t: t.album.artist.name + t.album.name + str(t.disc) + str(t.number) + t.title ) ] + 'child': [ c.as_subsonic_child() for c in sorted(childs, key = lambda t: t.sort_key()) ] } }) +@app.route('/rest/getMusicDirectory.view') +def show_directory(): + did = request.args.get('id') + if not did: + return request.error_formatter(10, 'Missing directory id') + try: + fid = uuid.UUID(did) + except: + return request.error_formatter(0, 'Invalid directory id') + + folder = Folder.query.get(fid) + if not folder: + return request.error_formatter(70, 'Directory not found') + + directory = { + 'id': str(folder.id), + 'name': folder.name, + 'child': [ f.as_subsonic_child() for f in sorted(folder.children, key = lambda c: c.name) ] + [ t.as_subsonic_child() for t in sorted(folder.tracks, key = lambda t: t.sort_key()) ] + } + if not folder.root: + directory['parent'] = str(folder.parent_id) + + return request.formatter({ 'directory': directory }) + diff --git a/db.py b/db.py index cccaa6e..d7e6448 100755 --- a/db.py +++ b/db.py @@ -67,6 +67,18 @@ class Folder(Base): parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True) children = relationship('Folder', backref = backref('parent', remote_side = [ id ])) + def as_subsonic_child(self): + info = { + 'id': str(self.id), + 'isDir': True, + 'title': self.name, + } + if not self.root: + info['parent'] = str(self.parent_id) + info['artist'] = self.parent.name + + return info + class Artist(Base): __tablename__ = 'artist' @@ -144,6 +156,9 @@ class Track(Base): ret = '%02i:%s' % (self.duration / 3600, ret) return ret + def sort_key(self): + return self.album.artist.name + self.album.name + ("%02i" % self.disc) + ("%02i" % self.number) + self.title + def init_db(): Base.metadata.create_all(bind = engine)