-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request MUST be declared for serializer #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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={}, *args, **kwargs): | ||
def __init__(self, geometry_string, options={}, uri_prefix=api_settings['URI_PREFIX'], *args, **kwargs): | ||
""" | ||
Create an instance of the HyperlinkedSorlImageField image serializer. | ||
|
||
|
@@ -65,6 +66,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) | ||
|
||
|
@@ -82,12 +84,14 @@ 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, ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how this would fare with existing usage as the current implementation attempts to use While I'm not too afraid of breaking changes at this stage I think it's worth some consideration. Perhaps this behaviour should only be triggered by a specific option? |
||
"`%s` requires the request in the serializer" | ||
" context. Add `context={'request': request}` when instantiating " | ||
"the serializer." % self.__class__.__name__ | ||
) | ||
|
||
if self.uri_prefix: | ||
return request.build_absolute_uri(self.uri_prefix + image.url) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is I think I would prefer something like
or even |
||
return request.build_absolute_uri(image.url) | ||
to_native = to_representation |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.conf import settings | ||
|
||
SORL_THUMBNAIL_SETTINGS = getattr(settings, "SORL_THUMBNAIL_SETTINGS", {}) | ||
|
||
SORL_THUMBNAIL_SETTINGS.setdefault("URI_PREFIX", None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A corresponding entry in the☺️
Args:
list above would be niceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean you don't like settings usage here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that's a bit unclear now that I read it myself. I just meant I'd like an the docstring above, under the Args heading.