1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 17:06: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): def get_entity_id(cls, eid):
"""Return the entity ID as its proper type.""" """Return the entity ID as its proper type."""
if cls == Folder: if cls == Folder:
if isinstance(eid, uuid.UUID):
raise GenericError("Invalid ID")
try: try:
return int(eid) return int(eid)
except ValueError: except ValueError:

View File

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