diff --git a/test_input/content/test_gallery/3ddrucker.jpg b/test_input/content/test_gallery/3ddrucker.jpg new file mode 100644 index 0000000..eda95a3 Binary files /dev/null and b/test_input/content/test_gallery/3ddrucker.jpg differ diff --git a/test_input/content/test_gallery/description.md b/test_input/content/test_gallery/description.md new file mode 100644 index 0000000..81bce18 --- /dev/null +++ b/test_input/content/test_gallery/description.md @@ -0,0 +1,5 @@ +28.1.2016 +14.2.2016 + +This is a test gallery description. +Awesome shit an stuff ! diff --git a/test_input/content/test_gallery/firecake.jpg b/test_input/content/test_gallery/firecake.jpg new file mode 100644 index 0000000..0eb90dd Binary files /dev/null and b/test_input/content/test_gallery/firecake.jpg differ diff --git a/test_input/content/test_gallery/fullmatrix.jpg b/test_input/content/test_gallery/fullmatrix.jpg new file mode 100644 index 0000000..38f7d21 Binary files /dev/null and b/test_input/content/test_gallery/fullmatrix.jpg differ diff --git a/test_input/content/test_gallery/ledcube.jpg b/test_input/content/test_gallery/ledcube.jpg new file mode 100644 index 0000000..38cb323 Binary files /dev/null and b/test_input/content/test_gallery/ledcube.jpg differ diff --git a/test_input/content/test_gallery/nixie.jpg b/test_input/content/test_gallery/nixie.jpg new file mode 100644 index 0000000..4bb87ed Binary files /dev/null and b/test_input/content/test_gallery/nixie.jpg differ diff --git a/test_input/templates/gallery.html b/test_input/templates/gallery.html new file mode 100644 index 0000000..1b548dd --- /dev/null +++ b/test_input/templates/gallery.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +

{{page_title}}

+ + {% for image in images %} + + {% endfor %} + +{% endblock %} diff --git a/test_input/testblog.py b/test_input/testblog.py index 64b3ea2..b88113a 100644 --- a/test_input/testblog.py +++ b/test_input/testblog.py @@ -7,6 +7,7 @@ from verdandi.verdandi import Verdandi from verdandi.modules.page import Page from verdandi.modules.commonassets import CommonAssets from verdandi.modules.newsfeed import NewsFeed +from verdandi.modules.gallery import Gallery class TestPage1(Page): title = "A cool new Page" @@ -41,13 +42,20 @@ class News(NewsFeed): menu_title = "News" menu_label = "news" +class TestGallery(Gallery): + title = "A Test Gallery" + url = "testgallery.html" + gallery_directory = "test_gallery" + gallery_images_url = "img/test_gallery" + class TestBlog(Verdandi): modules = [TestPage1(), TestPage2(), TestPage3(), Assets(), - News()] + News(), + TestGallery()] testblog = TestBlog() diff --git a/verdandi/mixins/templatemixin.py b/verdandi/mixins/templatemixin.py index f3b505c..53d32c1 100644 --- a/verdandi/mixins/templatemixin.py +++ b/verdandi/mixins/templatemixin.py @@ -29,6 +29,8 @@ class TemplateMixin(object): if not os.path.isdir(out_dir): os.mkdir(out_dir) + print "Rendering %s" % out_path + out_file = open(out_path, "wb") out_file.write(result) out_file.close() diff --git a/verdandi/modules/gallery.py b/verdandi/modules/gallery.py new file mode 100644 index 0000000..6fa3667 --- /dev/null +++ b/verdandi/modules/gallery.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python2 +import os +from datetime import datetime +from PIL import Image + +import markdown + +from verdandi.mixins.templatemixin import TemplateMixin +from verdandi.mixins.menuitemmixin import MenuItemMixin +from verdandi.mixins.assetsmixin import AssetsMixin +from verdandi.mixins.newsitemmixin import NewsItemMixin +from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS + +class Gallery(MenuItemMixin, NewsItemMixin, TemplateMixin, AssetsMixin): + title = 'Gallery Title' + gallery_description_file = 'description.md' + gallery_directory = 'gallery' + gallery_images_url = 'img/gallery' + gallery_thumbnail_size = 300 + gallery_thumbnail_quality = 90 + gallery_thumbnail_prefix = 'thumb_' + + menu_title = 'Gallery' + menu_label = 'gallery' + + news_item_len = 10 + + template = "gallery.html" + + content_directory = CONTENT_DIRECTORY + markdown_extensions = MARKDOWN_EXTENSIONS + + images = [] + + def process_message(self, message): + if message == None: + self.full_gallery_path = os.path.join(self.content_directory, self.gallery_directory) + description_path = os.path.join(self.full_gallery_path, self.gallery_description_file) + self.description = self.read_content_file(description_path) + + files = os.listdir(self.full_gallery_path) + self.images = filter(lambda image: os.path.splitext(image)[1] == '.jpg', files) + + other_messages = super(Gallery, self).process_message(message) + + return other_messages + + + def get_assets(self): + assets = super(Gallery, self).get_assets() + + for image in self.images: + src_image = os.path.join(self.gallery_directory, image) + dst_image = os.path.join(self.gallery_images_url, image) + assets += [(src_image, dst_image)] + + return assets + + + def get_news_item(self): + lines = self.description['content'].split('\n') + elipsized_description = '\n'.join(lines[0:self.news_item_len]) + + elipsized_description += '\n\n View the [images](%s) ...' % self.url + + item = { + 'title': self.title, + 'content': elipsized_description, + 'creation_time': self.description['creation_time'], + 'edit_time': self.description['edit_time'], + 'url' : self.url + } + + return item + + + def get_context(self): + context = super(Gallery,self).get_context() + context['page_title'] = self.title + + markdown_converter = markdown.Markdown(extensions = self.markdown_extensions) + + context['content_creation_time'] = self.description['creation_time'] + context['content_edit_time'] = self.description['edit_time'] + + markdown_source = self.description['content'] + context['description'] = markdown_converter.convert(markdown_source) + + context['images'] = [] + + for image in self.images: + image_url = os.path.join(self.gallery_images_url, image) + thumb_url = os.path.join(self.gallery_images_url, self.gallery_thumbnail_prefix + image) + context['images'] += [{'image' : image_url, 'thumb' : thumb_url}] + + return context + + + def calulate_thumb_dimesions(self, input_dimensions): + input_width, input_height = input_dimensions + + thumb_width = 0 + thumb_height = 0 + + input_width = input_width * 1.0 + input_height = input_height * 1.0 + + if input_width > input_height: + thumb_width = self.gallery_thumbnail_size + thumb_height = self.gallery_thumbnail_size / input_width * input_height + else: + thumb_width = self.gallery_thumbnail_size / input_height * input_width + thumb_height = self.gallery_thumbnail_size + + return (int(thumb_width), int(thumb_height)) + + + def render_files(self, context, output_directory, jinja_env): + + dst_dir = os.path.join(output_directory, self.gallery_images_url) + + for image in self.images: + src_path = os.path.join(self.full_gallery_path, image) + dst_path = os.path.join(dst_dir, self.gallery_thumbnail_prefix + image) + + print "Creating thumbnail: %s" % dst_path + + image = Image.open(src_path) + thumbnail = image.resize(self.calulate_thumb_dimesions(image.size), Image.LANCZOS) + thumbnail.save(dst_path, quality=self.gallery_thumbnail_quality) + + super(Gallery, self).render_files(context, output_directory, jinja_env)