From 3422e8920a9e3110d62cb1279e8dd21d2babf2c4 Mon Sep 17 00:00:00 2001 From: Patrick Moor Date: Sat, 16 Apr 2011 03:54:55 +0000 Subject: [PATCH] allow restriction to a certain album by passing "album_id" --- domain.py | 10 ++++++++++ json_handler.py | 3 ++- picker.py | 12 ++++++++++-- user_handler.py | 3 ++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/domain.py b/domain.py index 238a633..35abba8 100644 --- a/domain.py +++ b/domain.py @@ -14,6 +14,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import re + +_ALBUM_ID_RE = re.compile(r"/albumid/([0-9]+)$") + class Album(object): def __init__(self, title, count, feed): @@ -27,6 +31,12 @@ def GetFeed(self): def GetCount(self): return self._count + def GetId(self): + m = _ALBUM_ID_RE.search(self._feed) + if not m: + raise Exception("id not found in %s" % self._feed) + return int(m.group(1)) + def __str__(self): return self._feed diff --git a/json_handler.py b/json_handler.py index dc8d75e..bb49e60 100644 --- a/json_handler.py +++ b/json_handler.py @@ -26,7 +26,8 @@ def get(self, user_name): self.response.headers["Content-Type"] = "application/json" self.response.headers["Access-Control-Allow-Origin"] = "*" size = self.request.get("size", "200u") - picture = self._picker.Pick(user_name, size) + album_id = int(self.request.get("album_id", 0)) + picture = self._picker.Pick(user_name, size, album_id) if picture: content = { "height": picture.GetHeight(), diff --git a/picker.py b/picker.py index a459609..5d63913 100644 --- a/picker.py +++ b/picker.py @@ -30,9 +30,17 @@ def _PickWeighted(self, albums): if pick < 0: return album - def Pick(self, user_name, size): + def _PickById(self, albums, album_id): + for album in albums: + if album.GetId() == album_id: + return album + + def Pick(self, user_name, size, album_id=None): albums = self._album_repository.GetAlbums(user_name) - album = self._PickWeighted(albums) + if album_id: + album = self._PickById(albums, album_id) + else: + album = self._PickWeighted(albums) if album: pictures = self._album_repository.GetPictures(album, size) if pictures: diff --git a/user_handler.py b/user_handler.py index ba9a2c7..e67a872 100644 --- a/user_handler.py +++ b/user_handler.py @@ -24,7 +24,8 @@ def __init__(self, picker): def get(self, user_name): size = self.request.get("size", "200u") link = bool(int(self.request.get("link", 1))) - picture = self._picker.Pick(user_name, size) + album_id = int(self.request.get("album_id", 0)) + picture = self._picker.Pick(user_name, size, album_id) if picture: html = """""" % ( picture.GetThumbnailUrl(),