From bc0fdb6b0a080a081e6347f3f22f0a0eb0c56a1f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 8 Dec 2013 21:59:47 -0800 Subject: [PATCH] gallery.py: Handle query and path separately for ._url() 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 | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/posts/gallery/gallery.py b/posts/gallery/gallery.py index 40e09e1..7b0d3d1 100755 --- a/posts/gallery/gallery.py +++ b/posts/gallery/gallery.py @@ -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 '{}'.format(self._url(path), text) + return '{}'.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 [ '
', '

', - 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'), '

', '
', ] @@ -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(' ') 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([ '
', '

', - 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'), '

', ]) content.extend(self._captioned_video(path)) -- 2.26.2