From b4d3de365181958575bd1a29df1a1b14a9f4cdef Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 22 Mar 2018 23:47:53 -0700 Subject: [PATCH] Add Bookmark.normalize_title and _url. --- etiquette/objects.py | 39 +++++++++++++++++++++++++++++++++------ etiquette/photodb.py | 7 ++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 57139ee..2f3bf7b 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -541,13 +541,42 @@ class Bookmark(ObjectBase): db_row = dict(zip(constants.SQL_COLUMNS['bookmarks'], db_row)) self.id = db_row['id'] - self.title = db_row['title'] - self.url = db_row['url'] + self.title = self.normalize_title(db_row['title']) + self.url = self.normalize_url(db_row['url']) self.author_id = self.normalize_author_id(db_row['author_id']) def __repr__(self): return 'Bookmark:{id}'.format(id=self.id) + @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 + + @staticmethod + def normalize_url(url): + if url is None: + return '' + + if not isinstance(url, str): + raise TypeError('URL must be string, not %s' % type(title)) + + url = url.strip() + + if not url: + raise ValueError('Invalid URL "%s"' % url) + + return url + @decorators.required_feature('bookmark.edit') @decorators.transaction def delete(self, *, commit=True): @@ -565,12 +594,10 @@ class Bookmark(ObjectBase): return if title is not None: - self.title = title + self.title = self.normalize_title(title) if url is not None: - if not url: - raise ValueError('Need a URL') - self.url = url + self.url = self.normalize_url(url) data = { 'id': self.id, diff --git a/etiquette/photodb.py b/etiquette/photodb.py index a63bad0..4e3501b 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -126,15 +126,12 @@ class PDBBookmarkMixin: @decorators.required_feature('bookmark.new') @decorators.transaction def new_bookmark(self, url, title=None, *, author=None, commit=True): - if not url: - raise ValueError('Must provide a URL') + title = objects.Bookmark.normalize_title(title) + url = objects.Bookmark.normalize_url(url) bookmark_id = self.generate_id('bookmarks') - title = title or None author_id = self.get_user_id_or_none(author) - # To do: NORMALIZATION AND VALIDATION - data = { 'author_id': author_id, 'id': bookmark_id,