Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #877 - Updates date format #3240

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions cfgov/jinja2/v1/_includes/macros/time.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
{% macro render(datetime,
show_value_for={'date':true, 'time':true, 'timezone':true}) %}
<span class="datetime">
{% if show_value_for.date == true %}
{% if show_value_for.date == true %}
<time class="datetime_date" datetime="{{ dt.format_datetime(datetime) }}">
{{ dt.format_date(datetime) }}
{{ render_date_no_markup(datetime, show_value_for) }}
</time>
{% endif %}

Expand All @@ -41,3 +41,20 @@
{% endif %}
</span>
{% endmacro %}

{% macro render_date_no_markup(datetime,
Copy link
Contributor

@sebworks sebworks Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehhhh, I'm not crazy about it. @richaagarwal, do you know a better way to write this code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's awesome! Best code I've ever written! Look at all the brackets!

show_value_for={'date':true, 'time':true, 'timezone':true}) %}
{# TODO: Replace this with a custom Jinja method that uses
https://dateutil.readthedocs.io/ #}
{# Displays the date in the format: MMM. DD, YYYY #}
{% set format_month = '{dt:%b}'.format(dt = datetime) %}
{% if format_month == 'May' %}
{# May doesn't have a period. #}
{{ '{dt:%b} {dt.day}, {dt.year}'.format(dt = datetime) }}
{% elif format_month == 'Sep' %}
{# September is four letters. #}
{{ 'Sept. {dt.day}, {dt.year}'.format(dt = datetime) }}
{% else %}
{{ '{dt:%b}. {dt.day}, {dt.year}'.format(dt = datetime) }}
{% endif %}
Copy link
Member

@willbarton willbarton Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time thinking about this and looking at possible alternatives. I couldn't find a Python library that has built-in support for formatting months like this (conditionality around when a month is abbreviated and to what character-number).

At the very least, I don't think this should be in the template itself, it should be a filter or something similar that can be called from any template (particularly if this is the standardized way to refer to dates) and can be properly unittested.

It would also simplify the logic greatly to use a mapping (like Python's build-in datetime library) of month number to abbreviation instead of ifs/elses.

Copy link
Member

@willbarton willbarton Aug 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it further, a custom wrapper around strftime that either introduces our own format code or handles %b differently from the standard library would mean we could actually use date formatting here (instead of dt.month, dt.day, dt.year) and in all the other places in this changeset like we should be doing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all sounds well and good and over my head 😄

{% endmacro %}
3 changes: 2 additions & 1 deletion cfgov/jinja2/v1/_includes/organisms/bureau-structure.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ <h1>Bureau Structure</h1>
<p>
Last updated:
{# Displays the date in the format: MMM. DD, YYYY #}
{{'{dt:%b}. {dt.day}, {dt.year}'.format(dt = value.last_updated_date)}}
{% import 'macros/time.html' as time %}
{{ time.render_date_no_markup(value.last_updated_date) }}
</p>
{% endif %}
<div class="o-bureau-structure_chart">
Expand Down
4 changes: 3 additions & 1 deletion cfgov/jinja2/v1/_includes/organisms/post-preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
data-month="{{ event.start_dt | date('%b') }}"
data-day="{{ event.start_dt | date('%-d') }}"
datetime="{{ event.start_dt | date('%c') }}">
<span class="u-visually-hidden">{{ event.start_dt| date('%b %-d, %Y') }}</span>
<span class="u-visually-hidden">
{{ time.render_date_no_markup(event.start_dt) }}
</span>
</time>
{% if event.live_stream_link %}
<img class="o-post-preview_image"
Expand Down
7 changes: 5 additions & 2 deletions cfgov/jinja2/v1/ask-cfpb/answer-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% import 'tags.html' as tags with context %}
{% import 'form_block.html' as form_block with context %}
{% import 'macros/util/format/datetime.html' as dt %}
{% import 'macros/time.html' as time %}
{% import 'ask/ask-search-bar.html' as ask_search_bar with context %}

{% block title %}
Expand Down Expand Up @@ -37,7 +38,10 @@
block__flush-top
block__sub">
{% if last_edited %}
<time datetime='{{ last_edited }}' pubdate class="answer-edited-date">updated {{ dt.format_date(last_edited) }}</time>
<time datetime='{{ last_edited }}' pubdate class="answer-edited-date">
{# Displays the date in the format: MMM. DD, YYYY #}
updated {{ time.render_date_no_markup(last_edited) }}
</time>
{% endif %}

<h2>{{ page.question | striptags }}</h2>
Expand Down Expand Up @@ -97,7 +101,6 @@ <h3 class="answer-snippet">
</div>
{{ ask_search_bar.render( 'left' ) }}


{% endblock %}

{% block content_sidebar_modifiers -%}
Expand Down
9 changes: 8 additions & 1 deletion cfgov/jobmanager/models/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ def make_grades_value(self, instance, value):
return ', '.join(sorted(g.grade.grade for g in value.all()))

def make_close_date_value(self, instance, value):
return value.strftime('%b %d, %Y').upper()
formatted_month = value.strftime('%b')
if formatted_month != 'May' and formatted_month != 'Sep':
formatted_month = formatted_month + '.'
elif formatted_month == 'Sep':
formatted_month = 'Sept.'
formatted_day = value.strftime('%d').lstrip('0')
formatted_year = value.strftime('%Y')
return formatted_month + ' ' + formatted_day + ', ' + formatted_year
46 changes: 41 additions & 5 deletions cfgov/jobmanager/tests/models/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def test_html_formatting(self):
'<li class="m-list_item">'
'<a class="m-list_link" href=".*">Assistant</a>'
'<p class="a-date">CLOSING<span class="datetime">'
'.*APR 21, 2099.*</span></p>'
'.*Apr. 21, 2099.*</span></p>'
'</li>'
'<li class="m-list_item">'
'<a class="m-list_link" href=".*">Manager</a>'
'<p class="a-date">CLOSING<span class="datetime">.'
'*AUG 05, 2099.*</span></p>'
'*Aug. 5, 2099.*</span></p>'
'</li>'
))

Expand Down Expand Up @@ -206,13 +206,13 @@ def test_html_formatting(self):
'<tr>'
'<td data-label="TITLE"><a class="" href=".*">Assistant</a></td>'
'<td data-label="GRADE">12</td>'
'<td data-label="POSTING CLOSES">APR 21, 2099</td>'
'<td data-label="POSTING CLOSES">Apr. 21, 2099</td>'
'<td data-label="REGION">Silicon Valley</td>'
'</tr>'
'<tr>'
'<td data-label="TITLE"><a class="" href=".*">Manager</a></td>'
'<td data-label="GRADE">1, 2, 3</td>'
'<td data-label="POSTING CLOSES">AUG 05, 2099</td>'
'<td data-label="POSTING CLOSES">Aug. 5, 2099</td>'
'<td data-label="REGION">Silicon Valley</td>'
'</tr>'
))
Expand All @@ -230,7 +230,43 @@ def test_html_formatting_no_grade(self):
'<tr>'
'<td data-label="TITLE"><a class="" href=".*">CEO</a></td>'
'<td data-label="GRADE"></td>'
'<td data-label="POSTING CLOSES">DEC 01, 2099</td>'
'<td data-label="POSTING CLOSES">Dec. 1, 2099</td>'
'<td data-label="REGION">Silicon Valley</td>'
'</tr>'
))

def test_html_formatting_no_grade_may_date(self):
make_job_listing_page(
title='CEO',
close_date=date(2099, 5, 1)
)

table = JobListingTable()
html = table.render(table.to_python({}))

self.assertHtmlRegexpMatches(html, (
'<tr>'
'<td data-label="TITLE"><a class="" href=".*">CEO</a></td>'
'<td data-label="GRADE"></td>'
'<td data-label="POSTING CLOSES">May 1, 2099</td>'
'<td data-label="REGION">Silicon Valley</td>'
'</tr>'
))

def test_html_formatting_no_grade_sept_date(self):
make_job_listing_page(
title='CEO',
close_date=date(2099, 9, 1)
)

table = JobListingTable()
html = table.render(table.to_python({}))

self.assertHtmlRegexpMatches(html, (
'<tr>'
'<td data-label="TITLE"><a class="" href=".*">CEO</a></td>'
'<td data-label="GRADE"></td>'
'<td data-label="POSTING CLOSES">Sept. 1, 2099</td>'
'<td data-label="REGION">Silicon Valley</td>'
'</tr>'
))
Expand Down
2 changes: 1 addition & 1 deletion cfgov/unprocessed/css/calendar-icon.less
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- name: Calendar Icon
markup: |
<time class="calendar-icon" data-month="Jan" data-day="21" datetime="2015-01-21">
<span class="u-visually-hidden">Jan 21, 2015</span>
<span class="u-visually-hidden">Jan. 21, 2015</span>
</time>
denotes:
- |
Expand Down
2 changes: 1 addition & 1 deletion cfgov/unprocessed/css/organisms/item-introduction.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<p class="short-desc">…</p>
<p class="short-desc">
By <a href="#">AuthorA</a> -
<span><time>DEC 24, 2015</time></span>
<span><time>DEC. 24, 2015</time></span>
</p>
<div>
[social media molecule]
Expand Down
2 changes: 1 addition & 1 deletion cfgov/unprocessed/css/organisms/post-preview.less
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Published
<span class="datetime">
<time class="datetime_date" datetime="2003-08-05T21:36:11.000000-0400">
AUG 05, 2003
AUG. 5, 2003
</time>
</span>
</span>
Expand Down