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

Porting supysonic.api.annotation

This commit is contained in:
Alban Féron 2022-12-18 17:24:05 +01:00
parent 2b472b4d97
commit 7401b4dec9
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
2 changed files with 77 additions and 114 deletions

View File

@ -28,16 +28,16 @@ def star_single(cls, starcls, eid):
try:
e = cls[eid]
except ObjectNotFound:
except cls.DoesNotExist:
raise NotFound("{} {}".format(cls.__name__, eid))
try:
starcls[request.user, eid]
raise GenericError("{} {} already starred".format(cls.__name__, eid))
except ObjectNotFound:
except starcls.DoesNotExist:
pass
starcls(user=request.user, starred=e)
starcls.create(user=request.user, starred=e)
def unstar_single(cls, starcls, eid):
@ -48,7 +48,9 @@ def unstar_single(cls, starcls, eid):
:param eid: id of the entity to unstar
"""
delete(s for s in starcls if s.user.id == request.user.id and s.starred.id == eid)
starcls.delete().where(
starcls.user == request.user, starcls.starred == eid
).execute()
def handle_star_request(func):
@ -139,17 +141,13 @@ def rate():
if rating == 0:
if tid is not None:
delete(
r
for r in RatingTrack
if r.user.id == request.user.id and r.rated.id == tid
)
RatingTrack.delete().where(
RatingTrack.user == request.user, RatingTrack.rated == tid
).execute()
else:
delete(
r
for r in RatingFolder
if r.user.id == request.user.id and r.rated.id == fid
)
RatingFolder.delete().where(
RatingFolder.user == request.user, RatingFolder.rated == fid
).execute()
else:
if tid is not None:
rated = Track[tid]
@ -163,8 +161,9 @@ def rate():
try:
rating_info = rating_cls[request.user, uid]
rating_info.rating = rating
except ObjectNotFound:
rating_cls(user=request.user, rated=rated, rating=rating)
rating_info.save()
except rating_cls.DoesNotExist:
rating_cls.create(user=request.user, rated=rated, rating=rating)
return request.formatter.empty

View File

@ -1,15 +1,13 @@
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017 Alban 'spl0k' Féron
# Copyright (C) 2017-2022 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.
import unittest
import uuid
from pony.orm import db_session
from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs
from .apitestbase import ApiTestBase
@ -19,17 +17,18 @@ class AnnotationTestCase(ApiTestBase):
def setUp(self):
super().setUp()
with db_session:
root = Folder(name="Root", root=True, path="tests")
folder = Folder(name="Folder", path="tests/assets", parent=root)
artist = Artist(name="Artist")
album = Album(name="Album", artist=artist)
root = Folder.create(name="Root", root=True, path="tests")
folder = Folder.create(
name="Folder", root=False, path="tests/assets", parent=root
)
artist = Artist.create(name="Artist")
album = Album.create(name="Album", artist=artist)
# Populate folder ids
root = Folder.get(name="Root")
folder = Folder.get(name="Folder")
track = Track(
track = Track.create(
title="Track",
album=album,
artist=artist,
@ -48,6 +47,7 @@ class AnnotationTestCase(ApiTestBase):
self.albumid = album.id
self.trackid = track.id
self.user = User.get(name="alice")
self.prefs = ClientPrefs.create(user=self.user, client_name="tests")
def test_star(self):
self._make_request("star", error=10)
@ -61,17 +61,12 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("star", {"id": str(self.artistid)}, error=70)
self._make_request("star", {"id": str(self.albumid)}, error=70)
self._make_request("star", {"id": str(self.trackid)}, skip_post=True)
with db_session:
prefs = ClientPrefs.get(
lambda p: p.user.name == "alice" and p.client_name == "tests"
)
self.assertIn(
"starred", Track[self.trackid].as_subsonic_child(self.user, prefs)
"starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
self._make_request("star", {"id": str(self.trackid)}, error=0)
self._make_request("star", {"id": str(self.folderid)}, skip_post=True)
with db_session:
self.assertIn("starred", Folder[self.folderid].as_subsonic_child(self.user))
self._make_request("star", {"id": str(self.folderid)}, error=0)
@ -79,7 +74,6 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("star", {"albumId": str(self.artistid)}, error=70)
self._make_request("star", {"albumId": str(self.trackid)}, error=70)
self._make_request("star", {"albumId": str(self.albumid)}, skip_post=True)
with db_session:
self.assertIn("starred", Album[self.albumid].as_subsonic_album(self.user))
self._make_request("star", {"albumId": str(self.albumid)}, error=0)
@ -87,10 +81,7 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("star", {"artistId": str(self.albumid)}, error=70)
self._make_request("star", {"artistId": str(self.trackid)}, error=70)
self._make_request("star", {"artistId": str(self.artistid)}, skip_post=True)
with db_session:
self.assertIn(
"starred", Artist[self.artistid].as_subsonic_artist(self.user)
)
self.assertIn("starred", Artist[self.artistid].as_subsonic_artist(self.user))
self._make_request("star", {"artistId": str(self.artistid)}, error=0)
def test_unstar(self):
@ -110,31 +101,18 @@ class AnnotationTestCase(ApiTestBase):
self._make_request("unstar", {"artistId": "unknown"}, error=0)
self._make_request("unstar", {"id": str(self.trackid)}, skip_post=True)
with db_session:
prefs = ClientPrefs.get(
lambda p: p.user.name == "alice" and p.client_name == "tests"
)
self.assertNotIn(
"starred", Track[self.trackid].as_subsonic_child(self.user, prefs)
"starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
self._make_request("unstar", {"id": str(self.folderid)}, skip_post=True)
with db_session:
self.assertNotIn(
"starred", Folder[self.folderid].as_subsonic_child(self.user)
)
self.assertNotIn("starred", Folder[self.folderid].as_subsonic_child(self.user))
self._make_request("unstar", {"albumId": str(self.albumid)}, skip_post=True)
with db_session:
self.assertNotIn(
"starred", Album[self.albumid].as_subsonic_album(self.user)
)
self.assertNotIn("starred", Album[self.albumid].as_subsonic_album(self.user))
self._make_request("unstar", {"artistId": str(self.artistid)}, skip_post=True)
with db_session:
self.assertNotIn(
"starred", Artist[self.artistid].as_subsonic_artist(self.user)
)
self.assertNotIn("starred", Artist[self.artistid].as_subsonic_artist(self.user))
def test_set_rating(self):
self._make_request("setRating", error=10)
@ -158,24 +136,16 @@ class AnnotationTestCase(ApiTestBase):
)
self._make_request("setRating", {"id": str(self.trackid), "rating": 6}, error=0)
with db_session:
prefs = ClientPrefs.get(
lambda p: p.user.name == "alice" and p.client_name == "tests"
)
self.assertNotIn(
"userRating", Track[self.trackid].as_subsonic_child(self.user, prefs)
"userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
for i in range(1, 6):
self._make_request(
"setRating", {"id": str(self.trackid), "rating": i}, skip_post=True
)
with db_session:
prefs = ClientPrefs.get(
lambda p: p.user.name == "alice" and p.client_name == "tests"
)
self.assertEqual(
Track[self.trackid].as_subsonic_child(self.user, prefs)[
Track[self.trackid].as_subsonic_child(self.user, self.prefs)[
"userRating"
],
i,
@ -184,12 +154,8 @@ class AnnotationTestCase(ApiTestBase):
self._make_request(
"setRating", {"id": str(self.trackid), "rating": 0}, skip_post=True
)
with db_session:
prefs = ClientPrefs.get(
lambda p: p.user.name == "alice" and p.client_name == "tests"
)
self.assertNotIn(
"userRating", Track[self.trackid].as_subsonic_child(self.user, prefs)
"userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs)
)
self.assertNotIn(
@ -199,14 +165,12 @@ class AnnotationTestCase(ApiTestBase):
self._make_request(
"setRating", {"id": str(self.folderid), "rating": i}, skip_post=True
)
with db_session:
self.assertEqual(
Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i
)
self._make_request(
"setRating", {"id": str(self.folderid), "rating": 0}, skip_post=True
)
with db_session:
self.assertNotIn(
"userRating", Folder[self.folderid].as_subsonic_child(self.user)
)