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:
parent
cc838f14d3
commit
67670aace8
@ -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:
|
||||||
|
@ -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
|
|
||||||
ferr = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
func(Track, eid)
|
tid = get_entity_id(Track, eid)
|
||||||
except Exception as e:
|
except GenericError:
|
||||||
terr = e
|
tid = None
|
||||||
try:
|
try:
|
||||||
func(Folder, eid)
|
fid = get_entity_id(Folder, eid)
|
||||||
except GenericError as e:
|
except GenericError:
|
||||||
if e.message == "Invalid ID" and isinstance(terr, ObjectNotFound):
|
fid = None
|
||||||
ferr = NotFound("Folder not in database")
|
err = None
|
||||||
else:
|
|
||||||
ferr = e
|
|
||||||
except Exception as e:
|
|
||||||
ferr = e
|
|
||||||
|
|
||||||
if terr and ferr:
|
if tid is None and fid is None:
|
||||||
errors += [terr, ferr]
|
raise GenericError("Invalid ID")
|
||||||
|
|
||||||
|
if tid is not None:
|
||||||
|
try:
|
||||||
|
func(Track, tid)
|
||||||
|
except Exception as e:
|
||||||
|
err = e
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
func(Folder, fid)
|
||||||
|
except Exception as e:
|
||||||
|
err = e
|
||||||
|
|
||||||
|
if err:
|
||||||
|
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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user