From 73482636f4fe2b08466850a4a37d901057346690 Mon Sep 17 00:00:00 2001 From: Roman Vasilyev Date: Tue, 2 Feb 2016 23:32:15 -0800 Subject: [PATCH 1/3] request MUST be declared for serializer --- sorl_thumbnail_serializer/fields.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sorl_thumbnail_serializer/fields.py b/sorl_thumbnail_serializer/fields.py index 66b54ee..1442124 100644 --- a/sorl_thumbnail_serializer/fields.py +++ b/sorl_thumbnail_serializer/fields.py @@ -82,12 +82,12 @@ def to_representation(self, value): image = get_thumbnail(value, self.geometry_string, **self.options) - try: - request = self.context.get('request', None) - return request.build_absolute_uri(image.url) - except: - try: - return super(HyperlinkedSorlImageField, self).to_representation(image.url) - except AttributeError: # NOQA - return super(HyperlinkedSorlImageField, self).to_native(image.url) # NOQA + request = self.context.get('request', None) + assert request is not None, ( + "`%s` requires the request in the serializer" + " context. Add `context={'request': request}` when instantiating " + "the serializer." % self.__class__.__name__ + ) + + return request.build_absolute_uri(image.url) to_native = to_representation From c402b8e98771bb3cd282042982ff955ca5475f1c Mon Sep 17 00:00:00 2001 From: Roman Vasilyev Date: Fri, 8 Apr 2016 09:28:23 -0700 Subject: [PATCH 2/3] now we can handle more than current domain --- sorl_thumbnail_serializer/fields.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sorl_thumbnail_serializer/fields.py b/sorl_thumbnail_serializer/fields.py index 1442124..bcf5d57 100644 --- a/sorl_thumbnail_serializer/fields.py +++ b/sorl_thumbnail_serializer/fields.py @@ -49,7 +49,7 @@ class HyperlinkedSorlImageField(serializers.ImageField): """A Django REST Framework Field class returning hyperlinked scaled and cached images.""" - def __init__(self, geometry_string, options={}, *args, **kwargs): + def __init__(self, geometry_string, options={}, uri_prefix=None, *args, **kwargs): """ Create an instance of the HyperlinkedSorlImageField image serializer. @@ -65,6 +65,7 @@ def __init__(self, geometry_string, options={}, *args, **kwargs): """ # NOQA self.geometry_string = geometry_string self.options = options + self.uri_prefix = uri_prefix super(HyperlinkedSorlImageField, self).__init__(*args, **kwargs) @@ -89,5 +90,7 @@ def to_representation(self, value): "the serializer." % self.__class__.__name__ ) + if self.uri_prefix: + return request.build_absolute_uri(self.uri_prefix + image.url) return request.build_absolute_uri(image.url) to_native = to_representation From 690ff2763015fde92d0d01f11f60921f7e6cf107 Mon Sep 17 00:00:00 2001 From: Roman Vasilyev Date: Thu, 13 Oct 2016 09:43:28 -0700 Subject: [PATCH 3/3] settings module and configurable uri prefix --- sorl_thumbnail_serializer/fields.py | 3 ++- sorl_thumbnail_serializer/settings.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 sorl_thumbnail_serializer/settings.py diff --git a/sorl_thumbnail_serializer/fields.py b/sorl_thumbnail_serializer/fields.py index bcf5d57..80c5b80 100644 --- a/sorl_thumbnail_serializer/fields.py +++ b/sorl_thumbnail_serializer/fields.py @@ -43,13 +43,14 @@ class TestModelViewSet(viewsets.ModelViewSet): from rest_framework import serializers from sorl.thumbnail import get_thumbnail +from .settings import SORL_THUMBNAIL_SETTINGS as api_settings class HyperlinkedSorlImageField(serializers.ImageField): """A Django REST Framework Field class returning hyperlinked scaled and cached images.""" - def __init__(self, geometry_string, options={}, uri_prefix=None, *args, **kwargs): + def __init__(self, geometry_string, options={}, uri_prefix=api_settings['URI_PREFIX'], *args, **kwargs): """ Create an instance of the HyperlinkedSorlImageField image serializer. diff --git a/sorl_thumbnail_serializer/settings.py b/sorl_thumbnail_serializer/settings.py new file mode 100644 index 0000000..cc5441d --- /dev/null +++ b/sorl_thumbnail_serializer/settings.py @@ -0,0 +1,5 @@ +from django.conf import settings + +SORL_THUMBNAIL_SETTINGS = getattr(settings, "SORL_THUMBNAIL_SETTINGS", {}) + +SORL_THUMBNAIL_SETTINGS.setdefault("URI_PREFIX", None)