Add Bookmark.normalize_title and _url.

This commit is contained in:
voussoir 2018-03-22 23:47:53 -07:00
parent 6ee86431e5
commit b4d3de3651
2 changed files with 35 additions and 11 deletions

View file

@ -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,

View file

@ -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,