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 |
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=" » ".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">« Prev</a></li>' % (self._page_fname(cur_page - 1)) |
|
437 else : |
|
438 pagination += '<li><span>« 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 »</a></li>' % (self._page_fname(cur_page + 1)) |
|
448 else : |
|
449 pagination += '<li><span>Next »</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=" » ".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 |