From 562124e77d345b2731a4277a934e5649a350c455 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 9 Jul 2019 19:08:41 +0200
Subject: [PATCH] misc: format date in a parseable format by default (#34703)

---
 tests/test_templates.py    | 57 +++++++++++++++++++++++---------------
 wcs/formats/__init__.py    |  0
 wcs/formats/en/__init__.py |  0
 wcs/formats/en/formats.py  |  2 ++
 wcs/formats/fr/__init__.py |  0
 wcs/formats/fr/formats.py  |  2 ++
 wcs/settings.py            |  3 ++
 7 files changed, 42 insertions(+), 22 deletions(-)
 create mode 100644 wcs/formats/__init__.py
 create mode 100644 wcs/formats/en/__init__.py
 create mode 100644 wcs/formats/en/formats.py
 create mode 100644 wcs/formats/fr/__init__.py
 create mode 100644 wcs/formats/fr/formats.py

diff --git a/tests/test_templates.py b/tests/test_templates.py
index b0a0a3aaa..b3049ded9 100644
--- a/tests/test_templates.py
+++ b/tests/test_templates.py
@@ -4,6 +4,7 @@ import datetime
 import pytest
 import string
 
+from django.test import override_settings
 from quixote import cleanup
 from qommon.template import Template, TemplateError
 from wcs.conditions import Condition
@@ -145,10 +146,15 @@ def test_template_encoding():
 
 def test_datetime_templatetags():
     tmpl = Template('{{ plop|datetime }}')
-    assert tmpl.render({'plop': '2017-12-21 10:32'}) == 'Dec. 21, 2017, 10:32 a.m.'
-    assert tmpl.render({'plop': '21/12/2017 10h32'}) == 'Dec. 21, 2017, 10:32 a.m.'
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 21, 2017, midnight'
-    assert tmpl.render({'plop': '21/12/2017'}) == 'Dec. 21, 2017, midnight'
+    assert tmpl.render({'plop': '2017-12-21 10:32'}) == '2017-12-21 10:32'
+    assert tmpl.render({'plop': '21/12/2017 10h32'}) == '2017-12-21 10:32'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 00:00'
+    assert tmpl.render({'plop': '21/12/2017'}) == '2017-12-21 00:00'
+    with override_settings(LANGUAGE_CODE='fr-fr'):
+        assert tmpl.render({'plop': '2017-12-21 10:32'}) == '21/12/2017 10:32'
+        assert tmpl.render({'plop': '21/12/2017 10h32'}) == '21/12/2017 10:32'
+        assert tmpl.render({'plop': '2017-12-21'}) == '21/12/2017 00:00'
+        assert tmpl.render({'plop': '21/12/2017'}) == '21/12/2017 00:00'
     assert tmpl.render({'plop': '10h32'}) == ''
     assert tmpl.render({'plop': 'x'}) == ''
     assert tmpl.render({'plop': None}) == ''
@@ -179,12 +185,19 @@ def test_datetime_templatetags():
     assert tmpl.render({'d2': '2017-12-22'}) == 'd1<=d2'
 
     tmpl = Template('{{ plop|date }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 21, 2017'
-    assert tmpl.render({'plop': '21/12/2017'}) == 'Dec. 21, 2017'
-    assert tmpl.render({'plop': '2017-12-21 10:32'}) == 'Dec. 21, 2017'
-    assert tmpl.render({'plop': '21/12/2017 10:32'}) == 'Dec. 21, 2017'
-    assert tmpl.render({'plop': '21/12/2017 10h32'}) == 'Dec. 21, 2017'
-    assert tmpl.render({'plop': '21/12/2017 10:32:42'}) == 'Dec. 21, 2017'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21'
+    assert tmpl.render({'plop': '21/12/2017'}) == '2017-12-21'
+    assert tmpl.render({'plop': '2017-12-21 10:32'}) == '2017-12-21'
+    assert tmpl.render({'plop': '21/12/2017 10:32'}) == '2017-12-21'
+    assert tmpl.render({'plop': '21/12/2017 10h32'}) == '2017-12-21'
+    assert tmpl.render({'plop': '21/12/2017 10:32:42'}) == '2017-12-21'
+    with override_settings(LANGUAGE_CODE='fr-fr'):
+        assert tmpl.render({'plop': '2017-12-21'}) == '21/12/2017'
+        assert tmpl.render({'plop': '21/12/2017'}) == '21/12/2017'
+        assert tmpl.render({'plop': '2017-12-21 10:32'}) == '21/12/2017'
+        assert tmpl.render({'plop': '21/12/2017 10:32'}) == '21/12/2017'
+        assert tmpl.render({'plop': '21/12/2017 10h32'}) == '21/12/2017'
+        assert tmpl.render({'plop': '21/12/2017 10:32:42'}) == '21/12/2017'
     assert tmpl.render({'plop': '10:32'}) == ''
     assert tmpl.render({'plop': 'x'}) == ''
     assert tmpl.render({'plop': None}) == ''
@@ -263,24 +276,24 @@ def test_datetime_templatetags():
 
 def test_date_maths():
     tmpl = Template('{{ plop|add_days:4 }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 25, 2017'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 25, 2017'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-25'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-25'
     tmpl = Template('{{ plop|add_days:"-1" }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 20, 2017'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 20, 2017'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-20'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-20'
     tmpl = Template('{{ plop|add_days:1.5 }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-22'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22'
     tmpl = Template('{{ plop|add_days:"1.5" }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-22'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22'
 
     tmpl = Template('{{ plop|add_hours:24 }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 22, 2017, midnight'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017, 6 p.m.'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-22 00:00'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22 18:00'
     tmpl = Template('{{ plop|add_hours:"12.5" }}')
-    assert tmpl.render({'plop': '2017-12-21'}) == 'Dec. 21, 2017, 12:30 p.m.'
-    assert tmpl.render({'plop': '2017-12-21 18:00'}) == 'Dec. 22, 2017, 6:30 a.m.'
+    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 12:30'
+    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22 06:30'
 
 def test_variable_unicode_error_handling():
     tmpl = Template('{{ form_var_éléphant }}')
diff --git a/wcs/formats/__init__.py b/wcs/formats/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wcs/formats/en/__init__.py b/wcs/formats/en/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wcs/formats/en/formats.py b/wcs/formats/en/formats.py
new file mode 100644
index 000000000..1362306ce
--- /dev/null
+++ b/wcs/formats/en/formats.py
@@ -0,0 +1,2 @@
+DATETIME_FORMAT = 'Y-m-d H:i'
+DATE_FORMAT = "Y-m-d"
diff --git a/wcs/formats/fr/__init__.py b/wcs/formats/fr/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wcs/formats/fr/formats.py b/wcs/formats/fr/formats.py
new file mode 100644
index 000000000..99222ed10
--- /dev/null
+++ b/wcs/formats/fr/formats.py
@@ -0,0 +1,2 @@
+DATETIME_FORMAT = 'd/m/Y H:i'
+DATE_FORMAT = "d/m/Y"
diff --git a/wcs/settings.py b/wcs/settings.py
index 8bbc57740..67d4b145f 100644
--- a/wcs/settings.py
+++ b/wcs/settings.py
@@ -126,6 +126,9 @@ ROOT_URLCONF = 'wcs.urls'
 # Python dotted path to the WSGI application used by Django's runserver.
 WSGI_APPLICATION = 'wcs.wsgi.application'
 
+# custom date formats
+FORMAT_MODULE_PATH = 'wcs.formats'
+
 INSTALLED_APPS = (
     #'django.contrib.auth',
     #'django.contrib.contenttypes',
-- 
2.22.0

