def get_icon_url(base_url_format, obj, size, default_format='default-{size}.png'): """ Returns either the icon URL for a given (`obj`, `size`). base_url_format` is a string that will be used for url formatting if we are not using a remote storage, see ADDON_ICON_URL for an example. If no icon type if set on the `obj`, then the url for the appropriate default icon for the given `size` will be returned. `obj` needs to implement `icon_type` and `icon_hash` properties for this function to work. Note: does not check size, so it can return 404 URLs if you specify an invalid size. """ # Return default image if no icon_type was stored. if not obj.icon_type: return '{path}/{name}'.format(path=static_url('ICONS_DEFAULT_URL'), name=default_format.format(size=size)) else: # If we don't have the icon_hash set to a dummy string ("never"), # when the icon is eventually changed, icon_hash will be updated. suffix = obj.icon_hash or 'never' if storage_is_remote(): # We don't care about base_url_format, the storage provides the url # for a given path. We assume AWS_QUERYSTRING_AUTH is False atm. path = '%s/%s-%s.png' % (obj.get_icon_dir(), obj.pk, size) return '%s?modified=%s' % (public_storage.url(path), suffix) # [1] is the whole ID, [2] is the directory. split_id = re.match(r'((\d*?)\d{1,3})$', str(obj.pk)) return base_url_format % (split_id.group(2) or 0, obj.pk, size, suffix)
def test_get_icon_url_bigger_pk(self): website = Website(pk=98765432, icon_type='image/png') if not storage_is_remote(): expected = (static_url('WEBSITE_ICON_URL') % (str(website.pk)[:-3], website.pk, 32, 'never')) else: path = '%s/%s-%s.png' % (website.get_icon_dir(), website.pk, 32) expected = '%s?modified=never' % public_storage.url(path) assert website.get_icon_url(32).endswith(expected), ( 'Expected %s, got %s' % (expected, website.get_icon_url(32)))
def test_download(self): res = self.client.get(self.url) path = public_storage.url(self.file.signed_file_path) self.assert3xx(res, path) assert settings.XSENDFILE_HEADER not in res
def test_download_storage(self): ok_(self.langpack.download_url) response = self.client.get(self.langpack.download_url) path = public_storage.url(self.langpack.file_path) self.assert3xx(response, path)