# HG changeset patch
# User terom
# Date 1194548945 0
# Node ID 156cdfffef8ef4da223f71507d98e5a0eeda615d
# Parent d46ab092d2b20964203b56226724e26a10bbcdf5
fix shorturls to dirs, and a new feature: pagination for index pages
diff -r d46ab092d2b2 -r 156cdfffef8e degal.py
--- 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 = "
" # - Showing Images %d - %d of %d
" % (cur_page*IMAGE_COUNT, cur_page*IMAGE_COUNT+len(page), image_count)
+
+ if cur_page > 0 :
+ pagination += '- « Prev
' % (self._page_fname(cur_page - 1))
+ else :
+ pagination += '- « Prev
'
+
+ for x in xrange(0, len(pages)) :
+ if x == cur_page :
+ pagination += '- %s
' % (x + 1)
+ else :
+ pagination += '- %s
' % (self._page_fname(x), x+1)
+
+ if cur_page < len(pages) - 1 :
+ pagination += '- Next »
' % (self._page_fname(cur_page + 1))
+ else :
+ pagination += '- Next »
'
+
+ pagination += "
"
+ 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 of this image's thumbnail. Path relative to directory we are in
"""
- return link(self.html_name, "" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
+ return link(self.html_name, "" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
def previewImgTag (self) :
"""
a of this image's preview. Path relative to directory we are in
"""
- return link(self.name, "" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
+ return link(self.name, "" % (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
diff -r d46ab092d2b2 -r 156cdfffef8e shorturl.cgi
--- a/shorturl.cgi Thu Nov 08 17:53:03 2007 +0000
+++ b/shorturl.cgi Thu Nov 08 19:09:05 2007 +0000
@@ -12,6 +12,8 @@
try :
type, dirpath, fname = db[key]
+ if type == 'dir' : fname = 'index'
+
path = os.path.join(dirpath, fname) + '.html'
finally :
db.close()
diff -r d46ab092d2b2 -r 156cdfffef8e style.css
--- a/style.css Thu Nov 08 17:53:03 2007 +0000
+++ b/style.css Thu Nov 08 19:09:05 2007 +0000
@@ -55,3 +55,36 @@
}
+div.paginate {
+ height: 50px;
+ width: 100%;
+ text-align: center;
+}
+
+div.paginate ul {
+ margin: 0px
+ padding: 0px;
+ line-height: 30px;
+ white-space: nowrap;
+}
+
+div.paginate li {
+ list-style-type: none;
+ display: inline;
+}
+
+div.paginate li *,
+div.paginate li strong,
+div.paginate li span {
+ padding: 7px 10px;
+}
+
+div.paginate li span {
+ color: #444444;
+}
+
+div.paginate li a:hover {
+ text-decoration: none;
+ background-color: #666666;
+}
+
diff -r d46ab092d2b2 -r 156cdfffef8e templates/gallery.html
--- a/templates/gallery.html Thu Nov 08 17:53:03 2007 +0000
+++ b/templates/gallery.html Thu Nov 08 19:09:05 2007 +0000
@@ -11,9 +11,11 @@
+
+