Permalink
Browse files

return URLs with https scheme when requested so

  • Loading branch information...
1 parent 3432d2b commit fe7212ecb47cb397b2f93fca6f8d284c1006d5c6 @posativ committed Aug 30, 2012
Showing with 21 additions and 16 deletions.
  1. +8 −9 regenwolken/specs.py
  2. +5 −0 regenwolken/utils.py
  3. +8 −7 regenwolken/views.py
View
17 regenwolken/specs.py
@@ -30,7 +30,7 @@
markdown = None
-def Item(obj, conf, **kw):
+def Item(obj, conf, scheme='http'):
"""JSON-compatible dict representing Item.
href: used for renaming -> http://developer.getcloudapp.com/rename-item
@@ -56,26 +56,26 @@ def Item(obj, conf, **kw):
obj = Struct(**obj)
result = {
- "href": "http://%s/items/%s" % (conf['HOSTNAME'], obj._id),
+ "href": "%s://%s/items/%s" % (scheme, conf['HOSTNAME'], obj._id),
"private": obj.private,
"subscribed": True,
"item_type": obj.item_type,
"view_counter": obj.view_counter,
- "icon": "http://%s/images/item_types/%s.png" % (conf['HOSTNAME'], obj.item_type),
+ "icon": "%s://%s/images/item_types/%s.png" % (scheme, conf['HOSTNAME'], obj.item_type),
"source": obj.source,
"created_at": strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()),
"updated_at": strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()),
"deleted_at": None }
if obj.item_type == 'bookmark':
x['name'] = obj.name
- x['url'] = 'http://' + conf['HOSTNAME'] + '/' + obj.short_id
+ x['url'] = scheme + '://' + conf['HOSTNAME'] + '/' + obj.short_id
x['content_url'] = x['url'] + '/content'
x['remote_url'] = None
x['redirect_url'] = obj.redirect_url
else:
x['name'] = obj.filename
- x['url'] = 'http://' + conf['HOSTNAME'] + '/' + obj.short_id
+ x['url'] = scheme + '://' + conf['HOSTNAME'] + '/' + obj.short_id
x['content_url'] = x['url'] + '/' + secure_filename(obj.filename)
x['remote_url'] = x['url'] + '/' + url_quote(obj.filename)
x['thumbnail_url'] = x['url'] # TODO: thumbails
@@ -86,12 +86,11 @@ def Item(obj, conf, **kw):
x['updated_at'] = obj.updated_at
x['deleted_at'] = obj.deleted_at
if obj.deleted_at:
- x['icon'] = "http://%s/images/item_types/trash.png" % conf['HOSTNAME']
+ x['icon'] = scheme + "://" + conf['HOSTNAME'] + "/images/item_types/trash.png"
except AttributeError:
pass
result.update(x)
- result.update(kw)
return result
@@ -135,7 +134,7 @@ def Account(account, conf, **kw):
class Drop:
"""Drop class which renders item-specific layouts."""
- def __init__(self, drop, conf):
+ def __init__(self, drop, conf, scheme):
def guess_type(filename):
try:
@@ -147,7 +146,7 @@ def guess_type(filename):
return 'text'
return 'other'
- self.__dict__.update(Item(drop, conf))
+ self.__dict__.update(Item(drop, conf, scheme))
self.drop = drop
self.item_type = guess_type(self.filename)
self.url = self.__dict__['content_url']
View
5 regenwolken/utils.py
@@ -19,6 +19,11 @@
ImageFile = None
+def urlscheme(url):
+ """return the current scheme (HTTP or HTTPS)"""
+ return 'https' if url.startswith('https://') else 'http'
+
+
def md5(data):
"""returns md5 of data has hexdigest"""
return hashlib.md5(data).hexdigest()
View
15 regenwolken/views.py
@@ -14,7 +14,7 @@
from pymongo import DESCENDING
from flask import request, abort, jsonify, json, current_app, render_template, redirect
-from regenwolken.utils import login, private, A1, slug, thumbnail, clear
+from regenwolken.utils import login, private, A1, slug, thumbnail, clear, urlscheme
from regenwolken.specs import Item, Account, Drop
@@ -48,7 +48,7 @@ def index():
if obj is None:
abort(400)
else:
- return jsonify(Item(obj, config))
+ return jsonify(Item(obj, config, urlscheme(request.url)))
else:
abort(501)
@@ -167,7 +167,8 @@ def items():
items = db.items.find(query)
for item in items.sort('updated_at', DESCENDING)[pp*(page-1):pp*page]:
- listing.append(Item(fs.get(_id=item['_id']), current_app.config))
+ listing.append(Item(fs.get(_id=item['_id']),
+ current_app.config, urlscheme(request.url)))
return json.dumps(listing[::-1])
@@ -220,14 +221,14 @@ def items_view(short_id):
if obj.item_type == 'bookmark':
return redirect(obj.redirect_url)
- drop = Drop(obj, current_app.config)
+ drop = Drop(obj, current_app.config, urlscheme(request.url))
if drop.item_type == 'image':
return render_template('image.html', drop=drop)
elif drop.item_type == 'text':
return render_template('text.html', drop=drop)
else:
return render_template('other.html', drop=drop)
- return jsonify(Item(obj, current_app.config))
+ return jsonify(Item(obj, current_app.config, urlscheme(request.url)))
@login
@@ -266,7 +267,7 @@ def items_edit(object_id):
db.items.save(item)
item = fs.get(item['_id'])
- return jsonify(Item(item, conf))
+ return jsonify(Item(item, conf, urlscheme(request.url)))
@private
@@ -389,7 +390,7 @@ def insert(name, redirect_url):
'updated_at': strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()),
}
- item = Item(x, conf)
+ item = Item(x, conf, urlscheme(request.url))
db.items.insert(x)
items = acc['items']

0 comments on commit fe7212e

Please sign in to comment.