本文整理匯總了Python中django.utils.encoding.force_str方法的典型用法代碼示例。如果您正苦於以下問題:Python encoding.force_str方法的具體用法?Python encoding.force_str怎麽用?Python encoding.force_str使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.utils.encoding
的用法示例。
在下文中一共展示了encoding.force_str方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: change
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def change(request, object, message_or_fields):
"""
Log that an object has been successfully changed.
The argument *message_or_fields* must be a sequence of modified field names
or a custom change message.
"""
if isinstance(message_or_fields, str):
message = message_or_fields
else:
message = get_change_message(message_or_fields)
models.LogEntry.objects.log_action(
user_id=request.user.pk,
content_type_id=ContentType.objects.get_for_model(object).pk,
object_id=object.pk,
object_repr=force_str(object),
action_flag=models.CHANGE,
change_message=message,
)
示例2: localize_input
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def localize_input(value, default=None):
"""
Checks if an input value is a localizable type and returns it
formatted with the appropriate formatting string of the current locale.
"""
if isinstance(value, (decimal.Decimal, float) + six.integer_types):
return number_format(value)
elif isinstance(value, datetime.datetime):
value = datetime_safe.new_datetime(value)
format = force_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
return value.strftime(format)
elif isinstance(value, datetime.date):
value = datetime_safe.new_date(value)
format = force_str(default or get_format('DATE_INPUT_FORMATS')[0])
return value.strftime(format)
elif isinstance(value, datetime.time):
format = force_str(default or get_format('TIME_INPUT_FORMATS')[0])
return value.strftime(format)
return value
示例3: get_connection_params
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def get_connection_params(self):
settings_dict = self.settings_dict
# None may be used to connect to the default 'postgres' db
if settings_dict['NAME'] == '':
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured(
"settings.DATABASES is improperly configured. "
"Please supply the NAME value.")
conn_params = {
'database': settings_dict['NAME'] or 'postgres',
}
conn_params.update(settings_dict['OPTIONS'])
conn_params.pop('isolation_level', None)
if settings_dict['USER']:
conn_params['user'] = settings_dict['USER']
if settings_dict['PASSWORD']:
conn_params['password'] = force_str(settings_dict['PASSWORD'])
if settings_dict['HOST']:
conn_params['host'] = settings_dict['HOST']
if settings_dict['PORT']:
conn_params['port'] = settings_dict['PORT']
return conn_params
示例4: get_connection_params
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def get_connection_params(self):
kwargs = {
'conv': django_conversions,
'charset': 'utf8',
}
if six.PY2:
kwargs['use_unicode'] = True
settings_dict = self.settings_dict
if settings_dict['USER']:
kwargs['user'] = settings_dict['USER']
if settings_dict['NAME']:
kwargs['db'] = settings_dict['NAME']
if settings_dict['PASSWORD']:
kwargs['passwd'] = force_str(settings_dict['PASSWORD'])
if settings_dict['HOST'].startswith('/'):
kwargs['unix_socket'] = settings_dict['HOST']
elif settings_dict['HOST']:
kwargs['host'] = settings_dict['HOST']
if settings_dict['PORT']:
kwargs['port'] = int(settings_dict['PORT'])
# We need the number of potentially affected rows after an
# "UPDATE", not the number of changed rows.
kwargs['client_flag'] = CLIENT.FOUND_ROWS
kwargs.update(settings_dict['OPTIONS'])
return kwargs
示例5: __str__
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def __str__(self):
from django.db import models
if self.obj is None:
obj = "?"
elif isinstance(self.obj, models.base.ModelBase):
# We need to hardcode ModelBase and Field cases because its __str__
# method doesn't return "applabel.modellabel" and cannot be changed.
model = self.obj
app = model._meta.app_label
obj = '%s.%s' % (app, model._meta.object_name)
else:
obj = force_str(self.obj)
id = "(%s) " % self.id if self.id else ""
hint = "\n\tHINT: %s" % self.hint if self.hint else ''
return "%s: %s%s%s" % (obj, id, self.msg, hint)
示例6: build_json_resource_obj
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name,
force_type_resolution=False):
"""
Builds the resource object (type, id, attributes) and extracts relationships.
"""
# Determine type from the instance if the underlying model is polymorphic
if force_type_resolution:
resource_name = utils.get_resource_type_from_instance(resource_instance)
resource_data = [
('type', resource_name),
('id', encoding.force_str(resource_instance.pk) if resource_instance else None),
('attributes', cls.extract_attributes(fields, resource)),
]
relationships = cls.extract_relationships(fields, resource, resource_instance)
if relationships:
resource_data.append(('relationships', relationships))
# Add 'self' link if field is present and valid
if api_settings.URL_FIELD_NAME in resource and \
isinstance(fields[api_settings.URL_FIELD_NAME], relations.RelatedField):
resource_data.append(('links', {'self': resource[api_settings.URL_FIELD_NAME]}))
return OrderedDict(resource_data)
示例7: test_datefilter_filtered
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDTAdmin(RangeModelDT, site)
request = self.request_factory.get('/', {'created_at__range__gte': self.today,
'created_at__range__lte': self.tomorrow})
request.user = self.user
changelist = self.get_changelist(request, RangeModelDT, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['system_name'], 'created-at')
示例8: test_datefilter_with_default
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_with_default(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDTAdmin(RangeModelDT, site)
modeladmin.get_rangefilter_created_at_default = lambda r: [self.today, self.tomorrow]
request = self.request_factory.get('/')
request.user = self.user
changelist = self.get_changelist(request, RangeModelDT, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.djangonaut_book, self.django_book])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
self.assertEqual(filterspec.default_gte, self.today)
self.assertEqual(filterspec.default_lte, self.tomorrow)
示例9: test_datefilter_filtered_with_one_params
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered_with_one_params(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDTAdmin(RangeModelDT, site)
request = self.request_factory.get('/', {'created_at__range__gte': self.today})
request.user = self.user
changelist = self.get_changelist(request, RangeModelDT, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['system_name'], 'created-at')
示例10: test_datefilter_filtered_datefield
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datefilter_filtered_datefield(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDAdmin(RangeModelD, site)
request = self.request_factory.get('/', {'created_at__range__gte': self.today,
'created_at__range__lte': self.tomorrow})
request.user = self.user
changelist = self.get_changelist(request, RangeModelD, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book_date])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['system_name'], 'created-at')
示例11: test_datetimfilter_filtered
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datetimfilter_filtered(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDTTimeAdmin(RangeModelDT, site)
request = self.request_factory.get('/', {'created_at__range__gte_0': self.today,
'created_at__range__gte_1': self.min_time,
'created_at__range__lte_0': self.tomorrow,
'created_at__range__lte_1': self.max_time})
request.user = self.user
changelist = self.get_changelist(request, RangeModelDT, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.django_book])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
choice = select_by(filterspec.choices(changelist))
self.assertEqual(choice['query_string'], '?')
self.assertEqual(choice['system_name'], 'created-at')
示例12: test_datetimfilter_with_default
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def test_datetimfilter_with_default(self):
self.request_factory = RequestFactory()
modeladmin = RangeModelDTTimeAdmin(RangeModelDT, site)
modeladmin.get_rangefilter_created_at_default = lambda r: [self.today, self.tomorrow]
request = self.request_factory.get('/')
request.user = self.user
changelist = self.get_changelist(request, RangeModelDT, modeladmin)
queryset = changelist.get_queryset(request)
self.assertEqual(list(queryset), [self.djangonaut_book, self.django_book])
filterspec = changelist.get_filters(request)[0][0]
self.assertEqual(force_str(filterspec.title), 'created at')
self.assertEqual(filterspec.default_gte, self.today)
self.assertEqual(filterspec.default_lte, self.tomorrow)
示例13: _to_xml
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def _to_xml(self, xml, data):
if isinstance(data, (list, tuple)):
for item in data:
xml.startElement(self.item_tag_name, {})
self._to_xml(xml, item)
xml.endElement(self.item_tag_name)
elif isinstance(data, dict):
for key, value in data.items():
xml.startElement(key, {})
self._to_xml(xml, value)
xml.endElement(key)
elif data is None:
# Don't output any value
pass
else:
xml.characters(force_str(data))
示例14: addition
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def addition(request, object):
"""
Log that an object has been successfully added.
"""
models.LogEntry.objects.log_action(
user_id=request.user.pk,
content_type_id=ContentType.objects.get_for_model(object).pk,
object_id=object.pk,
object_repr=force_str(object),
action_flag=models.ADDITION,
)
示例15: deletion
# 需要導入模塊: from django.utils import encoding [as 別名]
# 或者: from django.utils.encoding import force_str [as 別名]
def deletion(request, object, object_repr=None):
"""
Log that an object will be deleted.
"""
models.LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(object).pk,
object_id=object.pk,
object_repr=object_repr or force_str(object),
action_flag=models.DELETION,
)