diff --git a/geonode/base/api/serializers.py b/geonode/base/api/serializers.py index 03d959193e5..9f9bfbdb1e8 100644 --- a/geonode/base/api/serializers.py +++ b/geonode/base/api/serializers.py @@ -17,7 +17,6 @@ # along with this program. If not, see . # ######################################################################### -from django.conf import settings from django.contrib.auth.models import Group from django.contrib.auth import get_user_model @@ -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 ( @@ -38,6 +36,7 @@ TopicCategory, SpatialRepresentationType ) +from geonode.base.utils import build_absolute_uri from geonode.groups.models import GroupCategory, GroupProfile @@ -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): @@ -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): @@ -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): @@ -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) @@ -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 diff --git a/geonode/base/utils.py b/geonode/base/utils.py index 75f91bfdd94..edbe319d08c 100644 --- a/geonode/base/utils.py +++ b/geonode/base/utils.py @@ -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 @@ -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