From 0bba066527695a2a2e7bd39dddc57362aa2d7f65 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Mon, 7 Jan 2013 23:18:12 -0800 Subject: [PATCH] abstracted moment.js usages --- app/__init__.py | 3 +++ app/momentjs.py | 18 ++++++++++++++++++ app/templates/post.html | 5 +---- app/templates/user.html | 4 +--- 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 app/momentjs.py diff --git a/app/__init__.py b/app/__init__.py index 217210a..57bb3d2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,6 +5,7 @@ from flask.ext.login import LoginManager from flask.ext.openid import OpenID from flask.ext.mail import Mail from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD +from momentjs import momentjs app = Flask(__name__) app.config.from_object('config') @@ -35,5 +36,7 @@ if not app.debug: app.logger.setLevel(logging.INFO) app.logger.info('microblog startup') +app.jinja_env.globals['momentjs'] = momentjs + from app import views, models diff --git a/app/momentjs.py b/app/momentjs.py new file mode 100644 index 0000000..5d2db81 --- /dev/null +++ b/app/momentjs.py @@ -0,0 +1,18 @@ +from jinja2 import Markup + +class momentjs: + def __init__(self, timestamp): + self.timestamp = timestamp + + def render(self, format): + return Markup("" % (self.timestamp.strftime("%Y-%m-%dT%H:%M:%S Z"), format)) + + def format(self, fmt): + return self.render("format(\"%s\")" % fmt) + + def calendar(self): + return self.render("calendar()") + + def fromNow(self): + return self.render("fromNow()") + diff --git a/app/templates/post.html b/app/templates/post.html index 9543fda..0207904 100644 --- a/app/templates/post.html +++ b/app/templates/post.html @@ -2,10 +2,7 @@ -

{{post.author.nickname}} said - :

+

{{post.author.nickname}} said {{momentjs(post.timestamp).fromNow()}}:

{{post.body}}

diff --git a/app/templates/user.html b/app/templates/user.html index afc636f..a51edf5 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -10,9 +10,7 @@

{{user.nickname}}

{% if user.about_me %}

{{user.about_me}}

{% endif %} {% if user.last_seen %} -

Last seen: - -

+

Last seen: {{momentjs(user.last_seen).calendar()}}

{% endif %}

Followers: {{user.followers.count() - 1}} | Following: {{user.followed.count() - 1}} | {% if user.id == g.user.id %}