Add Album.normalize_title and _description.

This commit is contained in:
voussoir 2018-03-22 23:39:11 -07:00
parent dddd8a3aa1
commit 6ee86431e5
3 changed files with 35 additions and 12 deletions

View file

@ -5,6 +5,7 @@ but are returned by the PDB accesses.
import os import os
import PIL.Image import PIL.Image
import string
import traceback import traceback
from . import constants from . import constants
@ -232,8 +233,8 @@ class Album(ObjectBase, GroupableMixin):
db_row = dict(zip(constants.SQL_COLUMNS['albums'], db_row)) db_row = dict(zip(constants.SQL_COLUMNS['albums'], db_row))
self.id = db_row['id'] self.id = db_row['id']
self.title = db_row['title'] or '' self.title = self.normalize_title(db_row['title'])
self.description = db_row['description'] or '' self.description = self.normalize_description(db_row['description'])
self.author_id = self.normalize_author_id(db_row['author_id']) self.author_id = self.normalize_author_id(db_row['author_id'])
self.name = 'Album %s' % self.id self.name = 'Album %s' % self.id
@ -249,6 +250,32 @@ class Album(ObjectBase, GroupableMixin):
def __repr__(self): def __repr__(self):
return 'Album:{id}'.format(id=self.id) 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): def _uncache(self):
self._uncache_sums() self._uncache_sums()
self.photodb.caches['album'].remove(self.id) self.photodb.caches['album'].remove(self.id)
@ -393,10 +420,10 @@ class Album(ObjectBase, GroupableMixin):
return return
if title is not None: if title is not None:
self.title = title self.title = self.normalize_title(title)
if description is not None: if description is not None:
self.description = description self.description = self.normalize_description(description)
data = { data = {
'id': self.id, 'id': self.id,

View file

@ -80,14 +80,10 @@ class PDBAlbumMixin:
''' '''
Create a new album. Photos can be added now or later. Create a new album. Photos can be added now or later.
''' '''
album_id = self.generate_id('albums') title = objects.Album.normalize_title(title)
title = title or '' description = objects.Album.normalize_description(description)
description = description or ''
if not isinstance(title, str):
raise TypeError('Title must be string, not %s' % type(title))
if not isinstance(description, str): album_id = self.generate_id('albums')
raise TypeError('Description must be string, not %s' % type(description))
self.log.debug('New Album: %s %s', album_id, title) self.log.debug('New Album: %s %s', album_id, title)

View file

@ -55,7 +55,7 @@ p
id="description_text" id="description_text"
data-editor-id="description" data-editor-id="description"
data-editor-placeholder="description" data-editor-placeholder="description"
{% if album.description == "" %}class="hidden"{% endif %} {% if not album.description %}class="hidden"{% endif %}
> >
{{-album.description-}} {{-album.description-}}
</pre> </pre>