changeset 315:1f605f821a68

text_x_arnica: sort_by bug fixed by moving sorting to render.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 17 Dec 2008 20:34:39 +0100
parents c1e0bb60a0c2
children 8a76def2ce62
files data/plugin/parser/text_x_arnica.py
diffstat 1 files changed, 52 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/parser/text_x_arnica.py	Sun Nov 30 13:07:40 2008 +0100
+++ b/data/plugin/parser/text_x_arnica.py	Wed Dec 17 20:34:39 2008 +0100
@@ -300,7 +300,7 @@
         for line in quotes:
             if line.startswith('[[') and line.endswith(']]'):
                 img, alias = line[2:-2].split('|', 1)
-                image_alias[img.strip()] = (self.formatter.text(alias.strip()), counter)
+                image_alias[img.strip()] = (self.formatter.text(alias.strip()), counter, img)
                 counter += 1
         return image_alias
 
@@ -311,47 +311,16 @@
         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
-        path = AttachFile.getAttachDir(self.request, self.pagename, create=1)
-        image_dict = {}
+
         image_alias = self.get_image_alias()
         if self.only_items:
-            # get the same order of files and aliastext as on the page written
             files = image_alias.keys()
-            alias_text = image_alias.values()
-            index = [alias_text[idx][1] for idx in range(len(files))]
-            alias_text = [alias_text[idx][0] for idx in index]
-            files = [files[idx] for idx in index]
-
             all_files = [fn for fn in files if wikiutil.isPicture(fn) and
                          AttachFile.exists(self.request, self.pagename, fn)]
-            if self.sort_by == "alias":
-                # ToDo use a longer var
-                i = 0
-                for attfile in all_files:
-                    image_dict[alias_text[i]] = attfile
-                    # ToDo use a longer var
-                    i += 1
-                keys = image_dict.keys()
-                keys.sort()
-                all_files = [image_dict[txt] for txt in keys]
         else:
             all_files = _get_files(self.request, self.pagename)
         if self.file_regex != u'.':
             all_files = [attfile for attfile in all_files if re.match(self.file_regex, attfile)]
-
-        if self.sort_by == "name" and not self.only_items:
-            all_files.sort()
-        if self.sort_by == "date":
-            for attfile in all_files:
-                infile = os.path.join(path, attfile)
-                ft_file = "%s%x" % (os.path.getmtime(infile), randint(0, 256))
-                image_dict[ft_file] = attfile
-            keys = image_dict.keys()
-            keys.sort()
-            all_files = [image_dict[txt] for txt in keys]
-        image_dict.clear()
-        if self.reverse_sort:
-            all_files.reverse()
         if all_files:
             self.define_thumb_webnails(all_files, image_alias)
         return all_files
@@ -379,7 +348,9 @@
             self.request.write(self.formatter.text(text))
             self.request.write(self.formatter.div(1))
             self.request.write(self.formatter.preformatted(1))
-            for image in self.arnica_image.keys():
+            keys = self.arnica_image.keys()
+            keys.sort()
+            for image in keys:
                 text = ' * [[%s|alias]]\n' % image
                 self.request.write(self.formatter.text(text))
             self.request.write(self.formatter.preformatted(0))
@@ -390,8 +361,50 @@
         cols = min([self.columns, len(self.arnica_image)])
         result = []
         image_names = self.arnica_image.keys()
-        index = [self.arnica_image[img][4] for img in image_names]
-        selected_images = [image_names[int(idx)] for idx in index]
+        image_dict = {}
+        if self.only_items:
+            image_alias = self.get_image_alias()
+            alias_text = image_alias.values()
+            to_order = [int(alias_text[ix][1]) for ix in range(len(alias_text))]
+            names = [alias_text[ix][2] for ix in range(len(alias_text))]
+            alias = [alias_text[ix][0] for ix in range(len(alias_text))]
+
+            i = 0
+            for ix in to_order:
+                image_names[ix] = names[i]
+                alias_text[ix] = alias[i]
+                i += 1
+
+            # sort by alias
+            if self.sort_by == "alias":
+                i = 0
+                for img in image_names:
+                    image_dict[alias_text[i]] = img
+                    i += 1
+                keys = image_dict.keys()
+                keys.sort()
+                image_names = [image_dict[txt] for txt in keys]
+        else:
+            # sort by date
+            if self.sort_by == "date":
+                for img in image_names:
+                    itemname = self.pagename + '/' + img
+                    # to get uniq times (gives order for sorting)
+                    ft_file = "%d%x" % (self.Image(self.request, itemname).ctime, randint(0, 256))
+                    image_dict[ft_file] =  img
+                keys = image_dict.keys()
+                keys.sort()
+                image_names = [image_dict[txt] for txt in keys]
+
+            # default sort by name
+            elif self.sort_by == "name" and not self.only_items:
+                image_names.sort()
+
+        image_dict.clear()
+        # reverse sort
+        if self.reverse_sort:
+            image_names.reverse()
+
         if self.album:
             cols = 1
             album_image = self.album_image or self.arnica_image.keys()[0] #self.high_resolution_image[0]
@@ -400,11 +413,11 @@
 "%(album_image)s" because it does not exist or it is not listed
 in your item list!""") % {"album_image": album_image, })
             else:
-                html = self.html_arrange_thumbnails(album_image, selected_images)
+                html = self.html_arrange_thumbnails(album_image, image_names)
             result.append(''.join(html))
         else:
-            for image in selected_images:
-                html = self.html_arrange_thumbnails(image, selected_images)
+            for image in image_names:
+                html = self.html_arrange_thumbnails(image, image_names)
                 result.append(''.join(html))
                 if col_count == cols and self.columns != 0:
                     col_count = 0