forked from github/pelican
Merge pull request #1844 from ingwinlu/get_summary_sideeffects
Replace get_summary with _get_summary
This commit is contained in:
commit
437bcdbd66
3 changed files with 42 additions and 12 deletions
|
|
@ -273,6 +273,9 @@ class Content(object):
|
||||||
|
|
||||||
return hrefs.sub(replacer, content)
|
return hrefs.sub(replacer, content)
|
||||||
|
|
||||||
|
def get_siteurl(self):
|
||||||
|
return self._context.get('localsiteurl', '')
|
||||||
|
|
||||||
@memoized
|
@memoized
|
||||||
def get_content(self, siteurl):
|
def get_content(self, siteurl):
|
||||||
if hasattr(self, '_get_content'):
|
if hasattr(self, '_get_content'):
|
||||||
|
|
@ -281,22 +284,19 @@ class Content(object):
|
||||||
content = self._content
|
content = self._content
|
||||||
return self._update_content(content, siteurl)
|
return self._update_content(content, siteurl)
|
||||||
|
|
||||||
def get_siteurl(self):
|
|
||||||
return self._context.get('localsiteurl', '')
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def content(self):
|
def content(self):
|
||||||
return self.get_content(self.get_siteurl())
|
return self.get_content(self.get_siteurl())
|
||||||
|
|
||||||
def _get_summary(self):
|
@memoized
|
||||||
|
def get_summary(self, siteurl):
|
||||||
"""Returns the summary of an article.
|
"""Returns the summary of an article.
|
||||||
|
|
||||||
This is based on the summary metadata if set, otherwise truncate the
|
This is based on the summary metadata if set, otherwise truncate the
|
||||||
content.
|
content.
|
||||||
"""
|
"""
|
||||||
if hasattr(self, '_summary'):
|
if hasattr(self, '_summary'):
|
||||||
return self._update_content(self._summary,
|
return self._update_content(self._summary, siteurl)
|
||||||
self.get_siteurl())
|
|
||||||
|
|
||||||
if self.settings['SUMMARY_MAX_LENGTH'] is None:
|
if self.settings['SUMMARY_MAX_LENGTH'] is None:
|
||||||
return self.content
|
return self.content
|
||||||
|
|
@ -304,15 +304,17 @@ class Content(object):
|
||||||
return truncate_html_words(self.content,
|
return truncate_html_words(self.content,
|
||||||
self.settings['SUMMARY_MAX_LENGTH'])
|
self.settings['SUMMARY_MAX_LENGTH'])
|
||||||
|
|
||||||
@memoized
|
|
||||||
def get_summary(self, siteurl):
|
|
||||||
"""uses siteurl to be memoizable"""
|
|
||||||
return self._get_summary()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def summary(self):
|
def summary(self):
|
||||||
return self.get_summary(self.get_siteurl())
|
return self.get_summary(self.get_siteurl())
|
||||||
|
|
||||||
|
def _get_summary(self):
|
||||||
|
"""deprecated function to access summary"""
|
||||||
|
|
||||||
|
logger.warn('_get_summary() has been deprecated since 3.6.4. '
|
||||||
|
'Use the summary decorator instead')
|
||||||
|
return self.summary
|
||||||
|
|
||||||
@summary.setter
|
@summary.setter
|
||||||
def summary(self, value):
|
def summary(self, value):
|
||||||
"""Dummy function"""
|
"""Dummy function"""
|
||||||
|
|
|
||||||
|
|
@ -167,6 +167,12 @@ class LimitLogger(SafeLogger):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(LimitLogger, self).__init__(*args, **kwargs)
|
super(LimitLogger, self).__init__(*args, **kwargs)
|
||||||
|
self.enable_filter()
|
||||||
|
|
||||||
|
def disable_filter(self):
|
||||||
|
self.removeFilter(LimitLogger.limit_filter)
|
||||||
|
|
||||||
|
def enable_filter(self):
|
||||||
self.addFilter(LimitLogger.limit_filter)
|
self.addFilter(LimitLogger.limit_filter)
|
||||||
|
|
||||||
logging.setLoggerClass(LimitLogger)
|
logging.setLoggerClass(LimitLogger)
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ TEST_CONTENT = str(generate_lorem_ipsum(n=1))
|
||||||
TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False)
|
TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False)
|
||||||
|
|
||||||
|
|
||||||
class TestPage(unittest.TestCase):
|
class TestPage(LoggedTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestPage, self).setUp()
|
super(TestPage, self).setUp()
|
||||||
|
|
@ -41,9 +41,19 @@ class TestPage(unittest.TestCase):
|
||||||
},
|
},
|
||||||
'source_path': '/path/to/file/foo.ext'
|
'source_path': '/path/to/file/foo.ext'
|
||||||
}
|
}
|
||||||
|
self._disable_limit_filter()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
locale.setlocale(locale.LC_ALL, self.old_locale)
|
locale.setlocale(locale.LC_ALL, self.old_locale)
|
||||||
|
self._enable_limit_filter()
|
||||||
|
|
||||||
|
def _disable_limit_filter(self):
|
||||||
|
from pelican.contents import logger
|
||||||
|
logger.disable_filter()
|
||||||
|
|
||||||
|
def _enable_limit_filter(self):
|
||||||
|
from pelican.contents import logger
|
||||||
|
logger.enable_filter()
|
||||||
|
|
||||||
def test_use_args(self):
|
def test_use_args(self):
|
||||||
# Creating a page with arguments passed to the constructor should use
|
# Creating a page with arguments passed to the constructor should use
|
||||||
|
|
@ -87,6 +97,18 @@ class TestPage(unittest.TestCase):
|
||||||
page = Page(**page_kwargs)
|
page = Page(**page_kwargs)
|
||||||
self.assertEqual(page.summary, '')
|
self.assertEqual(page.summary, '')
|
||||||
|
|
||||||
|
def test_summary_get_summary_warning(self):
|
||||||
|
"""calling ._get_summary() should issue a warning"""
|
||||||
|
page_kwargs = self._copy_page_kwargs()
|
||||||
|
page = Page(**page_kwargs)
|
||||||
|
self.assertEqual(page.summary, TEST_SUMMARY)
|
||||||
|
self.assertEqual(page._get_summary(), TEST_SUMMARY)
|
||||||
|
self.assertLogCountEqual(
|
||||||
|
count=1,
|
||||||
|
msg="_get_summary\(\) has been deprecated since 3\.6\.4\. "
|
||||||
|
"Use the summary decorator instead",
|
||||||
|
level=logging.WARNING)
|
||||||
|
|
||||||
def test_slug(self):
|
def test_slug(self):
|
||||||
page_kwargs = self._copy_page_kwargs()
|
page_kwargs = self._copy_page_kwargs()
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue