flask_moment.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from distutils.version import StrictVersion
  2. from datetime import datetime
  3. from jinja2 import Markup
  4. from flask import current_app
  5. class _moment(object):
  6. @staticmethod
  7. def include_moment(version='2.10.3', local_js=None):
  8. js = ''
  9. if local_js is not None:
  10. js = '<script src="%s"></script>\n' % local_js
  11. elif version is not None:
  12. js_filename = 'moment-with-locales.min.js' \
  13. if StrictVersion(version) >= StrictVersion('2.8.0') \
  14. else 'moment-with-langs.min.js'
  15. js = '<script src="//cdnjs.cloudflare.com/ajax/libs/' \
  16. 'moment.js/%s/%s"></script>\n' % (version, js_filename)
  17. return Markup('''%s<script>
  18. moment.locale("en");
  19. function flask_moment_render(elem) {
  20. $(elem).text(eval('moment("' + $(elem).data('timestamp') + '").' + $(elem).data('format') + ';'));
  21. $(elem).removeClass('flask-moment').show();
  22. }
  23. function flask_moment_render_all() {
  24. $('.flask-moment').each(function() {
  25. flask_moment_render(this);
  26. if ($(this).data('refresh')) {
  27. (function(elem, interval) { setInterval(function() { flask_moment_render(elem) }, interval); })(this, $(this).data('refresh'));
  28. }
  29. })
  30. }
  31. $(document).ready(function() {
  32. flask_moment_render_all();
  33. });
  34. </script>''' % js)
  35. @staticmethod
  36. def include_jquery(version='2.1.0', local_js=None):
  37. js = ''
  38. if local_js is not None:
  39. js = '<script src="%s"></script>\n' % local_js
  40. else:
  41. js = ('<script src="//code.jquery.com/' +
  42. 'jquery-%s.min.js"></script>') % version
  43. return Markup(js)
  44. @staticmethod
  45. def locale(language):
  46. return Markup('<script>\nmoment.locale("%s");\n</script>' % language)
  47. @staticmethod
  48. def lang(language):
  49. return _moment.locale(language)
  50. def __init__(self, timestamp=None, local=False):
  51. if timestamp is None:
  52. timestamp = datetime.utcnow()
  53. self.timestamp = timestamp
  54. self.local = local
  55. def _timestamp_as_iso_8601(self, timestamp):
  56. tz = ''
  57. if not self.local:
  58. tz = 'Z'
  59. return timestamp.strftime('%Y-%m-%dT%H:%M:%S' + tz)
  60. def _render(self, format, refresh=False):
  61. t = self._timestamp_as_iso_8601(self.timestamp)
  62. return Markup(('<span class="flask-moment" data-timestamp="%s" ' +
  63. 'data-format="%s" data-refresh="%d" ' +
  64. 'style="display: none">%s</span>') %
  65. (t, format, int(refresh) * 60000, t))
  66. def format(self, fmt, refresh=False):
  67. return self._render("format('%s')" % fmt, refresh)
  68. def fromNow(self, no_suffix=False, refresh=False):
  69. return self._render("fromNow(%s)" % int(no_suffix), refresh)
  70. def fromTime(self, timestamp, no_suffix=False, refresh=False):
  71. return self._render("from(moment('%s'),%s)" %
  72. (self._timestamp_as_iso_8601(timestamp),
  73. int(no_suffix)), refresh)
  74. def calendar(self, refresh=False):
  75. return self._render("calendar()", refresh)
  76. def valueOf(self, refresh=False):
  77. return self._render("valueOf()", refresh)
  78. def unix(self, refresh=False):
  79. return self._render("unix()", refresh)
  80. class Moment(object):
  81. def __init__(self, app=None):
  82. if app is not None:
  83. self.init_app(app)
  84. def init_app(self, app):
  85. if not hasattr(app, 'extensions'):
  86. app.extensions = {}
  87. app.extensions['moment'] = _moment
  88. app.context_processor(self.context_processor)
  89. @staticmethod
  90. def context_processor():
  91. return {
  92. 'moment': current_app.extensions['moment']
  93. }
  94. def create(self, timestamp=None):
  95. return current_app.extensions['moment'](timestamp)