gallery.py: Handle query and path separately for ._url()
authorW. Trevor King <wking@tremily.us>
Mon, 9 Dec 2013 05:59:47 +0000 (21:59 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 9 Dec 2013 05:59:47 +0000 (21:59 -0800)
This avoids computing a relpath for:

  /full/path/to/gallery?pp=2

from a base-path of:

  /full/path/to/gallery

and getting:

  ../gallery?pp=2

instead of the expected:

  ?pp=2

We could avoid this by using gallery/ in the first place, but keeping
query data as a dict until the last possible moment is also nice.

posts/gallery/gallery.py

index 40e09e16907fec4d4c5e59fd2ca685c26fece2a4..7b0d3d178d1f207679d6aa7d719de55b74aa1730 100755 (executable)
@@ -301,13 +301,15 @@ class CGIGalleryServer (object):
             return None
         return url[len(self._base_url):]
 
-    def _url(self, path):
+    def _url(self, path, query=None):
         relpath = _os_path.relpath(
             _os_path.join(self._base_path, path), self._base_path)
         if relpath == '.':
             relpath = ''
         elif path.endswith('/'):
             relpath += '/'
+        if query:
+            relpath = '{}?{}'.format(relpath, _urllib_parse.urlencode(query))
         return '{}{}'.format(self._base_url, relpath)
 
     def _label(self, path):
@@ -316,10 +318,11 @@ class CGIGalleryServer (object):
             dirname,base = _os_path.split(dirname)
         return base.replace('_', ' ').title()
 
-    def _link(self, path, text=None):
+    def _link(self, path, query=None, text=None):
         if text is None:
             text = self._label(path)
-        return '<a href="{}">{}</a>'.format(self._url(path), text)
+        return '<a href="{}">{}</a>'.format(
+            self._url(path=path, query=query), text)
 
     def _subdirs(self, path):
         try:
@@ -640,14 +643,14 @@ class CGIGalleryServer (object):
     def _directory_page_navigation(self, path, page, pages):
         if pages <= 1:
             return []
-        prev_page = path + '?pp={:d}'.format((page - 1) % pages + 1)
-        next_page = path + '?pp={:d}'.format((page + 1) % pages + 1)
+        prev_query = {'pp': (page - 1) % pages + 1}
+        next_query = {'pp': (page + 1) % pages + 1}
         return [
             '<div style="text-align: center;">',
             '<p>',
-            self._link(prev_page, 'previous'),
+            self._link(path=path, query=prev_query, text='previous'),
             '({:d} of {:d})'.format(page+1, pages),
-            self._link(next_page, 'next'),
+            self._link(path=path, query=next_query, text='next'),
             '</p>',
             '</div>', 
            ]
@@ -668,7 +671,7 @@ class CGIGalleryServer (object):
         for image in images:
             page = self._image_page(image)
             img = self._image(image, max_width=300, max_height=300)
-            link = self._link(page, img)
+            link = self._link(path=page, text=img)
             if column == 0:
                 content.append('  <tr>')
             content.extend([
@@ -717,7 +720,7 @@ class CGIGalleryServer (object):
         images_per_page = self._rows * self._columns
         i = images.index(path)
         page = i // images_per_page
-        gallery_page = '{}/?pp={:d}'.format(gallery, page + 1)
+        gallery_page = '{}/'.format(gallery)
         prev_page = self._image_page(images[i - 1])
         next_page = self._image_page(images[(i + 1) % len(images)])
         content = []
@@ -725,9 +728,10 @@ class CGIGalleryServer (object):
         content.extend([
                 '<div style="text-align: center;">',
                 '<p>',
-                self._link(prev_page, 'previous'),
-                self._link(gallery_page, 'all'),
-                self._link(next_page, 'next'),
+                self._link(path=prev_page, text='previous'),
+                self._link(
+                    path=gallery_page, query={'pp': page+1}, text='all'),
+                self._link(path=next_page, text='next'),
                 '</p>',
                 ])
         content.extend(self._captioned_video(path))