misc: use datetime objects for today and now global variables (#29400)
This commit is contained in:
parent
d5ac2ffcb8
commit
5f59d55941
|
@ -1,3 +1,4 @@
|
|||
import datetime
|
||||
import pytest
|
||||
import os
|
||||
from StringIO import StringIO
|
||||
|
@ -7,11 +8,21 @@ from wcs.qommon.ezt import (Template, UnclosedBlocksError, UnmatchedEndError,
|
|||
UnmatchedElseError, ArgCountSyntaxError, _re_parse)
|
||||
from wcs.scripts import ScriptsSubstitutionProxy
|
||||
|
||||
from utilities import get_app, create_temporary_pub
|
||||
from utilities import get_app, create_temporary_pub, clean_temporary_pub
|
||||
|
||||
def setup_module(module):
|
||||
cleanup()
|
||||
|
||||
def teardown_module(module):
|
||||
clean_temporary_pub()
|
||||
|
||||
@pytest.fixture
|
||||
def pub(request):
|
||||
pub = create_temporary_pub()
|
||||
pub.cfg['language'] = {'language': 'en'}
|
||||
pub.write_cfg()
|
||||
return pub
|
||||
|
||||
def test_simple_qualifier():
|
||||
template = Template()
|
||||
template.parse('<p>[foo]</p>')
|
||||
|
@ -83,6 +94,32 @@ def test_callable_qualifier():
|
|||
template.generate(output, {'foo': lambda x: x.write('bar')})
|
||||
assert output.getvalue() == '<p>bar</p>'
|
||||
|
||||
def test_date_qualifier(pub):
|
||||
template = Template()
|
||||
template.parse('<p>[foo]</p>')
|
||||
output = StringIO()
|
||||
template.generate(output, {'foo': datetime.date(2019, 1, 2)})
|
||||
assert output.getvalue() == '<p>2019-01-02</p>'
|
||||
|
||||
pub.cfg['language'] = {'language': 'fr'}
|
||||
pub.write_cfg()
|
||||
output = StringIO()
|
||||
template.generate(output, {'foo': datetime.date(2019, 1, 2)})
|
||||
assert output.getvalue() == '<p>02/01/2019</p>'
|
||||
|
||||
def test_datetime_qualifier(pub):
|
||||
template = Template()
|
||||
template.parse('<p>[foo]</p>')
|
||||
output = StringIO()
|
||||
template.generate(output, {'foo': datetime.datetime(2019, 1, 2, 14, 4)})
|
||||
assert output.getvalue() == '<p>2019-01-02 14:04</p>'
|
||||
|
||||
pub.cfg['language'] = {'language': 'fr'}
|
||||
pub.write_cfg()
|
||||
output = StringIO()
|
||||
template.generate(output, {'foo': datetime.datetime(2019, 1, 2, 14, 4)})
|
||||
assert output.getvalue() == '<p>02/01/2019 14:04</p>'
|
||||
|
||||
def test_unclosed_block():
|
||||
template = Template()
|
||||
with pytest.raises(UnclosedBlocksError):
|
||||
|
@ -152,8 +189,7 @@ def test_dict_index():
|
|||
template.generate(output, {'foo': {'a': 'bar'}})
|
||||
assert output.getvalue() == '<p>[foo.b]</p>'
|
||||
|
||||
def test_ezt_script():
|
||||
pub = create_temporary_pub()
|
||||
def test_ezt_script(pub):
|
||||
os.mkdir(os.path.join(pub.app_dir, 'scripts'))
|
||||
fd = open(os.path.join(pub.app_dir, 'scripts', 'hello_world.py'), 'w')
|
||||
fd.write('''result = "Hello %s" % ("world" if not args else args[0])''')
|
||||
|
|
|
@ -17,7 +17,7 @@ def setup_module(module):
|
|||
|
||||
pub = create_temporary_pub()
|
||||
|
||||
req = HTTPRequest(None, {})
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
pub._set_request(req)
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -98,6 +98,13 @@ def test_prefill_formula_date_value():
|
|||
field.prefill = {'type': 'formula', 'value': 'utils.add_days("2016-01-01", 10)'}
|
||||
assert field.get_prefill_value() == ('2016-01-11', False)
|
||||
|
||||
def test_prefill_formula_today_value():
|
||||
pub.substitutions.get_context_variables = pub.get_substitution_variables
|
||||
field = fields.Field()
|
||||
field.prefill = {'type': 'formula', 'value': 'today'}
|
||||
today = datetime.date.today().strftime('%Y-%m-%d')
|
||||
assert field.get_prefill_value() == (today, False)
|
||||
|
||||
def test_prefill_formula_time_value():
|
||||
pub.substitutions.get_context_variables = lambda: {}
|
||||
field = fields.Field()
|
||||
|
|
|
@ -223,6 +223,7 @@ Directives
|
|||
# http://svn.webdav.org/repos/projects/ezt/trunk/
|
||||
#
|
||||
|
||||
import datetime
|
||||
import string
|
||||
import re
|
||||
from types import StringType, IntType, FloatType, LongType
|
||||
|
@ -722,6 +723,12 @@ def _write_value(valrefs, fp, ctx, format=lambda s: s):
|
|||
if format:
|
||||
fp.write(format(piece))
|
||||
|
||||
elif isinstance(value, datetime.datetime):
|
||||
from .misc import localstrftime
|
||||
fp.write(localstrftime(value))
|
||||
elif isinstance(value, datetime.date):
|
||||
from .misc import strftime, date_format
|
||||
fp.write(strftime(date_format(), value))
|
||||
# plain old value, write to output
|
||||
else:
|
||||
fp.write(format(str(value)))
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
import collections
|
||||
import cPickle
|
||||
import ConfigParser
|
||||
import datetime
|
||||
import imp
|
||||
import os
|
||||
import fcntl
|
||||
|
@ -1046,8 +1047,8 @@ class QommonPublisher(Publisher, object):
|
|||
'site_url': self.get_frontoffice_url(),
|
||||
'site_url_backoffice': self.get_backoffice_url(),
|
||||
'site_lang': (get_request() and hasattr(get_request(), 'language') and get_request().language) or 'en',
|
||||
'today': misc.strftime(misc.date_format(), time.localtime()),
|
||||
'now': misc.localstrftime(time.localtime()),
|
||||
'today': datetime.date.today(),
|
||||
'now': datetime.datetime.now(),
|
||||
'is_in_backoffice': (self.get_request() and self.get_request().is_in_backoffice()),
|
||||
}
|
||||
if self.site_options is None:
|
||||
|
|
Loading…
Reference in New Issue