From 8ac22a6943b7b549f461dba253a7fa42b0029827 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 24 Sep 2011 16:50:33 +0100
Subject: [PATCH] add method and API to retrieve last uploaded songs

---
 server/djrandom/frontend/api_views.py | 11 ++++++++++-
 server/djrandom/model/mp3.py          |  5 +++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/server/djrandom/frontend/api_views.py b/server/djrandom/frontend/api_views.py
index 96f421a..893a952 100644
--- a/server/djrandom/frontend/api_views.py
+++ b/server/djrandom/frontend/api_views.py
@@ -151,7 +151,16 @@ def more_like_these_json():
 @app.route('/json/most_played', methods=['GET'])
 @require_auth
 def most_played_json():
+    n = int(request.args.get('n', 20))
     most_played = [{'sha1': sha1, 'count': count}
-                   for sha1, count in PlayLog.most_played(20)]
+                   for sha1, count in PlayLog.most_played(n)]
     return jsonify(results=most_played)
 
+
+@app.route('/json/last_uploaded', methods=['GET'])
+@require_auth
+def last_uploaded_json():
+    n = int(request.args.get('n', 20))
+    last_uploaded = [x.to_dict() for x in MP3.last_uploaded(n)]
+    return jsonify(results=last_uploaded)
+
diff --git a/server/djrandom/model/mp3.py b/server/djrandom/model/mp3.py
index 12a5b53..6a7f10c 100644
--- a/server/djrandom/model/mp3.py
+++ b/server/djrandom/model/mp3.py
@@ -36,6 +36,11 @@ class MP3(Base):
                 'size': self.size,
                 'uploaded_at': self.uploaded_at}
 
+    @classmethod
+    def last_uploaded(cls, n=10):
+        """Return the N last uploaded songs."""
+        return cls.query.filter_by(ready=True).order_by('desc uploaded_at').limit(n)
+
 
 class PlayLog(Base):
 
-- 
GitLab