Skip to content

Commit

Permalink
replace get_summary with _get_summary
Browse files Browse the repository at this point in the history
When memoizing summary a dummy get_summary function was introduced to
properly work. This has multiple problems:
* The siteurl argument is actually not used in the function, it is only
  used so it properly memoizes.
* It differs from how content is accessed and memoized.

This commit brings summary inline with how content is accessed and
processed. It also removes the old get_summary function with the unused
siteurl argument.

Additionally _get_summary is marked as deprecated and calls .summary
instead while also issueing a warning.
  • Loading branch information
ingwinlu committed Oct 19, 2015
1 parent a69f429 commit 2cb2bf0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
24 changes: 13 additions & 11 deletions pelican/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def replacer(m):

return hrefs.sub(replacer, content)

def get_siteurl(self):
return self._context.get('localsiteurl', '')

@memoized
def get_content(self, siteurl):
if hasattr(self, '_get_content'):
Expand All @@ -281,38 +284,37 @@ def get_content(self, siteurl):
content = self._content
return self._update_content(content, siteurl)

def get_siteurl(self):
return self._context.get('localsiteurl', '')

@property
def content(self):
return self.get_content(self.get_siteurl())

def _get_summary(self):
@memoized
def get_summary(self, siteurl):
"""Returns the summary of an article.
This is based on the summary metadata if set, otherwise truncate the
content.
"""
if hasattr(self, '_summary'):
return self._update_content(self._summary,
self.get_siteurl())
return self._update_content(self._summary, siteurl)

if self.settings['SUMMARY_MAX_LENGTH'] is None:
return self.content

return truncate_html_words(self.content,
self.settings['SUMMARY_MAX_LENGTH'])

@memoized
def get_summary(self, siteurl):
"""uses siteurl to be memoizable"""
return self._get_summary()

@property
def summary(self):
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
def summary(self, value):
"""Dummy function"""
Expand Down
24 changes: 23 additions & 1 deletion pelican/tests/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False)


class TestPage(unittest.TestCase):
class TestPage(LoggedTestCase):

def setUp(self):
super(TestPage, self).setUp()
Expand All @@ -41,9 +41,19 @@ def setUp(self):
},
'source_path': '/path/to/file/foo.ext'
}
self._disable_limit_filter()

def tearDown(self):
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):
# Creating a page with arguments passed to the constructor should use
Expand Down Expand Up @@ -87,6 +97,18 @@ def test_summary_max_length(self):
page = Page(**page_kwargs)
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):
page_kwargs = self._copy_page_kwargs()
settings = get_settings()
Expand Down

0 comments on commit 2cb2bf0

Please sign in to comment.