Add Bookmark.normalize_title and _url.
This commit is contained in:
parent
6ee86431e5
commit
b4d3de3651
2 changed files with 35 additions and 11 deletions
|
@ -541,13 +541,42 @@ class Bookmark(ObjectBase):
|
||||||
db_row = dict(zip(constants.SQL_COLUMNS['bookmarks'], db_row))
|
db_row = dict(zip(constants.SQL_COLUMNS['bookmarks'], db_row))
|
||||||
|
|
||||||
self.id = db_row['id']
|
self.id = db_row['id']
|
||||||
self.title = db_row['title']
|
self.title = self.normalize_title(db_row['title'])
|
||||||
self.url = db_row['url']
|
self.url = self.normalize_url(db_row['url'])
|
||||||
self.author_id = self.normalize_author_id(db_row['author_id'])
|
self.author_id = self.normalize_author_id(db_row['author_id'])
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'Bookmark:{id}'.format(id=self.id)
|
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.required_feature('bookmark.edit')
|
||||||
@decorators.transaction
|
@decorators.transaction
|
||||||
def delete(self, *, commit=True):
|
def delete(self, *, commit=True):
|
||||||
|
@ -565,12 +594,10 @@ class Bookmark(ObjectBase):
|
||||||
return
|
return
|
||||||
|
|
||||||
if title is not None:
|
if title is not None:
|
||||||
self.title = title
|
self.title = self.normalize_title(title)
|
||||||
|
|
||||||
if url is not None:
|
if url is not None:
|
||||||
if not url:
|
self.url = self.normalize_url(url)
|
||||||
raise ValueError('Need a URL')
|
|
||||||
self.url = url
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
|
|
|
@ -126,15 +126,12 @@ class PDBBookmarkMixin:
|
||||||
@decorators.required_feature('bookmark.new')
|
@decorators.required_feature('bookmark.new')
|
||||||
@decorators.transaction
|
@decorators.transaction
|
||||||
def new_bookmark(self, url, title=None, *, author=None, commit=True):
|
def new_bookmark(self, url, title=None, *, author=None, commit=True):
|
||||||
if not url:
|
title = objects.Bookmark.normalize_title(title)
|
||||||
raise ValueError('Must provide a URL')
|
url = objects.Bookmark.normalize_url(url)
|
||||||
|
|
||||||
bookmark_id = self.generate_id('bookmarks')
|
bookmark_id = self.generate_id('bookmarks')
|
||||||
title = title or None
|
|
||||||
author_id = self.get_user_id_or_none(author)
|
author_id = self.get_user_id_or_none(author)
|
||||||
|
|
||||||
# To do: NORMALIZATION AND VALIDATION
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'author_id': author_id,
|
'author_id': author_id,
|
||||||
'id': bookmark_id,
|
'id': bookmark_id,
|
||||||
|
|
Loading…
Reference in a new issue