本文整理汇总了Python中rest_framework.compat.smart_text函数的典型用法代码示例。如果您正苦于以下问题:Python smart_text函数的具体用法?Python smart_text怎么用?Python smart_text使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了smart_text函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_notes
def get_notes(self):
"""
Returns the body of the docstring trimmed before any parameters are
listed. First, get the class docstring and then get the method's. The
methods will always inherit the class comments.
"""
docstring = ""
class_docs = self.callback.__doc__ or ''
class_docs = smart_text(class_docs)
class_docs = IntrospectorHelper.strip_yaml_from_docstring(class_docs)
class_docs = IntrospectorHelper.strip_params_from_docstring(class_docs)
method_docs = self.get_docs()
if class_docs is not None:
docstring += class_docs + " \n"
if method_docs is not None:
method_docs = formatting.dedent(smart_text(method_docs))
method_docs = IntrospectorHelper.strip_yaml_from_docstring(
method_docs
)
method_docs = IntrospectorHelper.strip_params_from_docstring(
method_docs
)
docstring += '\n' + method_docs
return do_markdown(docstring)
示例2: label_from_instance
def label_from_instance(self, obj):
"""
Return a readable representation for use with eg. select widgets.
"""
desc = smart_text(obj)
ident = smart_text(self.to_native(obj))
if desc == ident:
return desc
return "%s - %s" % (desc, ident)
示例3: __init__
def __init__(self, source=None, label=None, help_text=None):
self.parent = None
self.creation_counter = Field.creation_counter
Field.creation_counter += 1
self.source = source
if label is not None:
self.label = smart_text(label)
if help_text is not None:
self.help_text = smart_text(help_text)
示例4: valid_value
def valid_value(self, value):
"""
Check to see if the provided value is a valid choice.
"""
for k, v in self.choices:
if isinstance(v, (list, tuple)):
# This is an optgroup, so look inside the group for options
for k2, v2 in v:
if value == smart_text(k2):
return True
else:
if value == smart_text(k) or value == k:
return True
return False
示例5: to_internal_value
def to_internal_value(self, data):
try:
return self.get_queryset().get(**{self.slug_field: data})
except ObjectDoesNotExist:
self.fail('does_not_exist', slug_name=self.slug_field, value=smart_text(data))
except (TypeError, ValueError):
self.fail('invalid')
示例6: from_native
def from_native(self, data):
if self.queryset is None:
raise Exception('Writable related fields must include a `queryset` argument')
data = data.strip()
# alter the value
if self.coerce is not None:
data = self.coerce(data)
try:
return self.queryset.get(**{self.slug_lookup_field: data})
except ObjectDoesNotExist:
if not self.allow_create:
# new objects are not allowed to be created
# hence the exception
raise ValidationError(
self.error_messages['does_not_exist'] % (self.slug_field, smart_text(data))
)
obj = self.queryset.model(**{self.slug_field: data})
obj.save()
return obj
except (TypeError, ValueError):
msg = self.error_messages['invalid']
raise ValidationError(msg)
示例7: authenticate
def authenticate(self, request):
"""
Returns a two-tuple of `User` and token if a valid signature has been
supplied using JWT-based authentication. Otherwise returns `None`.
"""
auth = get_authorization_header(request).split()
auth_header_prefix = api_settings.JWT_AUTH_HEADER_PREFIX.lower()
if not auth or smart_text(auth[0].lower()) != auth_header_prefix:
return None
if len(auth) == 1:
msg = 'Invalid Authorization header. No credentials provided.'
raise exceptions.AuthenticationFailed(msg)
elif len(auth) > 2:
msg = ('Invalid Authorization header. Credentials string '
'should not contain spaces.')
raise exceptions.AuthenticationFailed(msg)
try:
payload = jwt_decode_handler(auth[1])
except jwt.ExpiredSignature:
msg = 'Signature has expired.'
raise exceptions.AuthenticationFailed(msg)
except jwt.DecodeError:
msg = 'Error decoding signature.'
raise exceptions.AuthenticationFailed(msg)
user = self.authenticate_credentials(payload)
return (user, auth[1])
示例8: get_view_doc
def get_view_doc(view, html=True):
"""
Build view documentation. Return in html format.
If you want in markdown format, use html=False
"""
try:
description = view.__doc__ or ''
description = formatting.dedent(smart_text(description))
# include filters in description
filter_fields = get_filter_fields(view)
if filter_fields:
filter_doc = ['\n\n\n## Filters', '']
for f in filter_fields:
filter_doc.append('- `%s`' % f)
description += '\n'.join(filter_doc)
# replace {api_url} by current base url
api_url = "/api"
description = description.replace('{api_url}', api_url)
if html:
description = formatting.markup_description(description)
return description
except:
import traceback
traceback.print_exc()
raise
示例9: from_native
def from_native(self, data):
if isinstance(data, basestring):
try:
data = self.queryset.only('pk').get(slug=data).pk
except ObjectDoesNotExist:
msg = self.error_messages['does_not_exist'] % smart_text(data)
raise serializers.ValidationError(msg)
return super(SlugModelChoiceField, self).from_native(data)
示例10: from_native
def from_native(self, value):
if isinstance(value, six.string_types):
return value
if value is None:
return ''
return smart_text(value)
示例11: test_view_description_supports_unicode
def test_view_description_supports_unicode(self):
"""
Unicode in docstrings should be respected.
"""
self.assertEqual(
get_view_description(ViewWithNonASCIICharactersInDocstring),
smart_text(UTF8_TEST_DOCSTRING)
)
示例12: get_view_description
def get_view_description(cls, html=False):
"""
Return a description for an `APIView` class or `@api_view` function.
"""
description = cls.__doc__ or ''
description = _remove_leading_indent(smart_text(description))
if html:
return markup_description(description)
return description
示例13: get_view_description
def get_view_description(view_cls, html=False):
"""
Given a view class, return a textual description to represent the view.
This name is used in the browsable API, and in OPTIONS responses.
This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
"""
description = view_cls.__doc__ or ''
description = formatting.dedent(smart_text(description))
if html:
return formatting.markup_description(description)
return description
示例14: from_native
def from_native(self, data, files=None):
if not data or isinstance(data, dict):
return super(FromPrivateKeyMixin, self).from_native(data, files)
try:
obj = self.opts.model.objects.get(pk=data)
obj._from_pk = True
return obj
except ObjectDoesNotExist:
raise ValidationError(self.error_messages['does_not_exist'] % smart_text(data))
except (TypeError, ValueError):
received = type(data).__name__
raise ValidationError(self.error_messages['incorrect_type'] % received)
示例15: __init__
def __init__(self, source=None, label=None, help_text=None):
self.parent = None
self.creation_counter = Field.creation_counter
Field.creation_counter += 1
self.source = source
if label is not None:
self.label = smart_text(label)
else:
self.label = None
if help_text is not None:
self.help_text = strip_multiple_choice_msg(smart_text(help_text))
else:
self.help_text = None
self._errors = []
self._value = None
self._name = None