diff data/plugin/parser/text_x_arnica.py @ 569:df77d4563af1

Added album-overview functionality.
author sudo_dirk<d.alders@arcor.de>
date Sun, 05 Feb 2012 13:23:28 +0100
parents 8893fdfafd75
children bf8bae241ec9
line wrap: on
line diff
--- a/data/plugin/parser/text_x_arnica.py	Thu Jan 12 14:07:07 2012 +0100
+++ b/data/plugin/parser/text_x_arnica.py	Sun Feb 05 13:23:28 2012 +0100
@@ -11,11 +11,13 @@
     Based on Gallery2 by ReimarBauer 2005-2008, ThomasWaldmann 2005, FlorianFesti 2006
 
     @copyright: 2008-2010 by MoinMoin:ReimarBauer
+    @copyright: 2012 by MoinMoin:Dirk Alders
     @license: GNU GPL, see COPYING for details.
 """
 
 import os, re
 from random import randint
+from MoinMoin import search
 from MoinMoin import wikiutil
 from MoinMoin.action import AttachFile
 from MoinMoin.packages import packLine
@@ -31,11 +33,13 @@
                     reverse_sort=False,
                     only_items=False, template_itemlist=False,
                     album=False, album_title=unicode, album_image=u'', album_link_page=False,
+                    album_overview=False, album_short_title=False,
                     renew=False,
                     thumbnail_width=128,
                     webnail_width=640):
     """ dummy function to initialize all default parameters for arnica. The parameters are checked for wrong input.
     @param target_page: page to read attachments from. empty pagename is current page.
+                        for album_overview, target_page is now a string for title search. alternatively it is possible to use regex:... or category:... (e.g. regex:title:.* for all pages)
     @param columns: number of columns for thumbnails, default is 4. 0 means no linebreak
     @param file_regex: regex for selecting images
     @param image_for_webnail if set then the image is shown instead of the webnail
@@ -53,6 +57,8 @@
     @param album_title: default is pagename of the images for the album.
     @param album_image: image to show on album default is the first image
     @param album_link_page: link to page instead of slideshow
+    @param album_overview: if set, a thumbnail overview will be created (see also target_page)
+    @param album_short_title: reduces the album_title that way that it fits in one line
     @param renew: if set then all selected thumbnails_* and webnails_* are removed and will be recreated
     @param thumbnail_width: default width of thumbnail is 128px
     @param webnail_width: default width of webnail is 640px
@@ -83,6 +89,7 @@
 
         args = kw.get('format_args', '')
         self.init_settings = False
+        self.top_image_sorted = None
         # we use a macro definition to initialize the default init parameters
         # if a user enters a wrong parameter the failure is shown by the exception
         try:
@@ -99,6 +106,12 @@
             self.Image = wikiutil.importWikiPlugin(self.request.cfg, "macro", "Image", function="Image")
             self.breakable_word_length = self.thumbnail_width / 8
 
+    def shortenName(self, name, length, prestring='...'):
+        if len(name) > length:
+            return prestring + name[-length + len(prestring):]
+        else:
+            return name
+
     def html_tools_restricted(self, this_target):
         """ shows restricted tools
             @param this_target: image
@@ -223,8 +236,11 @@
         """
         title = ""
         if self.album and self.show_album_title:
-            title = wikiutil.make_breakable(self.album_title or self.pagename, self.breakable_word_length)
-            title = '<div class="title">%(n)d images (%(album_title)s)</div>' % {"n": len(self.arnica_image),
+            if self.album_short_title:
+                title = self.shortenName(self.album_title or self.pagename, self.breakable_word_length)
+            else:
+                title = wikiutil.make_breakable(self.album_title or self.pagename, self.breakable_word_length)
+            title = '<div class="title">%(n)d images<br>(%(album_title)s)</div>' % {"n": len(self.arnica_image),
                                                         "album_title": title}
         html = """
 <div class="thumbnail" style="width:%(width)spx">
@@ -318,13 +334,13 @@
                     counter += 1
         return image_alias
 
-    def select_files(self, formatter):
+    def select_files(self, formatter, target_page):
         """ select files """
         # we need to take the page_name from the formatter.page otherwise
         # include does not work
         self.pagename = formatter.page.page_name
-        if self.target_page and Page(self.request, self.target_page).exists() and self.request.user.may.read(self.target_page):
-            self.pagename = self.target_page
+        if target_page and Page(self.request, target_page).exists() and self.request.user.may.read(target_page):
+            self.pagename = target_page
 
         image_alias = self.get_image_alias()
         if self.only_items:
@@ -342,19 +358,53 @@
         return all_files
 
     def render(self, formatter):
+        if self.album and self.album_overview:
+            # find pages
+            if (self.target_page or self.pagename).split(":")[0] in ["regex", "category"]:
+                search_term = self.target_page or self.pagename
+            else:
+                search_term = u'regex:title:^%s/' % (self.target_page or self.pagename)
+            search_result = search.searchPages(self.request, search_term)
+            target_pages = set(title.page_name for title in search_result.hits)
+            # create album overview
+            albums = []
+            for target_page in target_pages:
+                self.arnica_image.clear()  # remove previous images
+                html = self.render_thumbnails(formatter, target_page)
+                if self.arnica_image:  # skip empty albums
+                    if self.sort_by == 'date':
+                        albums.append([self.Image(self.request, target_page + '/' + self.top_image_sorted).ctime, html])
+                    else:
+                        albums.append([self.top_image_sorted, html])
+            albums.sort()
+            if self.reverse_sort:
+                albums.reverse()
+            col_count = 1
+            cols = min([self.columns, len(albums)])
+            result = []
+            for entry in albums:
+                result.append(''.join(entry[1]))
+                if col_count == cols and self.columns != 0:
+                    col_count = 0
+                    result.append('<br class="clearboth">')
+                col_count += 1
+            result.append('<br class="clearboth">')
+            return ''.join(result)
+        else:
+            return self.render_thumbnails(formatter, self.target_page)
+
+    def render_thumbnails(self, formatter, target_page):
         """ renders thumbnails """
         _ = self._
         # checks if initializing of all attributes in __init__ was done
         if not self.init_settings:
             return
-        if self.target_page and (not Page(self.request, self.target_page).exists() or not self.request.user.may.read(self.target_page)):
-            text = _("""Page '%(new_pagename)s' does not exist or you don't have enough rights.""") % {"new_pagename": self.target_page}
-            self.request.write(self.formatter.text(text))
-            return
-        if not self.arnica_image and not self.select_files(formatter):
+        if target_page and (not Page(self.request, target_page).exists() or not self.request.user.may.read(target_page)):
+            text = _("""Page '%(new_pagename)s' does not exist or you don't have enough rights.""") % {"new_pagename": target_page}
+            return self.formatter.text(text)
+        if not self.arnica_image and not self.select_files(formatter, target_page):
             text = _("No matching image file found!")
-            self.request.write(self.formatter.text(text))
-            return
+            return self.formatter.text(text)
         if self.template_itemlist:
             self.request.write(self.formatter.div(1, css_class="text"))
             text = _("""\
@@ -423,9 +473,12 @@
         if self.reverse_sort:
             image_names.reverse()
 
+        # store first in album
+        self.top_image_sorted = image_names[0]
+
         if self.album:
             cols = 1
-            album_image = self.album_image or self.arnica_image.keys()[0] #self.high_resolution_image[0]
+            album_image = self.album_image or self.top_image_sorted  # self.high_resolution_image[0]
             if not album_image in self.arnica_image.keys():
                 html = self.formatter.text(_("""You can't use as album image:
 "%(album_image)s" because it does not exist or it is not listed
@@ -442,7 +495,9 @@
                     result.append('<br class="clearboth">')
                 col_count += 1
 
-        result.append('<br class="clearboth">')
+        if not self.album or not self.album_overview:
+            # no new line, if album and album_overview
+            result.append('<br class="clearboth">')
         result.insert(0, self.formatter.div(1, css_class="arnica"))
         result.append(self.formatter.div(0))
         return ''.join(result)