Skip to content

Commit

Permalink
update resource urls (#7470)
Browse files Browse the repository at this point in the history
  • Loading branch information
marthamareal authored May 5, 2021
1 parent 0a39cf8 commit d120dfe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
26 changes: 17 additions & 9 deletions geonode/base/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model

Expand All @@ -26,7 +25,6 @@
from dynamic_rest.serializers import DynamicEphemeralSerializer, DynamicModelSerializer
from dynamic_rest.fields.fields import DynamicRelationField, DynamicComputedField

from urllib.parse import urljoin
from avatar.templatetags.avatar_tags import avatar_url

from geonode.base.models import (
Expand All @@ -38,6 +36,7 @@
TopicCategory,
SpatialRepresentationType
)
from geonode.base.utils import build_absolute_uri

from geonode.groups.models import GroupCategory, GroupProfile

Expand Down Expand Up @@ -146,7 +145,7 @@ def __init__(self, avatar_size, **kwargs):
super(AvatarUrlField, self).__init__(**kwargs)

def get_attribute(self, instance):
return avatar_url(instance, self.avatar_size)
return build_absolute_uri(avatar_url(instance, self.avatar_size))


class EmbedUrlField(DynamicComputedField):
Expand All @@ -157,11 +156,20 @@ def __init__(self, **kwargs):
def get_attribute(self, instance):
_instance = instance.get_real_instance()
if hasattr(_instance, 'embed_url') and _instance.embed_url != NotImplemented:
return _instance.embed_url
return build_absolute_uri(_instance.embed_url)
else:
return ""


class DetailUrlField(DynamicComputedField):

def __init__(self, **kwargs):
super(DetailUrlField, self).__init__(**kwargs)

def get_attribute(self, instance):
return build_absolute_uri(instance.detail_url)


class ThumbnailUrlField(DynamicComputedField):

def __init__(self, **kwargs):
Expand All @@ -176,9 +184,7 @@ def get_attribute(self, instance):
except Exception as e:
logger.exception(e)

if thumbnail_url and 'http' not in thumbnail_url:
thumbnail_url = urljoin(settings.SITEURL, thumbnail_url)
return thumbnail_url
return build_absolute_uri(thumbnail_url)


class UserSerializer(DynamicModelSerializer):
Expand Down Expand Up @@ -250,7 +256,7 @@ def __init__(self, *args, **kwargs):
self.fields['featured'] = serializers.BooleanField()
self.fields['is_published'] = serializers.BooleanField()
self.fields['is_approved'] = serializers.BooleanField()
self.fields['detail_url'] = serializers.CharField(read_only=True)
self.fields['detail_url'] = DetailUrlField(read_only=True)
self.fields['created'] = serializers.DateTimeField(read_only=True)
self.fields['last_updated'] = serializers.DateTimeField(read_only=True)
self.fields['raw_abstract'] = serializers.CharField(read_only=True)
Expand Down Expand Up @@ -302,5 +308,7 @@ def to_representation(self, instance):
request = self.context.get('request')
data = super(ResourceBaseSerializer, self).to_representation(instance)
if request:
data['perms'] = instance.get_user_perms(request.user)
data['perms'] = instance.get_user_perms(request.user).union(
instance.get_self_resource().get_user_perms(request.user)
)
return data
8 changes: 8 additions & 0 deletions geonode/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# Standard Modules
import re
import logging
from urllib.parse import urljoin

from dateutil.parser import isoparse
from datetime import datetime, timedelta

Expand Down Expand Up @@ -133,6 +135,12 @@ def configuration_session_cache(session):
session['config'] = cached_config


def build_absolute_uri(url):
if url and 'http' not in url:
url = urljoin(settings.SITEURL, url)
return url


class OwnerRightsRequestViewUtils:

@staticmethod
Expand Down

0 comments on commit d120dfe

Please sign in to comment.