From 484e3ae1eed725ede53b54ce846eec93585796f7 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 7 Jan 2021 19:23:12 -0800 Subject: [PATCH] Add Album.sum_children. --- etiquette/objects.py | 24 +++++++++++++++---- .../etiquette_flask/templates/album_card.html | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 0cb649d..6575931 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -562,13 +562,13 @@ class Album(ObjectBase, GroupableMixin): self._remove_photo(photo) def sum_bytes(self, recurse=True): - query = ''' + query = stringtools.collapse_whitespace(''' SELECT SUM(bytes) FROM photos WHERE photos.id IN ( SELECT photoid FROM album_photo_rel WHERE albumid IN {albumids} ) - ''' + ''') if recurse: albumids = [child.id for child in self.walk_children()] else: @@ -579,17 +579,33 @@ class Album(ObjectBase, GroupableMixin): total = self.photodb.sql_select_one(query)[0] return total + def sum_children(self, recurse=True): + if recurse: + walker = self.walk_children() + # First yield is itself. + next(walker) + return sum(1 for child in walker) + + query = stringtools.collapse_whitespace(''' + SELECT COUNT(memberid) + FROM album_group_rel + WHERE parentid == ? + ''') + bindings = [self.id] + total = self.photodb.sql_select_one(query, bindings)[0] + return total + def sum_photos(self, recurse=True): ''' If all you need is the number of photos in the album, this method is preferable to len(album.get_photos()) because it performs the counting in the database instead of creating the Photo objects. ''' - query = ''' + query = stringtools.collapse_whitespace(''' SELECT COUNT(photoid) FROM album_photo_rel WHERE albumid IN {albumids} - ''' + ''') if recurse: albumids = [child.id for child in self.walk_children()] else: diff --git a/frontends/etiquette_flask/templates/album_card.html b/frontends/etiquette_flask/templates/album_card.html index 9cf9ee1..d68900d 100644 --- a/frontends/etiquette_flask/templates/album_card.html +++ b/frontends/etiquette_flask/templates/album_card.html @@ -47,7 +47,7 @@ draggable=true
{% if album == "root" %} {% else %} - {% set child_count = album.get_children()|length %} + {% set child_count = album.sum_children(recurse=False) %} {% set photo_count = album.sum_photos(recurse=False) %} {{child_count}} {{-' | '-}}