Skip to content

Commit

Permalink
default format (Fixes #48)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobthetechy authored and miguelgrinberg committed Jun 16, 2019
1 parent 1b273c4 commit 8786663
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ Function Reference

The supported list of display functions is shown below:

- `moment(timestamp=None, local=False).format(format_string)`
- `moment(timestamp=None, local=False).fromNow(no_suffix = False)`
- `moment(timestamp=None, local=False).fromTime(another_timesatmp, no_suffix = False)`
- `moment(timestamp=None, local=False).format(format_string=None)`
- `moment(timestamp=None, local=False).fromNow(no_suffix=False)`
- `moment(timestamp=None, local=False).fromTime(another_timesatmp, no_suffix=False)`
- `moment(timestamp=None, local=False).calendar()`
- `moment(timestamp=None, local=False).valueOf()`
- `moment(timestamp=None, local=False).unix()`
Expand All @@ -69,6 +69,11 @@ Auto-Refresh

All the display functions take an optional `refresh` argument that when set to `True` will re-render timestamps every minute. This can be useful for relative time formats such as the one returned by the `fromNow()` or `fromTime()` functions. By default refreshing is disabled.

Default Format
--------------

The `format()` function can be invoked without arguments, in which case a default format of ISO8601 defined by the moment.js library is used. If you want to set a different default, you can set the `MOMENT_DEFAULT_FORMAT` variable in the Flask configuration. Consult the [moment.js format documentation](http://momentjs.com/docs/#/displaying/format/) for a list of acepted tokens.

Internationalization
--------------------

Expand Down
12 changes: 8 additions & 4 deletions flask_moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ def include_moment(version=default_moment_version, local_js=None,
'crossorigin="anonymous"></script>\n' \
% (version, js_filename, sri)

default_format = ''
if 'MOMENT_DEFAULT_FORMAT' in current_app.config:
default_format = '\nmoment.defaultFormat = "%s";' % \
current_app.config['MOMENT_DEFAULT_FORMAT']
return Markup('''%s<script>
moment.locale("en");
moment.locale("en");%s
function flask_moment_render(elem) {
$(elem).text(eval('moment("' + $(elem).data('timestamp') + '").' + $(elem).data('format') + ';'));
$(elem).removeClass('flask-moment').show();
Expand All @@ -59,7 +63,7 @@ def include_moment(version=default_moment_version, local_js=None,
$(document).ready(function() {
flask_moment_render_all();
});
</script>''' % js) # noqa: E501
</script>''' % (js, default_format)) # noqa: E501

@staticmethod
def include_jquery(version=default_jquery_version, local_js=None,
Expand Down Expand Up @@ -119,8 +123,8 @@ def _render(self, format, refresh=False):
'style="display: none">%s</span>') %
(t, format, int(refresh) * 60000, t))

def format(self, fmt, refresh=False):
return self._render("format('%s')" % fmt, refresh)
def format(self, fmt=None, refresh=False):
return self._render("format('%s')" % (fmt or ''), refresh)

def fromNow(self, no_suffix=False, refresh=False):
return self._render("fromNow(%s)" % int(no_suffix), refresh)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_flask_moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ def test_include_moment_renders_properly(self, app, moment):
ts = str(render_template_string("{{ moment.include_moment() }}"))

assert "<script" in ts
assert default_moment_version + "/moment-with-locales.min.js" in str(
ts)
assert default_moment_version + "/moment-with-locales.min.js" in ts
assert 'moment.defaultFormat' not in ts

def test_include_moment_with_default(self, app, moment):
app.config['MOMENT_DEFAULT_FORMAT'] = 'foo'
ts = str(render_template_string("{{ moment.include_moment() }}"))
assert "<script" in ts
assert default_moment_version + "/moment-with-locales.min.js" in ts
assert 'moment.defaultFormat = "foo";' in ts

def test_include_jquery_default(self):
include_jquery = _moment.include_jquery()
Expand Down

0 comments on commit 8786663

Please sign in to comment.