--- a/degal.py Thu Nov 08 17:53:03 2007 +0000
+++ b/degal.py Thu Nov 08 19:09:05 2007 +0000
@@ -106,6 +106,9 @@
DEFAULT_TITLE = 'Image gallery'
+# how many image/page
+IMAGE_COUNT = 50
+
def isImage (fname) :
"""
Is the given filename likely to be an image file?
@@ -338,7 +341,7 @@
"""
Metadata for shorturls2.db
"""
- return 'dir', self.path, ''
+ return 'dir', self.path, 'index'
def linkTag (self) :
"""
@@ -372,6 +375,14 @@
c = len(self.path.split('/')) - 1
return os.path.join(*((['..']*c) + list(fnames)))
+
+ def _page_fname (self, page) :
+ assert page >= 0
+
+ if page > 0 :
+ return 'index_%d.html' % page
+ else :
+ return 'index.html'
def render (self) :
"""
@@ -402,18 +413,58 @@
directories = ''
render.info("Rendering %s", self.path)
+
+ # paginate!
+ images = self.sorted_images
+ image_count = len(images)
+ pages = []
- # render to index.html
- gallery_tpl.renderTo(self.pathFor('index.html'),
- STYLE_URL=self.inRoot('style.css'),
- BREADCRUMB=" » ".join([link(u, t) for (u, t) in self.breadcrumb()]),
- TITLE=self.title,
- DIRECTORIES=directories,
- CONTENT="".join([i.thumbImgTag() for i in self.sorted_images]),
- DESCR=self.descr,
- SHORTURL=self.inRoot('s', self.shorturl_code),
- SHORTURL_CODE=self.shorturl_code,
- )
+ while images :
+ pages.append(images[:IMAGE_COUNT])
+ images = images[IMAGE_COUNT:]
+
+ pagination_required = len(pages) > 1
+
+ if pagination_required :
+ render.info("Index split into %d pages of %d images each", len(pages), IMAGE_COUNT)
+
+ for cur_page, page in enumerate(pages) :
+ if pagination_required :
+ pagination = "<ul>" # <li>Showing Images %d - %d of %d</li>" % (cur_page*IMAGE_COUNT, cur_page*IMAGE_COUNT+len(page), image_count)
+
+ if cur_page > 0 :
+ pagination += '<li><a href="%s">« Prev</a></li>' % (self._page_fname(cur_page - 1))
+ else :
+ pagination += '<li><span>« Prev</span></li>'
+
+ for x in xrange(0, len(pages)) :
+ if x == cur_page :
+ pagination += '<li><strong>%s</strong></li>' % (x + 1)
+ else :
+ pagination += '<li><a href="%s">%s</a></li>' % (self._page_fname(x), x+1)
+
+ if cur_page < len(pages) - 1 :
+ pagination += '<li><a href="%s">Next »</a></li>' % (self._page_fname(cur_page + 1))
+ else :
+ pagination += '<li><span>Next »</span></li>'
+
+ pagination += "</ul>"
+ else :
+ pagination = ''
+
+
+ # render to index.html
+ gallery_tpl.renderTo(self.pathFor(self._page_fname(cur_page)),
+ STYLE_URL=self.inRoot('style.css'),
+ BREADCRUMB=" » ".join([link(u, t) for (u, t) in self.breadcrumb()]),
+ TITLE=self.title,
+ DIRECTORIES=directories,
+ PAGINATION=pagination,
+ CONTENT="".join([i.thumbImgTag() for i in page]),
+ DESCR=self.descr,
+ SHORTURL=self.inRoot('s', self.shorturl_code),
+ SHORTURL_CODE=self.shorturl_code,
+ )
# render images
for img in self.images.itervalues() :
@@ -520,13 +571,13 @@
"""
a <a><img /></a> of this image's thumbnail. Path relative to directory we are in
"""
- return link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
+ return link(self.html_name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
def previewImgTag (self) :
"""
a <a><img /></a> of this image's preview. Path relative to directory we are in
"""
- return link(self.name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
+ return link(self.name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
def linkTag (self) :
"""
@@ -604,7 +655,7 @@
fname - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
"""
- db = shelve.open('shorturls2', 'c')
+ db = shelve.open('shorturls2', 'c', writeback=True)
id = db.get('_id', 1)
@@ -613,6 +664,8 @@
# dict of path -> obj
paths = {}
+ index.info("Processing ShortURLs...")
+
while dirqueue :
dir = dirqueue.pop(0)
@@ -622,7 +675,7 @@
paths[dir.path] = dir
for img in dir.images.itervalues() :
- paths[img.html_path] = img
+ paths[img.path] = img
for key in db.keys() :
if key.startswith('_') :
@@ -630,7 +683,7 @@
type, dirpath, fname = db[key]
- path = os.path.join(dirpath, fname)
+ path = os.path.join(dirpath, fname).rstrip('/')
try :
paths.pop(path).shorturl_code = key