Skip to content

Commit 124a21f

Browse files
schedutronniranjan94
authored andcommitted
Move create_url() function
1 parent 0debeb0 commit 124a21f

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

app/api/helpers/storage.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ def upload_local(uploaded_file, key, **kwargs):
167167
return create_url(request.url, file_relative_path)
168168

169169

170+
def create_url(request_url, file_relative_path):
171+
"""Generates the URL of an uploaded file."""
172+
url = urlparse(request_url)
173+
174+
# No need to specify scheme-corresponding port
175+
port = url.port
176+
if port and url.scheme == SCHEMES.get(url.port, None):
177+
port = None
178+
179+
return '{scheme}://{hostname}:{port}{file_relative_path}'.format(
180+
scheme=url.scheme, hostname=url.hostname, port=port,
181+
file_relative_path=file_relative_path).replace(':None', '')
182+
183+
170184
def upload_to_aws(bucket_name, aws_region, aws_key, aws_secret, file, key, acl='public-read'):
171185
"""
172186
Uploads to AWS at key
@@ -252,17 +266,3 @@ def generate_hash(key):
252266
"""
253267
phash = generate_password_hash(key, get_settings()['secret'])
254268
return str(b64encode(phash), 'utf-8')[:10] # limit len to 10, is sufficient
255-
256-
257-
def create_url(request_url, file_relative_path):
258-
"""Generates the URL of an uploaded file."""
259-
url = urlparse(request_url)
260-
261-
# No need to specify scheme-corresponding port
262-
port = url.port
263-
if port and url.scheme == SCHEMES.get(url.port, None):
264-
port = None
265-
266-
return '{scheme}://{hostname}:{port}{file_relative_path}'.format(
267-
scheme=url.scheme, hostname=url.hostname, port=port,
268-
file_relative_path=file_relative_path).replace(':None', '')

tests/unittests/api/helpers/test_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_arbitrary_url(self):
1717
self.assertEqual(
1818
create_url(request_url, file_relative_path), expected_file_url
1919
)
20-
20+
2121
def test_http_url(self):
2222
"""Method to test a url with port 80."""
2323
request_url = 'http://localhost:80'

0 commit comments

Comments
 (0)