degal.py
changeset 5 156cdfffef8e
parent 1 740133ab6353
child 6 d9d1f8e5f384
equal deleted inserted replaced
4:d46ab092d2b2 5:156cdfffef8e
   104 THUMB_GEOM = (160, 120)
   104 THUMB_GEOM = (160, 120)
   105 PREVIEW_GEOM = (640, 480)
   105 PREVIEW_GEOM = (640, 480)
   106 
   106 
   107 DEFAULT_TITLE = 'Image gallery'
   107 DEFAULT_TITLE = 'Image gallery'
   108 
   108 
       
   109 # how many image/page
       
   110 IMAGE_COUNT = 50
       
   111 
   109 def isImage (fname) :
   112 def isImage (fname) :
   110     """
   113     """
   111         Is the given filename likely to be an image file?
   114         Is the given filename likely to be an image file?
   112     """
   115     """
   113 
   116 
   336 
   339 
   337     def getObjInfo (self) :
   340     def getObjInfo (self) :
   338         """
   341         """
   339             Metadata for shorturls2.db
   342             Metadata for shorturls2.db
   340         """
   343         """
   341         return 'dir', self.path, ''
   344         return 'dir', self.path, 'index'
   342 
   345 
   343     def linkTag (self) :
   346     def linkTag (self) :
   344         """
   347         """
   345             A text-link to this dir
   348             A text-link to this dir
   346         """
   349         """
   370         """
   373         """
   371 
   374 
   372         c = len(self.path.split('/')) - 1
   375         c = len(self.path.split('/')) - 1
   373 
   376 
   374         return os.path.join(*((['..']*c) + list(fnames)))
   377         return os.path.join(*((['..']*c) + list(fnames)))
       
   378     
       
   379     def _page_fname (self, page) :
       
   380         assert page >= 0
       
   381 
       
   382         if page > 0 :
       
   383             return  'index_%d.html' % page
       
   384         else :
       
   385             return 'index.html'
   375 
   386 
   376     def render (self) :
   387     def render (self) :
   377         """
   388         """
   378             Render the index.html, Images, and recurse into subdirs
   389             Render the index.html, Images, and recurse into subdirs
   379         """
   390         """
   400                 directories = "<ul>\n\t<li>%s</li>\n</ul>" % "</li>\n\t<li>".join(subdir_linkTags)
   411                 directories = "<ul>\n\t<li>%s</li>\n</ul>" % "</li>\n\t<li>".join(subdir_linkTags)
   401             else :
   412             else :
   402                 directories = ''
   413                 directories = ''
   403 
   414 
   404             render.info("Rendering %s", self.path)
   415             render.info("Rendering %s", self.path)
   405             
   416 
   406             # render to index.html
   417             # paginate!
   407             gallery_tpl.renderTo(self.pathFor('index.html'), 
   418             images = self.sorted_images
   408                 STYLE_URL=self.inRoot('style.css'),
   419             image_count = len(images)
   409                 BREADCRUMB=" &raquo; ".join([link(u, t) for (u, t) in self.breadcrumb()]),
   420             pages = []
   410                 TITLE=self.title,
   421             
   411                 DIRECTORIES=directories,
   422             while images :
   412                 CONTENT="".join([i.thumbImgTag() for i in self.sorted_images]),
   423                 pages.append(images[:IMAGE_COUNT])
   413                 DESCR=self.descr,
   424                 images = images[IMAGE_COUNT:]
   414                 SHORTURL=self.inRoot('s', self.shorturl_code),
   425 
   415                 SHORTURL_CODE=self.shorturl_code,
   426             pagination_required = len(pages) > 1
   416             )
   427 
       
   428             if pagination_required :
       
   429                 render.info("Index split into %d pages of %d images each", len(pages), IMAGE_COUNT)
       
   430             
       
   431             for cur_page, page in enumerate(pages) :
       
   432                 if pagination_required :
       
   433                     pagination = "<ul>" # <li>Showing Images %d - %d of %d</li>" % (cur_page*IMAGE_COUNT, cur_page*IMAGE_COUNT+len(page), image_count)
       
   434                     
       
   435                     if cur_page > 0 :
       
   436                         pagination += '<li><a href="%s">&laquo; Prev</a></li>' % (self._page_fname(cur_page - 1))
       
   437                     else :
       
   438                         pagination += '<li><span>&laquo; Prev</span></li>'
       
   439 
       
   440                     for x in xrange(0, len(pages)) :
       
   441                         if x == cur_page :
       
   442                             pagination += '<li><strong>%s</strong></li>' % (x + 1)
       
   443                         else :
       
   444                             pagination += '<li><a href="%s">%s</a></li>' % (self._page_fname(x), x+1)
       
   445 
       
   446                     if cur_page < len(pages) - 1 :
       
   447                         pagination += '<li><a href="%s">Next &raquo;</a></li>' % (self._page_fname(cur_page + 1))
       
   448                     else :
       
   449                         pagination += '<li><span>Next &raquo;</span></li>'
       
   450 
       
   451                     pagination += "</ul>"
       
   452                 else :
       
   453                     pagination = ''
       
   454 
       
   455 
       
   456                 # render to index.html
       
   457                 gallery_tpl.renderTo(self.pathFor(self._page_fname(cur_page)), 
       
   458                     STYLE_URL=self.inRoot('style.css'),
       
   459                     BREADCRUMB=" &raquo; ".join([link(u, t) for (u, t) in self.breadcrumb()]),
       
   460                     TITLE=self.title,
       
   461                     DIRECTORIES=directories,
       
   462                     PAGINATION=pagination,
       
   463                     CONTENT="".join([i.thumbImgTag() for i in page]),
       
   464                     DESCR=self.descr,
       
   465                     SHORTURL=self.inRoot('s', self.shorturl_code),
       
   466                     SHORTURL_CODE=self.shorturl_code,
       
   467                 )
   417         
   468         
   418         # render images
   469         # render images
   419         for img in self.images.itervalues() :
   470         for img in self.images.itervalues() :
   420             img.render()
   471             img.render()
   421         
   472         
   518 
   569 
   519     def thumbImgTag (self) :
   570     def thumbImgTag (self) :
   520         """
   571         """
   521             a <a><img /></a> of this image's thumbnail. Path relative to directory we are in
   572             a <a><img /></a> of this image's thumbnail. Path relative to directory we are in
   522         """
   573         """
   523         return link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
   574         return link(self.html_name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(THUMB_DIR, self.name), self.descr, self.title))
   524 
   575 
   525     def previewImgTag (self) :
   576     def previewImgTag (self) :
   526         """
   577         """
   527             a <a><img /></a> of this image's preview. Path relative to directory we are in
   578             a <a><img /></a> of this image's preview. Path relative to directory we are in
   528         """
   579         """
   529         return link(self.name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
   580         return link(self.name, "<img src='%s' alt='%s' title='%s' />" % (os.path.join(PREVIEW_DIR, self.name), self.descr, self.title))
   530 
   581 
   531     def linkTag (self) :
   582     def linkTag (self) :
   532         """
   583         """
   533             a <a></a> text-link to this image
   584             a <a></a> text-link to this image
   534         """
   585         """
   602         type    - one of 'img', 'dir'
   653         type    - one of 'img', 'dir'
   603         dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux'
   654         dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux'
   604         fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
   655         fname   - the filename, one of '', 'DSC9839.JPG', 'this.png', etc.
   605     """
   656     """
   606 
   657 
   607     db = shelve.open('shorturls2', 'c')
   658     db = shelve.open('shorturls2', 'c', writeback=True)
   608     
   659     
   609     id = db.get('_id', 1)
   660     id = db.get('_id', 1)
   610 
   661 
   611     dirqueue = [root]
   662     dirqueue = [root]
   612 
   663 
   613     # dict of path -> obj
   664     # dict of path -> obj
   614     paths = {}
   665     paths = {}
   615 
   666 
       
   667     index.info("Processing ShortURLs...")
       
   668 
   616     while dirqueue :
   669     while dirqueue :
   617         dir = dirqueue.pop(0)
   670         dir = dirqueue.pop(0)
   618 
   671 
   619         dirqueue.extend(dir.subdirs.itervalues())
   672         dirqueue.extend(dir.subdirs.itervalues())
   620 
   673 
   621         if dir.alive :
   674         if dir.alive :
   622             paths[dir.path] = dir
   675             paths[dir.path] = dir
   623 
   676 
   624         for img in dir.images.itervalues() :
   677         for img in dir.images.itervalues() :
   625             paths[img.html_path] = img
   678             paths[img.path] = img
   626 
   679 
   627     for key in db.keys() :
   680     for key in db.keys() :
   628         if key.startswith('_') :
   681         if key.startswith('_') :
   629             continue
   682             continue
   630 
   683 
   631         type, dirpath, fname = db[key]
   684         type, dirpath, fname = db[key]
   632         
   685         
   633         path = os.path.join(dirpath, fname)
   686         path = os.path.join(dirpath, fname).rstrip('/')
   634 
   687 
   635         try :
   688         try :
   636             paths.pop(path).shorturl_code = key
   689             paths.pop(path).shorturl_code = key
   637             index.debug("Code for `%s' is %s", path, key)
   690             index.debug("Code for `%s' is %s", path, key)
   638 
   691