Skip to content

Commit

Permalink
Use reverse and request.build_absolute_uri to build URL
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Apr 2, 2024
1 parent 49520c6 commit 85e1678
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions api/api/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.html import format_html

from elasticsearch import Elasticsearch, NotFoundError
Expand Down Expand Up @@ -206,12 +207,22 @@ def clean(self):
f"with identifier '{self.media_obj_id}'."
)

@property
def url(self):
origin = settings.CANONICAL_ORIGIN
if not origin.endswith("/"):
origin = f"{origin}/"
url = f"{origin}v1/{self.url_frag}/{self.media_obj.identifier}"
def url(self, request=None) -> str:
"""
Build the URL of the media item. This uses ``reverse`` and
``request.build_absolute_uri`` to build the URL without having to worry
about canonical URL or trailing slashes.
:param request: the current request object, to get absolute URLs
:return: the URL of the media item
"""

url = reverse(
f"{self.media_class.__name__.lower()}-detail",
args=[self.media_obj_id],
)
if request is not None:
url = request.build_absolute_uri(url)
return format_html(f"<a href={url}>{url}</a>")

@property
Expand Down

0 comments on commit 85e1678

Please sign in to comment.