0001-templatetags-add-add_days-and-add_hours-filters-2940.patch
tests/test_formdata.py | ||
---|---|---|
944 | 944 |
condition = Condition({'type': 'django', 'value': 'today == now|date'}) |
945 | 945 |
assert condition.evaluate() is True |
946 | 946 | |
947 |
def test_lazy_date_with_maths(pub, variable_test_data): |
|
948 |
# form_var_datefield : 2018-07-31 |
|
949 |
# form_var_datefield2 : 2018-08-31 |
|
950 |
# form_var_datestring : "2018-07-31" |
|
951 |
for condition_value in ( |
|
952 |
'form_var_datefield|add_days:0 <= "2019-01-01"', |
|
953 |
'form_var_datefield|add_days:0 >= "1980-01-01"', |
|
954 |
'form_var_datefield|add_days:0 == "2018-07-31"', |
|
955 |
'form_var_datefield|add_days:0 == "31/07/2018"', |
|
956 |
'form_var_datefield|add_days:5 == "2018-08-05"', |
|
957 |
'form_var_datefield|add_days:5 <= "2018-08-05"', |
|
958 |
'form_var_datefield|add_days:5 >= "2018-08-05"', |
|
959 |
'form_var_datefield|add_days:-5 == "2018-07-26"', |
|
960 |
'form_var_datefield|add_days:36500 > "2100-01-01"', # 2118 |
|
961 |
'form_var_datefield|add_days:-36500 < "1950-01-01"', # 1918 |
|
962 |
'form_var_datefield|add_days:31 == form_var_datefield2', |
|
963 |
'form_var_datefield|add_days:5|add_days:-5 == form_var_datestring', |
|
964 |
'form_var_datestring|add_days:31 == form_var_datefield2', |
|
965 |
'form_var_datestring|add_days:32 > form_var_datefield2', |
|
966 |
'form_var_datestring|add_days:30 < form_var_datefield2', |
|
967 |
): |
|
968 |
condition = Condition({'type': 'django', 'value': condition_value}) |
|
969 |
assert condition.evaluate() is True |
|
970 | ||
971 |
# form_var_datefield is in 2018, we hope today/now is after 2019 |
|
972 |
for condition_value in ( |
|
973 |
'form_var_datefield <= today', |
|
974 |
'"1970-01-01"|add_days:0 < today', |
|
975 |
'"2100-12-31"|add_days:0 > today', |
|
976 |
'form_var_datefield|add_days:0 <= today', |
|
977 |
'form_var_datefield|add_days:10 < today', |
|
978 |
'form_var_datefield|add_days:36500 > today', |
|
979 |
'form_var_datefield|add_days:36500 >= today', |
|
980 |
'form_var_datefield <= today|add_days:0', |
|
981 |
'form_var_datefield < today|add_days:-10', |
|
982 |
'form_var_datefield > today|add_days:-36500', |
|
983 |
'form_var_datefield >= today|add_days:-36500', |
|
984 |
): |
|
985 |
condition = Condition({'type': 'django', 'value': condition_value}) |
|
986 |
assert condition.evaluate() is True |
|
987 |
condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')}) |
|
988 |
assert condition.evaluate() is True |
|
989 | ||
990 |
for condition_value in ( |
|
991 |
'"1970-01-01"|add_hours:0 < today', |
|
992 |
'"2100-12-31"|add_hours:0 > today', |
|
993 |
'form_var_datefield|add_hours:0 <= today', |
|
994 |
'form_var_datefield|add_hours:10 < today', |
|
995 |
'form_var_datefield|add_hours:876000 > today', # + 100 years |
|
996 |
'form_var_datefield|add_hours:876000 >= today', |
|
997 |
): |
|
998 |
condition = Condition({'type': 'django', 'value': condition_value}) |
|
999 |
assert condition.evaluate() is True |
|
1000 |
condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')}) |
|
1001 |
assert condition.evaluate() is True |
|
1002 | ||
1003 |
for condition_value in ( |
|
1004 |
'today|add_days:0 == today', |
|
1005 |
'today|add_hours:0 == today', |
|
1006 |
'now|add_hours:0 == now', |
|
1007 |
): |
|
1008 |
condition = Condition({'type': 'django', 'value': condition_value}) |
|
1009 |
assert condition.evaluate() is True |
|
1010 | ||
1011 |
for condition_value in ( |
|
1012 |
'today|add_days:2 >= today', |
|
1013 |
'today|add_days:2 > today', |
|
1014 |
'today|add_days:2 != today', |
|
1015 |
'not today|add_days:2 == today', |
|
1016 |
'not today|add_days:2 <= today', |
|
1017 |
'not today|add_days:2 < today', |
|
1018 |
'today|add_days:-2 <= today', |
|
1019 |
'today|add_days:-2 < today', |
|
1020 |
'today|add_days:-2 != today', |
|
1021 |
'not today|add_days:-2 >= today', |
|
1022 |
'not today|add_days:-2 > today', |
|
1023 |
'not today|add_days:-2 == today', |
|
1024 |
): |
|
1025 |
condition = Condition({'type': 'django', 'value': condition_value}) |
|
1026 |
assert condition.evaluate() is True |
|
1027 |
condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')}) |
|
1028 |
assert condition.evaluate() is True |
|
1029 | ||
947 | 1030 |
def test_lazy_templates(pub, variable_test_data): |
948 | 1031 |
context = pub.substitutions.get_context_variables(mode='lazy') |
949 | 1032 |
tmpl = Template('{{form_var_foo_foo}}') |
tests/test_templates.py | ||
---|---|---|
259 | 259 |
assert tmpl.render({'plop': {'foo': 'bar'}}) == '' |
260 | 260 |
assert tmpl.render() == '' |
261 | 261 | |
262 |
def test_date_maths(): |
|
263 |
tmpl = Template('{{ plop|add_days:4 }}') |
|
264 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 25, 2017' |
|
265 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 25, 2017' |
|
266 |
tmpl = Template('{{ plop|add_days:"-1" }}') |
|
267 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 20, 2017' |
|
268 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 20, 2017' |
|
269 |
tmpl = Template('{{ plop|add_days:1.5 }}') |
|
270 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017' |
|
271 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017' |
|
272 |
tmpl = Template('{{ plop|add_days:"1.5" }}') |
|
273 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017' |
|
274 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017' |
|
275 | ||
276 |
tmpl = Template('{{ plop|add_hours:24 }}') |
|
277 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017, midnight' |
|
278 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017, 6 p.m.' |
|
279 |
tmpl = Template('{{ plop|add_hours:"12.5" }}') |
|
280 |
assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 21, 2017, 12:30 p.m.' |
|
281 |
assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017, 6:30 a.m.' |
|
282 | ||
262 | 283 |
def test_variable_unicode_error_handling(): |
263 | 284 |
tmpl = Template('{{ form_var_éléphant }}') |
264 | 285 |
assert tmpl.render() == '' |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU General Public License |
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
import copy |
|
17 | 18 |
import datetime |
18 | 19 |
from decimal import Decimal |
19 | 20 | |
... | ... | |
130 | 131 |
value = parse_decimal(value) |
131 | 132 |
return defaultfilters.floatformat(value, arg=arg) |
132 | 133 | |
134 |
@register.filter(expects_localtime=True, is_safe=False) |
|
135 |
def add_days(value, arg): |
|
136 |
if hasattr(value, 'timetuple'): |
|
137 |
# extract real value in case of lazy object |
|
138 |
value = value.timetuple() |
|
139 |
value = parse_date(value) # consider only date, not hours |
|
140 |
if not value: |
|
141 |
return '' |
|
142 |
from wcs.variables import lazy_date |
|
143 |
arg = parse_decimal(arg) |
|
144 |
if not arg: |
|
145 |
return lazy_date(value) |
|
146 |
result = value + datetime.timedelta(days=float(arg)) |
|
147 |
if hasattr(result, 'date'): |
|
148 |
result = result.date() |
|
149 |
return lazy_date(result) |
|
150 | ||
151 |
@register.filter(expects_localtime=True, is_safe=False) |
|
152 |
def add_hours(value, arg): |
|
153 |
if hasattr(value, 'timetuple'): |
|
154 |
# extract real value in case of lazy object |
|
155 |
value = value.timetuple() |
|
156 |
value = parse_datetime(value) |
|
157 |
if not value: |
|
158 |
return '' |
|
159 |
from wcs.variables import lazy_date |
|
160 |
arg = parse_decimal(arg) |
|
161 |
if not arg: |
|
162 |
return lazy_date(value) |
|
163 |
return lazy_date(value + datetime.timedelta(hours=float(arg))) |
|
164 | ||
133 | 165 |
@register.simple_tag |
134 | 166 |
def standard_text(text_id): |
135 | 167 |
return mark_safe(TextsDirectory.get_html_text(str(text_id))) |
136 |
- |