Skip to content

Commit

Permalink
[master,ux][s]: improve rendering of datetime in frontend by updating…
Browse files Browse the repository at this point in the history
… h.render_datetime (now like Feb 10, 2010).

* Also have generic option to add hours/mins (with_hours)
* Default is now without hours/min so had to update various templates to reflect this (and a few tests)
  • Loading branch information
rufuspollock committed Feb 8, 2012
1 parent 072786c commit ed6ddf0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
6 changes: 5 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -281,11 +281,15 @@ def pager(self, *args, **kwargs):
)
return super(Page, self).pager(*args, **kwargs)

def render_datetime(datetime_, date_format='%Y-%m-%d %H:%M'):
def render_datetime(datetime_, date_format=None, with_hours=False):
'''Render a datetime object or timestamp string as a pretty string
(Y-m-d H:m).
If timestamp is badly formatted, then a blank string is returned.
'''
if not date_format:
date_format = '%b %d, %Y'
if with_hours:
date_format += ', %H:%M'
if isinstance(datetime_, datetime.datetime):
return datetime_.strftime(date_format)
elif isinstance(datetime_, basestring):
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/_util.html
Expand Up @@ -384,7 +384,7 @@
</div>
</py:if>
</td>
<td>${h.render_datetime(revision.timestamp)}</td>
<td>${h.render_datetime(revision.timestamp, with_hours=True)}</td>
<td>${h.linked_user(revision.author)}</td>
<td>
<py:for each="pkg in revision.packages">
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/history.html
Expand Up @@ -47,7 +47,7 @@ <h3 py:if="c.error" class="form-errors">
<a href="${h.url_for(controller='revision',action='read',id=rev['id'])}">${rev['id'][:4]}&#8230;</a>
</td>
<td>
<a href="${h.url_for(controller='package',action='read',id='%s@%s' % (c.pkg_dict['name'], rev['timestamp']))}" title="${'Read dataset as of %s' % rev['timestamp']}">${h.render_datetime(rev['timestamp'])}</a></td>
<a href="${h.url_for(controller='package',action='read',id='%s@%s' % (c.pkg_dict['name'], rev['timestamp']))}" title="${'Read dataset as of %s' % rev['timestamp']}">${h.render_datetime(rev['timestamp'], with_hours=True)}</a></td>
<td>${h.linked_user(rev['author'])}</td>
<td>${rev['message']}</td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/read.html
Expand Up @@ -84,8 +84,8 @@ <h3>Related Datasets</h3>
<div py:match="content">
<py:if test="c.pkg_revision_id">
<div id="revision" class="widget-container">
<p py:if="c.pkg_revision_not_latest">This is an old revision of this dataset, as edited <!--!by ${h.linked_user(rev.author)}-->at ${h.render_datetime(c.pkg_revision_timestamp)}. It may differ significantly from the <a href="${url(controller='package', action='read', id=c.pkg.name)}">current revision</a>.</p>
<p py:if="not c.pkg_revision_not_latest">This is the current revision of this dataset, as edited <!--!by ${h.linked_user(rev.author)}-->at ${h.render_datetime(c.pkg_revision_timestamp)}.</p>
<p py:if="c.pkg_revision_not_latest">This is an old revision of this dataset, as edited <!--!by ${h.linked_user(rev.author)}-->at ${h.render_datetime(c.pkg_revision_timestamp, with_hours=True)}. It may differ significantly from the <a href="${url(controller='package', action='read', id=c.pkg.name)}">current revision</a>.</p>
<p py:if="not c.pkg_revision_not_latest">This is the current revision of this dataset, as edited <!--!by ${h.linked_user(rev.author)}-->at ${h.render_datetime(c.pkg_revision_timestamp, with_hours=True)}.</p>
</div>
</py:if>

Expand Down
7 changes: 3 additions & 4 deletions ckan/tests/functional/test_package.py
Expand Up @@ -503,7 +503,7 @@ def test_read_revision1(self):
side_html = self.named_div('sidebar', res)
print 'MAIN', main_html
assert 'This is an old revision of this dataset' in main_html
assert 'at 2011-01-01 00:00' in main_html
assert 'at Jan 01, 2011, 00:00' in main_html
self.check_named_element(main_html, 'a', 'href="/dataset/%s"' % self.pkg_name)
print 'PKG', pkg_html
assert 'title1' in res
Expand All @@ -521,7 +521,7 @@ def test_read_revision2(self):
side_html = self.named_div('sidebar', res)
print 'MAIN', main_html
assert 'This is an old revision of this dataset' in main_html
assert 'at 2011-01-02 00:00' in main_html
assert 'at Jan 02, 2011, 00:00' in main_html
self.check_named_element(main_html, 'a', 'href="/dataset/%s"' % self.pkg_name)
print 'PKG', pkg_html
assert 'title2' in res
Expand All @@ -540,7 +540,7 @@ def test_read_revision3(self):
print 'MAIN', main_html
assert 'This is an old revision of this dataset' not in main_html
assert 'This is the current revision of this dataset' in main_html
assert 'at 2011-01-03 00:00' in main_html
assert 'at Jan 03, 2011, 00:00' in main_html
self.check_named_element(main_html, 'a', 'href="/dataset/%s"' % self.pkg_name)
print 'PKG', pkg_html
assert 'title3' in res
Expand Down Expand Up @@ -1421,7 +1421,6 @@ def test_4_history_revision_package_link(self):
res = res.click(href=url)
main_html = self.main_div(res)
assert 'This is an old revision of this dataset' in main_html
assert 'at %s' % str(self.revision_timestamps[1])[:6] in main_html


class TestMarkdownHtmlWhitelist(TestPackageForm):
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -22,11 +22,11 @@ def test_extract_markdown(self):

def test_render_datetime(self):
res = h.render_datetime(datetime.datetime(2008, 4, 13, 20, 40, 20, 123456))
assert_equal(res, '2008-04-13 20:40')
assert_equal(res, 'Apr 13, 2008')

def test_render_datetime_but_from_string(self):
res = h.render_datetime('2008-04-13T20:40:20.123456')
assert_equal(res, '2008-04-13 20:40')
assert_equal(res, 'Apr 13, 2008')

def test_render_datetime_blank(self):
res = h.render_datetime(None)
Expand Down

0 comments on commit ed6ddf0

Please sign in to comment.