Implement album sorting
This adds an 'albums_sort_attr' configuration modeled off of the 'medias_sort_attr'. It defaults to the existing behavior of sorting sub-albums by directory name. It allows sort by any metadata attribute stored in the album metadata file. The existing album sort test wasn't really verifying album sorting behavior, so I just re-wrote the whole thing to include tests for the old behavior and new. This should resolve issue #121
This commit is contained in:
@@ -221,6 +221,7 @@ class Album(UnicodeMixin):
|
||||
self.name = path.split(os.path.sep)[-1]
|
||||
self.gallery = gallery
|
||||
self.settings = settings
|
||||
self.subdirs = dirnames
|
||||
self.output_file = settings['output_filename']
|
||||
self._thumbnail = None
|
||||
|
||||
@@ -240,10 +241,6 @@ class Album(UnicodeMixin):
|
||||
self.index_url = url_from_path(os.path.relpath(
|
||||
settings['destination'], self.dst_path)) + '/' + self.url_ext
|
||||
|
||||
# sort sub-albums
|
||||
dirnames.sort(key=strxfrm, reverse=settings['albums_sort_reverse'])
|
||||
self.subdirs = dirnames
|
||||
|
||||
#: List of all medias in the album (:class:`~sigal.gallery.Image` and
|
||||
#: :class:`~sigal.gallery.Video`).
|
||||
self.medias = medias = []
|
||||
@@ -313,6 +310,20 @@ class Album(UnicodeMixin):
|
||||
self.orig_path = join(self.dst_path, self.settings['orig_dir'])
|
||||
check_or_create_dir(self.orig_path)
|
||||
|
||||
def sort_subdirs(self, albums_sort_attr):
|
||||
if self.subdirs:
|
||||
if albums_sort_attr:
|
||||
root_path = self.path if self.path != '.' else ''
|
||||
key = lambda s: strxfrm(getattr(
|
||||
self.gallery.albums[join(root_path, s)], albums_sort_attr))
|
||||
else:
|
||||
key = strxfrm
|
||||
|
||||
self.subdirs.sort(key=key,
|
||||
reverse=self.settings['albums_sort_reverse'])
|
||||
|
||||
signals.albums_sorted.send(self)
|
||||
|
||||
def sort_medias(self, medias_sort_attr):
|
||||
if self.medias:
|
||||
if medias_sort_attr == 'date':
|
||||
@@ -525,6 +536,11 @@ class Gallery(object):
|
||||
album.create_output_directories()
|
||||
albums[relpath] = album
|
||||
|
||||
with progressbar(albums.values(), label="Sorting albums",
|
||||
file=self.progressbar_target) as progress_albums:
|
||||
for album in progress_albums:
|
||||
album.sort_subdirs(settings['albums_sort_attr'])
|
||||
|
||||
with progressbar(albums.values(), label="Sorting media",
|
||||
file=self.progressbar_target) as progress_albums:
|
||||
for album in progress_albums:
|
||||
|
||||
@@ -31,6 +31,7 @@ from .compat import PY2, text_type
|
||||
|
||||
|
||||
_DEFAULT_CONFIG = {
|
||||
'albums_sort_attr': 'name',
|
||||
'albums_sort_reverse': False,
|
||||
'autorotate_images': True,
|
||||
'colorbox_column_size': 4,
|
||||
|
||||
@@ -8,4 +8,5 @@ album_initialized = signal('album_initialized')
|
||||
gallery_initialized = signal('gallery_initialized')
|
||||
gallery_build = signal('gallery_build')
|
||||
media_initialized = signal('media_initialized')
|
||||
albums_sorted = signal('albums_sorted')
|
||||
medias_sorted = signal('medias_sorted')
|
||||
|
||||
@@ -101,6 +101,9 @@ thumb_size = (280, 210)
|
||||
# Use symbolic links instead of copying the original images
|
||||
# orig_link = False
|
||||
|
||||
# Attribute of Album objects which is used to sort medias (eg 'title').
|
||||
# albums_sort_attr = 'name'
|
||||
|
||||
# Reverse sort for albums
|
||||
# albums_sort_reverse = False
|
||||
|
||||
|
||||
BIN
tests/sample/pictures/dir1/test3/.index.md.swp
Normal file
BIN
tests/sample/pictures/dir1/test3/.index.md.swp
Normal file
Binary file not shown.
BIN
tests/sample/pictures/dir1/test3/3.jpg
Normal file
BIN
tests/sample/pictures/dir1/test3/3.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 69 KiB |
1
tests/sample/pictures/dir1/test3/index.md
Normal file
1
tests/sample/pictures/dir1/test3/index.md
Normal file
@@ -0,0 +1 @@
|
||||
Title: 01 First title alphabetically
|
||||
@@ -15,7 +15,7 @@ REF = {
|
||||
'title': 'An example gallery',
|
||||
'name': 'dir1',
|
||||
'thumbnail': 'dir1/test1/thumbnails/11.tn.jpg',
|
||||
'subdirs': ['test1', 'test2'],
|
||||
'subdirs': ['test1', 'test2', 'test3'],
|
||||
'medias': [],
|
||||
},
|
||||
'dir1/test1': {
|
||||
@@ -34,6 +34,13 @@ REF = {
|
||||
'subdirs': [],
|
||||
'medias': ['21.jpg', '22.jpg', 'archlinux-kiss-1024x640.png'],
|
||||
},
|
||||
'dir1/test3': {
|
||||
'title': '01 First title alphabetically',
|
||||
'name': 'test3',
|
||||
'thumbnail': 'test3/thumbnails/3.tn.jpg',
|
||||
'subdirs': [],
|
||||
'medias': ['3.jpg'],
|
||||
},
|
||||
'dir2': {
|
||||
'title': 'Another example gallery with a very long name',
|
||||
'name': 'dir2',
|
||||
@@ -165,10 +172,29 @@ def test_album_medias(settings):
|
||||
def test_albums_sort(settings):
|
||||
gal = Gallery(settings, ncpu=1)
|
||||
album = REF['dir1']
|
||||
subdirs = list(album['subdirs'])
|
||||
|
||||
settings['albums_sort_reverse'] = False
|
||||
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
|
||||
a.sort_subdirs('')
|
||||
assert [alb.name for alb in a.albums] == subdirs
|
||||
|
||||
settings['albums_sort_reverse'] = True
|
||||
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
|
||||
assert [im.filename for im in a.images] == list(reversed(album['medias']))
|
||||
a.sort_subdirs('')
|
||||
assert [alb.name for alb in a.albums] == list(reversed(subdirs))
|
||||
|
||||
titles = [im.title for im in a.albums]
|
||||
titles.sort()
|
||||
settings['albums_sort_reverse'] = False
|
||||
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
|
||||
a.sort_subdirs('title')
|
||||
assert [im.title for im in a.albums] == titles
|
||||
|
||||
settings['albums_sort_reverse'] = True
|
||||
a = Album('dir1', settings, album['subdirs'], album['medias'], gal)
|
||||
a.sort_subdirs('title')
|
||||
assert [im.title for im in a.albums] == list(reversed(titles))
|
||||
|
||||
|
||||
def test_medias_sort(settings):
|
||||
|
||||
Reference in New Issue
Block a user