Skip to content

Commit

Permalink
fixed #2 use of secure URLs when request is secure
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Feb 13, 2014
1 parent 71ae78f commit 0ee69da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Step 2: In your `<head>` section of your base template add the following code:
{{ moment.include_moment() }}
</head>

Note that jQuery is required. If you are already including it on your own then you can remove the `include_jquery()` line.
Note that jQuery is required. If you are already including it on your own then you can remove the `include_jquery()` line. These inclusions are done of secure HTTP if the request under which they are executed is secure.

Step 3: Render timestamps in your template. For example:

Expand Down
14 changes: 11 additions & 3 deletions flask_moment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from datetime import datetime
import json
from jinja2 import Markup
from flask import current_app
from flask import current_app, request

class _moment(object):
@staticmethod
def include_moment(version = '2.3.1'):
if version is not None:
js = '<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/%s/moment-with-langs.min.js"></script>\n' % version
if request.is_secure:
protocol = 'https'
else:
protocol = 'http'
js = '<script src="%s://cdnjs.cloudflare.com/ajax/libs/moment.js/%s/moment-with-langs.min.js"></script>\n' % (protocol, version)
return Markup('''%s<script>
function flask_moment_render(elem) {
$(elem).text(eval('moment("' + $(elem).data('timestamp') + '").' + $(elem).data('format') + ';'));
Expand All @@ -28,7 +32,11 @@ def include_moment(version = '2.3.1'):

@staticmethod
def include_jquery(version = '1.10.1'):
return Markup('<script src="http://code.jquery.com/jquery-%s.min.js"></script>' % version)
if request.is_secure:
protocol = 'https'
else:
protocol = 'http'
return Markup('<script src="%s://code.jquery.com/jquery-%s.min.js"></script>' % (protocol, version))

@staticmethod
def lang(language):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='Flask-Moment',
version='0.2.0',
version='0.2.1',
url='http://github.com/miguelgrinberg/flask-moment/',
license='MIT',
author='Miguel Grinberg',
Expand Down

0 comments on commit 0ee69da

Please sign in to comment.