mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Create new formatting context dict instead of using self.__dict__
Using self.__dict__ is fine, but when its mutated it changes the object's state. Creating a new dict avoids needing to think about that, and doesn't introduce Python 3 issues (ie, where self.number is accidentally set to '').
This commit is contained in:
parent
74c7c72fb3
commit
5ffbf907de
1 changed files with 29 additions and 17 deletions
|
|
@ -125,23 +125,35 @@ class Page(object):
|
||||||
if p.min_page <= self.number:
|
if p.min_page <= self.number:
|
||||||
rule = p
|
rule = p
|
||||||
|
|
||||||
value = getattr(rule, key)
|
if not rule:
|
||||||
|
return ''
|
||||||
|
|
||||||
if not isinstance(value, six.string_types):
|
prop_value = getattr(rule, key)
|
||||||
logger.warning('%s is set to %s' % (setting, value))
|
|
||||||
return value
|
if not isinstance(prop_value, six.string_types):
|
||||||
else:
|
logger.warning('%s is set to %s' % (key, prop_value))
|
||||||
context = self.__dict__
|
return prop_value
|
||||||
context['base_name'] = os.path.dirname(self.name)
|
|
||||||
context['number_sep'] = '/'
|
# URL or SAVE_AS is a string, format it with a controlled context
|
||||||
if self.number == 1:
|
context = {
|
||||||
# no page numbers on the first page
|
'name': self.name,
|
||||||
context['number'] = ''
|
'object_list': self.object_list,
|
||||||
context['number_sep'] = ''
|
'number': self.number,
|
||||||
ret = six.u(value).format(**context)
|
'paginator': self.paginator,
|
||||||
if ret[0] == '/':
|
'settings': self.settings,
|
||||||
ret = ret[1:]
|
'base_name': os.path.dirname(self.name),
|
||||||
return ret
|
'number_sep': '/',
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.number == 1:
|
||||||
|
# no page numbers on the first page
|
||||||
|
context['number'] = ''
|
||||||
|
context['number_sep'] = ''
|
||||||
|
|
||||||
|
ret = prop_value.format(**context)
|
||||||
|
if ret[0] == '/':
|
||||||
|
ret = ret[1:]
|
||||||
|
return ret
|
||||||
|
|
||||||
url = property(functools.partial(_from_settings, key='URL'))
|
url = property(functools.partial(_from_settings, key='URL'))
|
||||||
save_as = property(functools.partial(_from_settings, key='SAVE_AS'))
|
save_as = property(functools.partial(_from_settings, key='SAVE_AS'))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue