From 6ee86431e5ba693ee44540eabfb9ca402c2cf678 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 22 Mar 2018 23:39:11 -0700 Subject: [PATCH] Add Album.normalize_title and _description. --- etiquette/objects.py | 35 ++++++++++++++++--- etiquette/photodb.py | 10 ++---- .../etiquette_flask/templates/album.html | 2 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 8ee3312..57139ee 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -5,6 +5,7 @@ but are returned by the PDB accesses. import os import PIL.Image +import string import traceback from . import constants @@ -232,8 +233,8 @@ class Album(ObjectBase, GroupableMixin): db_row = dict(zip(constants.SQL_COLUMNS['albums'], db_row)) self.id = db_row['id'] - self.title = db_row['title'] or '' - self.description = db_row['description'] or '' + self.title = self.normalize_title(db_row['title']) + self.description = self.normalize_description(db_row['description']) self.author_id = self.normalize_author_id(db_row['author_id']) self.name = 'Album %s' % self.id @@ -249,6 +250,32 @@ class Album(ObjectBase, GroupableMixin): def __repr__(self): return 'Album:{id}'.format(id=self.id) + @staticmethod + def normalize_description(description): + if description is None: + return '' + + if not isinstance(description, str): + raise TypeError('Description must be string, not %s' % type(description)) + + description = description.strip() + + return description + + @staticmethod + def normalize_title(title): + if title is None: + return '' + + if not isinstance(title, str): + raise TypeError('Title must be string, not %s' % type(title)) + + title = title.strip() + for whitespace in string.whitespace: + title = title.replace(whitespace, ' ') + + return title + def _uncache(self): self._uncache_sums() self.photodb.caches['album'].remove(self.id) @@ -393,10 +420,10 @@ class Album(ObjectBase, GroupableMixin): return if title is not None: - self.title = title + self.title = self.normalize_title(title) if description is not None: - self.description = description + self.description = self.normalize_description(description) data = { 'id': self.id, diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 88258e0..a63bad0 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -80,14 +80,10 @@ class PDBAlbumMixin: ''' Create a new album. Photos can be added now or later. ''' - album_id = self.generate_id('albums') - title = title or '' - description = description or '' - if not isinstance(title, str): - raise TypeError('Title must be string, not %s' % type(title)) + title = objects.Album.normalize_title(title) + description = objects.Album.normalize_description(description) - if not isinstance(description, str): - raise TypeError('Description must be string, not %s' % type(description)) + album_id = self.generate_id('albums') self.log.debug('New Album: %s %s', album_id, title) diff --git a/frontends/etiquette_flask/templates/album.html b/frontends/etiquette_flask/templates/album.html index 8f93aee..34c9d59 100644 --- a/frontends/etiquette_flask/templates/album.html +++ b/frontends/etiquette_flask/templates/album.html @@ -55,7 +55,7 @@ p id="description_text" data-editor-id="description" data-editor-placeholder="description" - {% if album.description == "" %}class="hidden"{% endif %} + {% if not album.description %}class="hidden"{% endif %} > {{-album.description-}}