Skip to content

Commit

Permalink
Renomme et change le comportement du templatetag humane_time
Browse files Browse the repository at this point in the history
  • Loading branch information
philippemilink committed Jul 26, 2023
1 parent 44468ca commit 63579f0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
11 changes: 6 additions & 5 deletions doc/source/front-end/template-tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,24 @@ Ce filtre formate une date au format ``DateTime`` destiné à être affiché sur

Ce filtre effectue la même chose que ``format_date`` mais à destination des ``tooltip``.

``humane_time``
``date_from_timestamp``
---------------

Formate une date au format *Nombre de seconde depuis Epoch* en un élément lisible. Ainsi :
Convertit une date au format *Nombre de seconde depuis Epoch* en un objet
accepté par les autres filtres de ce module. Ainsi :

.. sourcecode:: html+django

{% load date %}
{{ date_epoch|humane_time }}
{{ date_epoch|date_from_timestamp|format_date }}

sera rendu :

.. sourcecode:: text

jeudi 01 janvier 1970 à 00h00
jeudi 01 janvier 1970 à 00h02

…si le contenu de ``date_epoch`` était de ``42``.
…si le contenu de ``date_epoch`` était de ``122``.

``from_elasticsearch_date``
---------------------------
Expand Down
4 changes: 2 additions & 2 deletions templates/tutorialv2/view/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h2 class="subtitle">
{% endif %}
</td>
<td>
{{ commit.authored_date|humane_time }}
{{ commit.authored_date|date_from_timestamp|format_date }}
</td>
<td>
<a href="{% url "content:view" content.pk content.slug %}?version={{ commit.hexsha }}" >
Expand Down Expand Up @@ -161,7 +161,7 @@ <h2 class="subtitle">
{% trans "mettre à jour" %}
{% endif %}
{% endcaptureas %}
{% blocktrans with action=action date_version=commit.authored_date|humane_time content_title=content.title %}
{% blocktrans with action=action date_version=commit.authored_date|date_from_timestamp|format_date content_title=content.title %}
Êtes-vous certain de vouloir <strong>{{ action }}</strong> la bêta pour le contenu
"<em>{{ content_title }}</em>" dans sa version de {{ date_version }} ?
{% endblocktrans %}
Expand Down
7 changes: 4 additions & 3 deletions zds/utils/templatetags/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ def tooltip_date(value):


@register.filter
def humane_time(timestamp):
"""Render time (number of second from epoch) to an human readable string"""
return format_date(datetime.fromtimestamp(timestamp))
def date_from_timestamp(timestamp):
"""Convert a timestamp (number of second from epoch) to a datetime object,
another filter should then be used to format the datetime object."""
return datetime.fromtimestamp(timestamp)


@register.filter
Expand Down
4 changes: 2 additions & 2 deletions zds/utils/tests/tests_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_tooltip_date(self):
tr = Template("{% load date %}" "{{ NoneVal | tooltip_date }}").render(self.context)
self.assertEqual("None", tr)

def test_humane_time(self):
def test_date_from_timestamp(self):
# Default behaviour
tr = Template("{% load date %}" "{{ date_epoch | humane_time }}").render(self.context)
tr = Template("{% load date %}" "{{ date_epoch | date_from_timestamp | format_date }}").render(self.context)

self.assertEqual(tr, "jeudi 01 janvier 1970 à 01h00")

0 comments on commit 63579f0

Please sign in to comment.