abstracted moment.js usages

This commit is contained in:
Miguel Grinberg 2013-01-07 23:18:12 -08:00
parent 39e7521d3b
commit 0bba066527
4 changed files with 23 additions and 7 deletions

View File

@ -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

18
app/momentjs.py Normal file
View File

@ -0,0 +1,18 @@
from jinja2 import Markup
class momentjs:
def __init__(self, timestamp):
self.timestamp = timestamp
def render(self, format):
return Markup("<script>\ndocument.write(moment(\"%s\").%s);\n</script>" % (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()")

View File

@ -2,10 +2,7 @@
<tr>
<td width="70px"><a href="{{url_for('user', nickname = post.author.nickname)}}"><img src="{{post.author.avatar(70)}}" /></a></td>
<td>
<p><a href="{{url_for('user', nickname = post.author.nickname)}}">{{post.author.nickname}}</a> said
<script>
document.write(moment("{{post.timestamp.strftime("%Y-%m-%dT%H:%M:%S Z")}}").fromNow());
</script>:</p>
<p><a href="{{url_for('user', nickname = post.author.nickname)}}">{{post.author.nickname}}</a> said {{momentjs(post.timestamp).fromNow()}}:</p>
<p><strong>{{post.body}}</strong></p>
</td>
</tr>

View File

@ -10,9 +10,7 @@
<h1>{{user.nickname}}</h1>
{% if user.about_me %}<p>{{user.about_me}}</p>{% endif %}
{% if user.last_seen %}
<p><em>Last seen:
<script>document.write(moment("{{user.last_seen.strftime("%Y-%m-%dT%H:%M:%S Z")}}").calendar());</script>
</em></p>
<p><em>Last seen: {{momentjs(user.last_seen).calendar()}}</em></p>
{% endif %}
<p>Followers: {{user.followers.count() - 1}} | Following: {{user.followed.count() - 1}} |
{% if user.id == g.user.id %}