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

Improve star/unstar handling

This commit is contained in:
mvn23 2019-10-09 18:57:42 +02:00
parent cc838f14d3
commit 67670aace8
2 changed files with 35 additions and 23 deletions

View File

@ -93,6 +93,8 @@ def get_entity(cls, param="id"):
def get_entity_id(cls, eid):
"""Return the entity ID as its proper type."""
if cls == Folder:
if isinstance(eid, uuid.UUID):
raise GenericError("Invalid ID")
try:
return int(eid)
except ValueError:

View File

@ -32,12 +32,14 @@ def star_single(cls, eid):
:param eid: id of the entity to star
"""
uid = get_entity_id(cls, eid)
e = cls[uid]
try:
e = cls[eid]
except ObjectNotFound:
raise NotFound("{} {}".format(cls.__name__, eid))
starred_cls = getattr(sys.modules[__name__], "Starred" + cls.__name__)
try:
starred_cls[request.user, uid]
starred_cls[request.user, eid]
raise GenericError("{} {} already starred".format(cls.__name__, eid))
except ObjectNotFound:
pass
@ -52,10 +54,9 @@ def unstar_single(cls, eid):
:param eid: id of the entity to unstar
"""
uid = get_entity_id(cls, eid)
starred_cls = getattr(sys.modules[__name__], "Starred" + cls.__name__)
delete(
s for s in starred_cls if s.user.id == request.user.id and s.starred.id == uid
s for s in starred_cls if s.user.id == request.user.id and s.starred.id == eid
)
return None
@ -68,35 +69,44 @@ def handle_star_request(func):
errors = []
for eid in id:
terr = None
ferr = None
try:
tid = get_entity_id(Track, eid)
except GenericError:
tid = None
try:
fid = get_entity_id(Folder, eid)
except GenericError:
fid = None
err = None
if tid is None and fid is None:
raise GenericError("Invalid ID")
if tid is not None:
try:
func(Track, eid)
func(Track, tid)
except Exception as e:
terr = e
try:
func(Folder, eid)
except GenericError as e:
if e.message == "Invalid ID" and isinstance(terr, ObjectNotFound):
ferr = NotFound("Folder not in database")
err = e
else:
ferr = e
try:
func(Folder, fid)
except Exception as e:
ferr = e
err = e
if terr and ferr:
errors += [terr, ferr]
if err:
errors.append(err)
for alId in albumId:
alb_id = get_entity_id(Album, alId)
try:
func(Album, alId)
func(Album, alb_id)
except Exception as e:
errors.append(e)
for arId in artistId:
art_id = get_entity_id(Artist, arId)
try:
func(Artist, arId)
func(Artist, art_id)
except Exception as e:
errors.append(e)