def test_format_value_hyperlink(self): """ Tests format_value with a URL """ url = 'http://url.com' name = 'name_of_url' hyperlink = Hyperlink(url, name) self.assertEqual(format_value(hyperlink), '<a href=%s>%s</a>' % (url, name))
def to_representation(self, value): format = self.context.get('format', None) if format and self.format and self.format != format: format = self.format # Return the hyperlink, or error if incorrectly configured. url = self.get_url_options(value, format) if url is None: return None return Hyperlink(url, value)
def to_representation(self, value): format = self.context.get('format', None) if format and self.format and self.format != format: format = self.format # Return the hyperlink, or error if incorrectly configured. if value.source_adjudicator is not None: url = self.get_url(value.source_adjudicator.adjudicator, 'api-adjudicator-detail', self.context['request'], format) elif value.source_team is not None: url = self.get_url(value.source_team.team, 'api-team-detail', self.context['request'], format) if url is None: return None return Hyperlink(url, value)
def to_representation(self, value): from pprint import pprint pprint('in drf rest_framework_nested.to_representation()') pprint(self) pprint(value) assert 'request' in self.context, ( "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " "the serializer." % self.__class__.__name__) request = self.context['request'] format = self.context.get('format', None) # By default use whatever format is given for the current context # unless the target is a different type to the source. # # Eg. Consider a HyperlinkedIdentityField pointing from a json # representation to an html property of that representation... # # '/snippets/1/' should link to '/snippets/1/highlight/' # ...but... # '/snippets/1/.json' should link to '/snippets/1/highlight/.html' if format and self.format and self.format != format: format = self.format # Return the hyperlink, or error if incorrectly configured. try: url = self.get_url(value, self.view_name, request, format) except NoReverseMatch: msg = ( 'Could not resolve URL for hyperlinked relationship using ' 'view name "%s". You may have failed to include the related ' 'model in your API, or incorrectly configured the ' '`lookup_field` attribute on this field.') if value in ('', None): value_string = {'': 'the empty string', None: 'None'}[value] msg += ( " WARNING: The value of the field on the model instance " "was %s, which may be why it didn't match any " "entries in your URL conf." % value_string) raise ImproperlyConfigured(msg % self.view_name) if url is None: return None from rest_framework.relations import Hyperlink return Hyperlink(url, value)
def _get_url_representation( self, value: Any, view_name: Optional[str] = None) -> Optional[str]: """ Copied from base with a few adjustments for Viewname detection """ assert "request" in self.context, ( "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " "the serializer." % self.__class__.__name__) request = self.context["request"] format_ = self.context.get("format", None) if format_ and self.format and self.format != format_: format_ = self.format try: reverse_view = view_name or self.view_name url = self.get_url(value, reverse_view, request, format_) except NoReverseMatch: msg = ( "Could not resolve URL for hyperlinked relationship using " "view name '%s'. You may have failed to include the related " "model in your API, or incorrectly configured the " "`lookup_field` attribute on this field.") if value in ("", None): value_string = {"": "the empty string", None: "None"}[value] msg += ( " WARNING: The value of the field on the model instance " "was %s, which may be why it didn't match any " "entries in your URL conf." % value_string) raise ImproperlyConfigured(msg % self.view_name) if url is None: return None return Hyperlink(url, value)
def to_representation(self, obj): # pylint: disable=arguments-differ return Hyperlink(obj, None)