mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 17:06:17 +00:00
Porting supysonic.api.browse
This commit is contained in:
parent
7401b4dec9
commit
95f77cc170
@ -96,8 +96,7 @@ def get_entity(cls, param="id"):
|
|||||||
eid = int(eid)
|
eid = int(eid)
|
||||||
else:
|
else:
|
||||||
eid = uuid.UUID(eid)
|
eid = uuid.UUID(eid)
|
||||||
entity = cls[eid]
|
return cls[eid]
|
||||||
return entity
|
|
||||||
|
|
||||||
|
|
||||||
def get_entity_id(cls, eid):
|
def get_entity_id(cls, eid):
|
||||||
@ -107,12 +106,12 @@ def get_entity_id(cls, eid):
|
|||||||
raise GenericError("Invalid ID")
|
raise GenericError("Invalid ID")
|
||||||
try:
|
try:
|
||||||
return int(eid)
|
return int(eid)
|
||||||
except ValueError:
|
except ValueError as e:
|
||||||
raise GenericError("Invalid ID")
|
raise GenericError("Invalid ID") from e
|
||||||
try:
|
try:
|
||||||
return uuid.UUID(eid)
|
return uuid.UUID(eid)
|
||||||
except (AttributeError, ValueError):
|
except (AttributeError, ValueError) as e:
|
||||||
raise GenericError("Invalid ID")
|
raise GenericError("Invalid ID") from e
|
||||||
|
|
||||||
|
|
||||||
def get_root_folder(id):
|
def get_root_folder(id):
|
||||||
@ -121,14 +120,13 @@ def get_root_folder(id):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
fid = int(id)
|
fid = int(id)
|
||||||
except ValueError:
|
except ValueError as e:
|
||||||
raise ValueError("Invalid folder ID")
|
raise ValueError("Invalid folder ID") from e
|
||||||
|
|
||||||
folder = Folder.get(id=fid, root=True)
|
try:
|
||||||
if folder is None:
|
return Folder.get(id=fid, root=True)
|
||||||
raise NotFound("Folder")
|
except Folder.DoesNotExist as e:
|
||||||
|
raise NotFound("Folder") from e
|
||||||
return folder
|
|
||||||
|
|
||||||
|
|
||||||
from .errors import *
|
from .errors import *
|
||||||
|
@ -9,6 +9,7 @@ import re
|
|||||||
import string
|
import string
|
||||||
|
|
||||||
from flask import current_app, request
|
from flask import current_app, request
|
||||||
|
from peewee import fn
|
||||||
|
|
||||||
from ..db import Folder, Artist, Album, Track
|
from ..db import Folder, Artist, Album, Track
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ def list_folders():
|
|||||||
{
|
{
|
||||||
"musicFolder": [
|
"musicFolder": [
|
||||||
{"id": str(f.id), "name": f.name}
|
{"id": str(f.id), "name": f.name}
|
||||||
for f in Folder.select(lambda f: f.root).order_by(Folder.name)
|
for f in Folder.select().where(Folder.root).order_by(Folder.name)
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -77,7 +78,7 @@ def list_indexes():
|
|||||||
ifModifiedSince = int(ifModifiedSince) / 1000
|
ifModifiedSince = int(ifModifiedSince) / 1000
|
||||||
|
|
||||||
if musicFolderId is None:
|
if musicFolderId is None:
|
||||||
folders = Folder.select(lambda f: f.root)[:]
|
folders = Folder.select().where(Folder.root)[:]
|
||||||
else:
|
else:
|
||||||
folders = [get_root_folder(musicFolderId)]
|
folders = [get_root_folder(musicFolderId)]
|
||||||
|
|
||||||
@ -95,8 +96,8 @@ def list_indexes():
|
|||||||
artists = []
|
artists = []
|
||||||
children = []
|
children = []
|
||||||
for f in folders:
|
for f in folders:
|
||||||
artists += f.children.select()[:]
|
artists += f.children[:]
|
||||||
children += f.tracks.select()[:]
|
children += f.tracks[:]
|
||||||
|
|
||||||
indexes = build_indexes(artists)
|
indexes = build_indexes(artists)
|
||||||
return request.formatter(
|
return request.formatter(
|
||||||
@ -137,9 +138,11 @@ def list_genres():
|
|||||||
{
|
{
|
||||||
"genre": [
|
"genre": [
|
||||||
{"value": genre, "songCount": sc, "albumCount": ac}
|
{"value": genre, "songCount": sc, "albumCount": ac}
|
||||||
for genre, sc, ac in select(
|
for genre, sc, ac in Track.select(
|
||||||
(t.genre, count(), count(t.album)) for t in Track if t.genre
|
Track.genre, fn.count(), fn.count(Track.album.distinct())
|
||||||
)
|
)
|
||||||
|
.group_by(Track.genre)
|
||||||
|
.tuples()
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -152,7 +155,7 @@ def list_artists():
|
|||||||
query = Artist.select()
|
query = Artist.select()
|
||||||
if mfid is not None:
|
if mfid is not None:
|
||||||
folder = get_root_folder(mfid)
|
folder = get_root_folder(mfid)
|
||||||
query = Artist.select(lambda a: folder in a.tracks.root_folder)
|
query = Artist.select().join(Track).where(Track.root_folder == folder)
|
||||||
|
|
||||||
indexes = build_indexes(query)
|
indexes = build_indexes(query)
|
||||||
return request.formatter(
|
return request.formatter(
|
||||||
|
@ -150,7 +150,7 @@ class Folder(PathMixin, db.Model):
|
|||||||
"name": self.name,
|
"name": self.name,
|
||||||
"child": [
|
"child": [
|
||||||
f.as_subsonic_child(user)
|
f.as_subsonic_child(user)
|
||||||
for f in self.children.order_by(lambda c: c.name.lower())
|
for f in self.children.order_by(fn.lower(Folder.name))
|
||||||
]
|
]
|
||||||
+ [
|
+ [
|
||||||
t.as_subsonic_child(user, client)
|
t.as_subsonic_child(user, client)
|
||||||
|
@ -9,8 +9,6 @@ import time
|
|||||||
import unittest
|
import unittest
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from pony.orm import db_session
|
|
||||||
|
|
||||||
from supysonic.db import Folder, Artist, Album, Track
|
from supysonic.db import Folder, Artist, Album, Track
|
||||||
|
|
||||||
from .apitestbase import ApiTestBase
|
from .apitestbase import ApiTestBase
|
||||||
@ -20,30 +18,31 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
with db_session:
|
self.empty_root = Folder.create(root=True, name="Empty root", path="/tmp")
|
||||||
self.empty_root = Folder(root=True, name="Empty root", path="/tmp")
|
self.root = Folder.create(root=True, name="Root folder", path="tests/assets")
|
||||||
self.root = Folder(root=True, name="Root folder", path="tests/assets")
|
|
||||||
|
|
||||||
for letter in "ABC":
|
for letter in "ABC":
|
||||||
folder = Folder(
|
folder = Folder.create(
|
||||||
name=letter + "rtist",
|
name=letter + "rtist",
|
||||||
path="tests/assets/{}rtist".format(letter),
|
path="tests/assets/{}rtist".format(letter),
|
||||||
|
root=False,
|
||||||
parent=self.root,
|
parent=self.root,
|
||||||
)
|
)
|
||||||
|
|
||||||
artist = Artist(name=letter + "rtist")
|
artist = Artist.create(name=letter + "rtist")
|
||||||
|
|
||||||
for lether in "AB":
|
for lether in "AB":
|
||||||
afolder = Folder(
|
afolder = Folder.create(
|
||||||
name=letter + lether + "lbum",
|
name=letter + lether + "lbum",
|
||||||
path="tests/assets/{0}rtist/{0}{1}lbum".format(letter, lether),
|
path="tests/assets/{0}rtist/{0}{1}lbum".format(letter, lether),
|
||||||
|
root=False,
|
||||||
parent=folder,
|
parent=folder,
|
||||||
)
|
)
|
||||||
|
|
||||||
album = Album(name=letter + lether + "lbum", artist=artist)
|
album = Album.create(name=letter + lether + "lbum", artist=artist)
|
||||||
|
|
||||||
for num, song in enumerate(["One", "Two", "Three"]):
|
for num, song in enumerate(["One", "Two", "Three"]):
|
||||||
Track(
|
Track.create(
|
||||||
disc=1,
|
disc=1,
|
||||||
number=num,
|
number=num,
|
||||||
title=song,
|
title=song,
|
||||||
@ -61,7 +60,7 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(Folder.select().count(), 11)
|
self.assertEqual(Folder.select().count(), 11)
|
||||||
self.assertEqual(Folder.select(lambda f: f.root).count(), 2)
|
self.assertEqual(Folder.select().where(Folder.root).count(), 2)
|
||||||
self.assertEqual(Artist.select().count(), 3)
|
self.assertEqual(Artist.select().count(), 3)
|
||||||
self.assertEqual(Album.select().count(), 6)
|
self.assertEqual(Album.select().count(), 6)
|
||||||
self.assertEqual(Track.select().count(), 18)
|
self.assertEqual(Track.select().count(), 18)
|
||||||
@ -86,7 +85,6 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(len(child), 0)
|
self.assertEqual(len(child), 0)
|
||||||
|
|
||||||
with db_session:
|
|
||||||
fid = Folder.get(name="Empty root").id
|
fid = Folder.get(name="Empty root").id
|
||||||
rv, child = self._make_request(
|
rv, child = self._make_request(
|
||||||
"getIndexes", {"musicFolderId": str(fid)}, tag="indexes"
|
"getIndexes", {"musicFolderId": str(fid)}, tag="indexes"
|
||||||
@ -106,7 +104,6 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
self._make_request("getMusicDirectory", {"id": 1234567890}, error=70)
|
self._make_request("getMusicDirectory", {"id": 1234567890}, error=70)
|
||||||
|
|
||||||
# should test with folders with both children folders and tracks. this code would break in that case
|
# should test with folders with both children folders and tracks. this code would break in that case
|
||||||
with db_session:
|
|
||||||
for f in Folder.select():
|
for f in Folder.select():
|
||||||
rv, child = self._make_request(
|
rv, child = self._make_request(
|
||||||
"getMusicDirectory", {"id": str(f.id)}, tag="directory"
|
"getMusicDirectory", {"id": str(f.id)}, tag="directory"
|
||||||
@ -158,7 +155,6 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
self._make_request("getArtist", {"id": "artist"}, error=0)
|
self._make_request("getArtist", {"id": "artist"}, error=0)
|
||||||
self._make_request("getArtist", {"id": str(uuid.uuid4())}, error=70)
|
self._make_request("getArtist", {"id": str(uuid.uuid4())}, error=70)
|
||||||
|
|
||||||
with db_session:
|
|
||||||
for ar in Artist.select():
|
for ar in Artist.select():
|
||||||
rv, child = self._make_request(
|
rv, child = self._make_request(
|
||||||
"getArtist", {"id": str(ar.id)}, tag="artist"
|
"getArtist", {"id": str(ar.id)}, tag="artist"
|
||||||
@ -181,7 +177,6 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
self._make_request("getAlbum", {"id": "nastynasty"}, error=0)
|
self._make_request("getAlbum", {"id": "nastynasty"}, error=0)
|
||||||
self._make_request("getAlbum", {"id": str(uuid.uuid4())}, error=70)
|
self._make_request("getAlbum", {"id": str(uuid.uuid4())}, error=70)
|
||||||
|
|
||||||
with db_session:
|
|
||||||
a = Album.select().first()
|
a = Album.select().first()
|
||||||
rv, child = self._make_request("getAlbum", {"id": str(a.id)}, tag="album")
|
rv, child = self._make_request("getAlbum", {"id": str(a.id)}, tag="album")
|
||||||
self.assertEqual(child.get("id"), str(a.id))
|
self.assertEqual(child.get("id"), str(a.id))
|
||||||
@ -201,7 +196,6 @@ class BrowseTestCase(ApiTestBase):
|
|||||||
self._make_request("getSong", {"id": "nastynasty"}, error=0)
|
self._make_request("getSong", {"id": "nastynasty"}, error=0)
|
||||||
self._make_request("getSong", {"id": str(uuid.uuid4())}, error=70)
|
self._make_request("getSong", {"id": str(uuid.uuid4())}, error=70)
|
||||||
|
|
||||||
with db_session:
|
|
||||||
s = Track.select().first()
|
s = Track.select().first()
|
||||||
self._make_request("getSong", {"id": str(s.id)}, tag="song")
|
self._make_request("getSong", {"id": str(s.id)}, tag="song")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user