Skip to content

Commit

Permalink
allow restriction to a certain album by passing "album_id"
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoor committed Apr 16, 2011
1 parent 8b69dac commit 3422e89
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions domain.py
Expand Up @@ -14,6 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import re

_ALBUM_ID_RE = re.compile(r"/albumid/([0-9]+)$")

class Album(object):

def __init__(self, title, count, feed):
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion json_handler.py
Expand Up @@ -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(),
Expand Down
12 changes: 10 additions & 2 deletions picker.py
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion user_handler.py
Expand Up @@ -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 = """<img src="%s" width="%d" height="%d" id="rndpic-img"/>""" % (
picture.GetThumbnailUrl(),
Expand Down

0 comments on commit 3422e89

Please sign in to comment.