Last active
May 17, 2021 10:02
-
-
Save dkdndes/50cec5991b07fc039ca431d815a1a1b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Utils for the media files.""" | |
from django.conf import settings | |
from django.contrib.sites.models import Site | |
from django.utils.functional import lazy | |
def generate_abs_media_url(): | |
"""Return the absolute media URL. | |
One problem of the media URL is, that the end user | |
can configure this in a relative way. THis works for the | |
django templates, but is problematic for the channels and REST | |
users. | |
Returns: | |
str: The absolute media URL. | |
""" | |
media_url = getattr(settings, 'MEDIA_URL') | |
http_check = media_url.startswith('http') | |
if http_check: | |
return media_url | |
# Generate the complete URL | |
protocol = getattr(settings, 'DEFAULT_HTTP_PROTOCOL', 'http') | |
return "{protocol}://{domain}{media}".format( | |
protocol=protocol, domain=Site.objects.get_current(), media=media_url) | |
absolute_media_url = lazy(generate_abs_media_url, str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment