Added very basic note summarizing for search results if the backend doesn't return a note summary (e.g. for SQLite).
This commit is contained in:
parent
ee38d64302
commit
2fa2010dd9
|
@ -426,18 +426,30 @@ class Notebooks( object ):
|
||||||
note = summarize and self.summarize_note( note ) or note,
|
note = summarize and self.summarize_note( note ) or note,
|
||||||
)
|
)
|
||||||
|
|
||||||
def summarize_note( self, note ):
|
def summarize_note( self, note, max_summary_length = None, word_count = None ):
|
||||||
"""
|
"""
|
||||||
Create a truncated note summary for the given note, and then return the note with its summary
|
Create a truncated, HTML-free note summary for the given note, and then return the note with
|
||||||
set.
|
its summary set.
|
||||||
|
|
||||||
@type note: model.Note or NoneType
|
@type note: model.Note or NoneType
|
||||||
@param note: note to summarize, or None
|
@param note: note to summarize, or None
|
||||||
|
@type max_summary_length: int or NoneType
|
||||||
|
@param max_summary_length: the length to which the summary is truncated (optional, defaults
|
||||||
|
to a reasonable length)
|
||||||
|
@type word_count: int or NoneType
|
||||||
|
@param word_count: the number of words to which the summary is truncated (optional, defaults
|
||||||
|
to a reasonable number of words)
|
||||||
@rtype: model.Note or NoneType
|
@rtype: model.Note or NoneType
|
||||||
@return: note with its summary member set, or None if no note was provided
|
@return: note with its summary member set, or None if no note was provided
|
||||||
"""
|
"""
|
||||||
MAX_SUMMARY_LENGTH = 40
|
DEFAULT_MAX_SUMMARY_LENGTH = 40
|
||||||
word_count = 10
|
DEFAULT_WORD_COUNT = 10
|
||||||
|
|
||||||
|
if not max_summary_length:
|
||||||
|
max_summary_length = DEFAULT_MAX_SUMMARY_LENGTH
|
||||||
|
|
||||||
|
if not word_count:
|
||||||
|
word_count = DEFAULT_WORD_COUNT
|
||||||
|
|
||||||
if note is None:
|
if note is None:
|
||||||
return None
|
return None
|
||||||
|
@ -460,13 +472,13 @@ class Notebooks( object ):
|
||||||
truncated = False
|
truncated = False
|
||||||
summary = first_words( words, word_count )
|
summary = first_words( words, word_count )
|
||||||
|
|
||||||
while len( summary ) > MAX_SUMMARY_LENGTH:
|
while len( summary ) > max_summary_length:
|
||||||
word_count -= 1
|
word_count -= 1
|
||||||
summary = first_words( words, word_count )
|
summary = first_words( words, word_count )
|
||||||
|
|
||||||
# if the first word is just ridiculously long, truncate it without finding a word boundary
|
# if the first word is just ridiculously long, truncate it without finding a word boundary
|
||||||
if word_count == 1:
|
if word_count == 1:
|
||||||
summary = summary[ : MAX_SUMMARY_LENGTH ]
|
summary = summary[ : max_summary_length ]
|
||||||
truncated = True
|
truncated = True
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -1111,6 +1123,9 @@ class Notebooks( object ):
|
||||||
|
|
||||||
notes = self.__database.select_many( Note, Notebook.sql_search_notes( user_id, notebook_id, search_text, self.__database.backend ) )
|
notes = self.__database.select_many( Note, Notebook.sql_search_notes( user_id, notebook_id, search_text, self.__database.backend ) )
|
||||||
|
|
||||||
|
# make a summary for each note that doesn't have one
|
||||||
|
notes = [ note.summary and note or self.summarize_note( note, max_summary_length = 80, word_count = 30 ) for note in notes ]
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
notes = notes,
|
notes = notes,
|
||||||
)
|
)
|
||||||
|
|
|
@ -3182,6 +3182,7 @@ class Test_notebooks( Test_controller ):
|
||||||
|
|
||||||
assert len( notes ) == 1
|
assert len( notes ) == 1
|
||||||
assert notes[ 0 ].object_id == self.note.object_id
|
assert notes[ 0 ].object_id == self.note.object_id
|
||||||
|
assert notes[ 0 ].summary
|
||||||
|
|
||||||
def test_search_without_login( self ):
|
def test_search_without_login( self ):
|
||||||
search_text = u"bla"
|
search_text = u"bla"
|
||||||
|
@ -3220,6 +3221,7 @@ class Test_notebooks( Test_controller ):
|
||||||
|
|
||||||
assert len( notes ) == 1
|
assert len( notes ) == 1
|
||||||
assert notes[ 0 ].object_id == self.note.object_id
|
assert notes[ 0 ].object_id == self.note.object_id
|
||||||
|
assert notes[ 0 ].summary
|
||||||
|
|
||||||
def test_search_empty( self ):
|
def test_search_empty( self ):
|
||||||
self.login()
|
self.login()
|
||||||
|
@ -3231,8 +3233,6 @@ class Test_notebooks( Test_controller ):
|
||||||
search_text = search_text,
|
search_text = search_text,
|
||||||
), session_id = self.session_id )
|
), session_id = self.session_id )
|
||||||
|
|
||||||
notes = result.get( "notes" )
|
|
||||||
|
|
||||||
assert result[ "error" ]
|
assert result[ "error" ]
|
||||||
assert u"missing" in result[ "error" ]
|
assert u"missing" in result[ "error" ]
|
||||||
|
|
||||||
|
@ -3246,8 +3246,6 @@ class Test_notebooks( Test_controller ):
|
||||||
search_text = search_text,
|
search_text = search_text,
|
||||||
), session_id = self.session_id )
|
), session_id = self.session_id )
|
||||||
|
|
||||||
notes = result.get( "notes" )
|
|
||||||
|
|
||||||
assert result[ "error" ]
|
assert result[ "error" ]
|
||||||
assert u"too long" in result[ "error" ]
|
assert u"too long" in result[ "error" ]
|
||||||
|
|
||||||
|
@ -3284,7 +3282,9 @@ class Test_notebooks( Test_controller ):
|
||||||
|
|
||||||
assert len( notes ) == 2
|
assert len( notes ) == 2
|
||||||
assert notes[ 0 ].object_id == note3.object_id
|
assert notes[ 0 ].object_id == note3.object_id
|
||||||
|
assert notes[ 0 ].summary
|
||||||
assert notes[ 1 ].object_id == self.note.object_id
|
assert notes[ 1 ].object_id == self.note.object_id
|
||||||
|
assert notes[ 1 ].summary
|
||||||
|
|
||||||
def test_search_character_refs( self ):
|
def test_search_character_refs( self ):
|
||||||
self.login()
|
self.login()
|
||||||
|
@ -3303,6 +3303,7 @@ class Test_notebooks( Test_controller ):
|
||||||
|
|
||||||
assert len( notes ) == 1
|
assert len( notes ) == 1
|
||||||
assert notes[ 0 ].object_id == note3.object_id
|
assert notes[ 0 ].object_id == note3.object_id
|
||||||
|
assert notes[ 0 ].summary
|
||||||
|
|
||||||
def test_search_titles( self ):
|
def test_search_titles( self ):
|
||||||
self.login()
|
self.login()
|
||||||
|
|
Reference in New Issue