fix shorturls to dirs, and a new feature: pagination for index pages
authorterom
Thu, 08 Nov 2007 19:09:05 +0000
changeset 5 156cdfffef8e
parent 4 d46ab092d2b2
child 6 d9d1f8e5f384
fix shorturls to dirs, and a new feature: pagination for index pages
degal.py
shorturl.cgi
style.css
templates/gallery.html
--- 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">&laquo; Prev</a></li>' % (self._page_fname(cur_page - 1))
+                    else :
+                        pagination += '<li><span>&laquo; 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 &raquo;</a></li>' % (self._page_fname(cur_page + 1))
+                    else :
+                        pagination += '<li><span>Next &raquo;</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=" &raquo; ".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
--- 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()
--- 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;
+}
+
--- 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 @@
     <div id="breadcrumb"><!-- BREADCRUMB --></div>
     <h1><!-- TITLE --></h1>
     <div id="dirs"><!-- DIRECTORIES --></div>
+    <div class="paginate"><!-- PAGINATION --></div>
     <div id="thumbnails">
       <!-- CONTENT -->
     </div>
+    <div class="paginate"><!-- PAGINATION --></div>
     <p id="description"><!-- DESCR --></p>
     <div id="info">
         <p>ShortURL: <a href="<!-- SHORTURL -->" rel="nofollow"><!-- SHORTURL_CODE --></a></p>