From 8725500ec7a022e25b87956bebae86313e3b9d74 Mon Sep 17 00:00:00 2001 From: spl0k Date: Mon, 24 Mar 2014 21:53:19 +0100 Subject: [PATCH] Converting api:browsing --- api/browse.py | 16 ++++++++-------- db.py | 32 +++++++++++++++++--------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/api/browse.py b/api/browse.py index c5ffd5c..bb579c4 100644 --- a/api/browse.py +++ b/api/browse.py @@ -19,7 +19,7 @@ # along with this program. If not, see . from flask import request -from web import app +from web import app, store from db import Folder, Artist, Album, Track from . import get_entity import uuid, time, string @@ -31,7 +31,7 @@ def list_folders(): 'musicFolder': [ { 'id': str(f.id), 'name': f.name - } for f in Folder.query.filter(Folder.root == True).order_by(Folder.name).all() ] + } for f in store.find(Folder, Folder.root == True).order_by(Folder.name) ] } }) @@ -46,25 +46,25 @@ def list_indexes(): return request.error_formatter(0, 'Invalid timestamp') if musicFolderId is None: - folder = Folder.query.filter(Folder.root == True).all() + folder = store.find(Folder, Folder.root == True) else: try: mfid = uuid.UUID(musicFolderId) except: return request.error_formatter(0, 'Invalid id') - folder = Folder.query.get(mfid) + folder = store.get(Folder, mfid) - if not folder or (type(folder) is not list and not folder.root): + if not folder or (type(folder) is Folder and not folder.root): return request.error_formatter(70, 'Folder not found') - last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan + last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is not Folder else folder.last_scan if (not ifModifiedSince is None) and last_modif < ifModifiedSince: return request.formatter({ 'indexes': { 'lastModified': last_modif * 1000 } }) # The XSD lies, we don't return artists but a directory structure - if type(folder) is list: + if type(folder) is not Folder: artists = [] childs = [] for f in folder: @@ -121,7 +121,7 @@ def show_directory(): def list_artists(): # According to the API page, there are no parameters? indexes = {} - for artist in Artist.query.all(): + for artist in store.find(Artist): index = artist.name[0].upper() if artist.name else '?' if index in map(str, xrange(10)): index = '#' diff --git a/db.py b/db.py index b2914ca..3c428bc 100644 --- a/db.py +++ b/db.py @@ -26,7 +26,7 @@ from storm.variables import Variable import uuid, datetime, time import os.path - + def now(): return datetime.datetime.now().replace(microsecond = 0) @@ -71,14 +71,14 @@ class Folder(object): if self.has_cover_art: info['coverArt'] = str(self.id) - starred = StarredFolder.query.get((user.id, self.id)) + starred = Store.of(self).get(StarredFolder, (user.id, self.id)) if starred: info['starred'] = starred.date.isoformat() - rating = RatingFolder.query.get((user.id, self.id)) + rating = Store.of(self).get(RatingFolder, (user.id, self.id)) if rating: info['userRating'] = rating.rating - avgRating = RatingFolder.query.filter(RatingFolder.rated_id == self.id).value(func.avg(RatingFolder.rating)) + avgRating = Store.of(self).find(RatingFolder, RatingFolder.rated_id == self.id).avg(RatingFolder.rating) if avgRating: info['averageRating'] = avgRating @@ -95,10 +95,10 @@ class Artist(object): 'id': str(self.id), 'name': self.name, # coverArt - 'albumCount': len(self.albums) + 'albumCount': self.albums.count() } - starred = StarredArtist.query.get((user.id, self.id)) + starred = Store.of(self).get(StarredArtist, (user.id, self.id)) if starred: info['starred'] = starred.date.isoformat() @@ -118,14 +118,16 @@ class Album(object): 'name': self.name, 'artist': self.artist.name, 'artistId': str(self.artist_id), - 'songCount': len(self.tracks), - 'duration': sum(map(lambda t: t.duration, self.tracks)), - 'created': min(map(lambda t: t.created, self.tracks)).isoformat() + 'songCount': self.tracks.count(), + 'duration': sum(self.tracks.values(Track.duration)), + 'created': min(self.tracks.values(Track.created)).isoformat() } - if self.tracks[0].folder.has_cover_art: - info['coverArt'] = str(self.tracks[0].folder_id) - starred = StarredAlbum.query.get((user.id, self.id)) + track_with_cover = self.tracks.find(Track.folder_id == Folder.id, Folder.has_cover_art).any() + if track_with_cover: + info['coverArt'] = str(track_with_cover.folder_id) + + starred = Store.of(self).get(StarredAlbum, (user.id, self.id)) if starred: info['starred'] = starred.date.isoformat() @@ -194,14 +196,14 @@ class Track(object): if self.folder.has_cover_art: info['coverArt'] = str(self.folder_id) - starred = StarredTrack.query.get((user.id, self.id)) + starred = Store.of(self).get(StarredTrack, (user.id, self.id)) if starred: info['starred'] = starred.date.isoformat() - rating = RatingTrack.query.get((user.id, self.id)) + rating = Store.of(self).get(RatingTrack, (user.id, self.id)) if rating: info['userRating'] = rating.rating - avgRating = RatingTrack.query.filter(RatingTrack.rated_id == self.id).value(func.avg(RatingTrack.rating)) + avgRating = Store.of(self).find(RatingTrack, RatingTrack.rated_id == self.id).avg(RatingTrack.rating) if avgRating: info['averageRating'] = avgRating