Skip to content

Commit

Permalink
Merge pull request #1874 from Scifabric/issue-1868
Browse files Browse the repository at this point in the history
Issue 1868
  • Loading branch information
teleyinex committed Jul 4, 2018
2 parents f8adc60 + 43f76cf commit 56b0932
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 10 deletions.
9 changes: 7 additions & 2 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def get_thumbnail_urls():
container = project.info.get('container')
if (thumbnail and container):
print "Updating project: %s" % project.short_name
thumbnail_url = get_avatar_url(upload_method, thumbnail, container)
thumbnail_url = get_avatar_url(upload_method, thumbnail,
container,
app.config.get('AVATAR_ABSOLUTE',
True))
project.info['thumbnail_url'] = thumbnail_url
db.session.merge(project)
db.session.commit()
Expand All @@ -111,7 +114,9 @@ def get_avatars_url():
container = user.info.get('container')
if (avatar and container):
print "Updating user: %s" % user.name
avatar_url = get_avatar_url(upload_method, avatar, container)
avatar_url = get_avatar_url(upload_method, avatar,
container,
app.config.get('AVATAR_ABSOLUTE'))
user.info['avatar_url'] = avatar_url
db.session.merge(user)
db.session.commit()
Expand Down
4 changes: 3 additions & 1 deletion pybossa/api/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ def _file_upload(self, data):
uploader.upload_file(_file,
container=container)
file_url = get_avatar_url(upload_method,
_file.filename, container)
_file.filename,
container,
current_app.config.get('AVATAR_ABSOLUTE'))
tmp['media_url'] = file_url
if tmp.get('info') is None:
tmp['info'] = dict()
Expand Down
3 changes: 3 additions & 0 deletions pybossa/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,6 @@

# Default cryptopan key
CRYPTOPAN_KEY = '32-char-str-for-AES-key-and-pad.'

# Instruct PYBOSSA to generate absolute paths or not for avatars
AVATAR_ABSOLUTE = True
6 changes: 3 additions & 3 deletions pybossa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ def fuzzyboolean(value):
raise ValueError("Invalid literal for boolean(): {}".format(value))


def get_avatar_url(upload_method, avatar, container):
def get_avatar_url(upload_method, avatar, container, external):
"""Return absolute URL for avatar."""
if upload_method.lower() == 'rackspace':
return url_for('rackspace',
Expand All @@ -517,10 +517,10 @@ def get_avatar_url(upload_method, avatar, container):
else:
filename = container + '/' + avatar
return url_for('uploads.uploaded_file', filename=filename,
_external=True)
_external=external)


def get_disqus_sso(user): # pragma: no cover
def get_disqus_sso(user): # pragma: no cover
# create a JSON packet of our data attributes
# return a script tag to insert the sso message."""
message, timestamp, sig, pub_key = get_disqus_sso_payload(user)
Expand Down
4 changes: 3 additions & 1 deletion pybossa/view/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ def _handle_avatar_update(user, avatar_form):
uploader.delete_file(user.info['avatar'], container)
upload_method = current_app.config.get('UPLOAD_METHOD')
avatar_url = get_avatar_url(upload_method,
_file.filename, container)
_file.filename,
container,
current_app.config.get('AVATAR_ABSOLUTE'))
user.info['avatar'] = _file.filename
user.info['container'] = container
user.info['avatar_url'] = avatar_url
Expand Down
5 changes: 4 additions & 1 deletion pybossa/view/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,10 @@ def handle_valid_form(form):
project.info['container'] = container
upload_method = current_app.config.get('UPLOAD_METHOD')
thumbnail_url = get_avatar_url(upload_method,
_file.filename, container)
_file.filename,
container,
current_app.config.get('AVATAR_ABSOLUTE')
)
project.info['thumbnail_url'] = thumbnail_url
project_repo.save(project)
flash(gettext('Your project thumbnail has been updated! It may \
Expand Down
3 changes: 3 additions & 0 deletions settings_local.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,6 @@ TTL_ZIP_SEC_FILES = 3

# Instruct PYBOSSA to generate HTTP or HTTPS
PREFERRED_URL_SCHEME='https'

# Instruct PYBOSSA to generate absolute paths or not for avatars
AVATAR_ABSOLUTE = True
1 change: 1 addition & 0 deletions settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@
"^/static/.*"
]
}
AVATAR_ABSOLUTE = True
10 changes: 8 additions & 2 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,14 +914,20 @@ def test_rank_by_chosen_attribute_reversed(self):
@patch('pybossa.util.url_for')
def test_get_avatar_url(self, mock_url_for):
"""Test get_avatar_url works."""
util.get_avatar_url('rackspace', '1.png', '1')
util.get_avatar_url('rackspace', '1.png', '1', True)
mock_url_for.assert_called_with('rackspace', container='1', filename='1.png')

util.get_avatar_url('local', '1.png', '1')
util.get_avatar_url('local', '1.png', '1', True)
mock_url_for.assert_called_with('uploads.uploaded_file',
_external=True,
filename='1/1.png')

util.get_avatar_url('local', '1.png', '1', False)
mock_url_for.assert_called_with('uploads.uploaded_file',
_external=False,
filename='1/1.png')



class TestJSONEncoder(object):

Expand Down

0 comments on commit 56b0932

Please sign in to comment.