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