本文整理汇总了Python中django.utils.functional.Promise方法的典型用法代码示例。如果您正苦于以下问题:Python functional.Promise方法的具体用法?Python functional.Promise怎么用?Python functional.Promise使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.functional
的用法示例。
在下文中一共展示了functional.Promise方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: field_display
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def field_display(field, safe=False):
out = []
if isinstance(field.field.widget, (forms.widgets.RadioSelect, forms.widgets.CheckboxSelectMultiple)):
out.append('<div class="field radio">%s</div>' % (str(field)))
else:
out.append('<div class="field">%s</div>' % (str(field)))
out.append(str(field.errors))
if field.help_text:
if isinstance(field.help_text, Promise):
out.append('<div class="helptext">%s</div>' % (escape(field.help_text)))
else:
if safe:
out.append('<div class="helptext">%s</div>' % (field.help_text))
else:
out.append('<div class="helptext">%s</div>' % (escape(field.help_text)))
return mark_safe('\n'.join(out))
示例2: mark_safe
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def mark_safe(s):
"""
Explicitly mark a string as safe for (HTML) output purposes. The returned
object can be used everywhere a string is appropriate.
If used on a method as a decorator, mark the returned data as safe.
Can be called multiple times on a single string.
"""
if hasattr(s, '__html__'):
return s
if isinstance(s, (str, Promise)):
return SafeText(s)
if callable(s):
return _safety_decorator(mark_safe, s)
return SafeText(str(s))
示例3: force_bytes
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Similar to smart_bytes, except that lazy instances are resolved to
strings, rather than kept as lazy objects.
If strings_only is True, don't convert (some) non-string-like objects.
"""
# Handle the common case first for performance reasons.
if isinstance(s, bytes):
if encoding == 'utf-8':
return s
else:
return s.decode('utf-8', errors).encode(encoding, errors)
if strings_only and is_protected_type(s):
return s
if isinstance(s, memoryview):
return bytes(s)
if isinstance(s, Promise) or not isinstance(s, str):
return str(s).encode(encoding, errors)
else:
return s.encode(encoding, errors)
示例4: get_model_fields
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def get_model_fields(model, base=None):
list = []
fields = model._meta.fields
for f in fields:
label = f.name
if hasattr(f, 'verbose_name'):
label = getattr(f, 'verbose_name')
if isinstance(label, Promise):
label = str(label)
if base:
list.append(('{}__{}'.format(base, f.name), label))
else:
list.append((f.name, label))
return list
示例5: mark_safe
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def mark_safe(s):
"""
Explicitly mark a string as safe for (HTML) output purposes. The returned
object can be used everywhere a string or unicode object is appropriate.
If used on a method as a decorator, mark the returned data as safe.
Can be called multiple times on a single string.
"""
if hasattr(s, '__html__'):
return s
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_bytes):
return SafeBytes(s)
if isinstance(s, (six.text_type, Promise)):
return SafeText(s)
if callable(s):
return _safety_decorator(mark_safe, s)
return SafeString(str(s))
示例6: mark_for_escaping
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def mark_for_escaping(s):
"""
Explicitly mark a string as requiring HTML escaping upon output. Has no
effect on SafeData subclasses.
Can be called multiple times on a single string (the resulting escaping is
only applied once).
"""
warnings.warn('mark_for_escaping() is deprecated.', RemovedInDjango20Warning)
if hasattr(s, '__html__') or isinstance(s, EscapeData):
return s
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_bytes):
return EscapeBytes(s)
if isinstance(s, (six.text_type, Promise)):
return EscapeText(s)
return EscapeString(str(s))
示例7: insert_values
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def insert_values(self, fields, objs, raw=False):
"""
Set up the insert query from the 'insert_values' dictionary. The
dictionary gives the model field names and their target values.
If 'raw_values' is True, the values in the 'insert_values' dictionary
are inserted directly into the query, rather than passed as SQL
parameters. This provides a way to insert NULL and DEFAULT keywords
into the query, for example.
"""
self.fields = fields
# Check that no Promise object reaches the DB. Refs #10498.
for field in fields:
for obj in objs:
value = getattr(obj, field.attname)
if isinstance(value, Promise):
setattr(obj, field.attname, force_text(value))
self.objs = objs
self.raw = raw
示例8: convert_context_to_json
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def convert_context_to_json(self, context):
"""
Get what we want out of the context dict and convert that to a JSON
object. Note that this does no object serialization b/c we're
not sending any objects.
"""
if 'month/shift' in self.request.path: # month calendar
return dumps(self.get_month_calendar_dict(context))
elif 'event-list/shift' in self.request.path: # month event list
return dumps(self.get_month_event_list_dict(context))
elif 'cal-and-list/shift' in self.request.path:
cal = self.get_month_calendar_dict(context)
l = self.get_month_event_list_dict(context)
cal.update(l)
return dumps(cal)
else: # day list view
for key, val in context.items():
if isinstance(val, Promise):
context[key] = force_text(val)
return dumps(self.get_day_context_dict(context))
示例9: smart_repr
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def smart_repr(value):
if isinstance(value, models.Manager):
return manager_repr(value)
if isinstance(value, Promise) and value._delegate_text:
value = force_text(value)
value = unicode_repr(value)
# Representations like u'help text'
# should simply be presented as 'help text'
if value.startswith("u'") and value.endswith("'"):
return value[1:]
# Representations like
# <django.core.validators.RegexValidator object at 0x1047af050>
# Should be presented as
# <django.core.validators.RegexValidator object>
value = re.sub(' at 0x[0-9A-Fa-f]{4,32}>', '>', value)
return value
示例10: default
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def default(self, obj):
if isinstance(obj, Promise):
return force_text(obj)
return super(LazyEncoder, self).default(obj)
示例11: default
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def default(self, o):
if isinstance(o, datetime.datetime):
return o.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(o, datetime.date):
return o.strftime('%Y-%m-%d')
elif isinstance(o, decimal.Decimal):
return str(o)
elif isinstance(o, Promise):
return force_text(o)
else:
try:
return super(JSONEncoder, self).default(o)
except Exception:
return smart_text(o)
示例12: mark_safe
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def mark_safe(s):
"""
Explicitly mark a string as safe for (HTML) output purposes. The returned
object can be used everywhere a string or unicode object is appropriate.
Can be called multiple times on a single string.
"""
if hasattr(s, '__html__'):
return s
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_bytes):
return SafeBytes(s)
if isinstance(s, (six.text_type, Promise)):
return SafeText(s)
return SafeString(str(s))
示例13: mark_for_escaping
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def mark_for_escaping(s):
"""
Explicitly mark a string as requiring HTML escaping upon output. Has no
effect on SafeData subclasses.
Can be called multiple times on a single string (the resulting escaping is
only applied once).
"""
if hasattr(s, '__html__') or isinstance(s, EscapeData):
return s
if isinstance(s, bytes) or (isinstance(s, Promise) and s._delegate_bytes):
return EscapeBytes(s)
if isinstance(s, (six.text_type, Promise)):
return EscapeText(s)
return EscapeString(str(s))
示例14: smart_text
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def smart_text(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Returns a text object representing 's' -- unicode on Python 2 and str on
Python 3. Treats bytestrings using the 'encoding' codec.
If strings_only is True, don't convert (some) non-string-like objects.
"""
if isinstance(s, Promise):
# The input is the result of a gettext_lazy() call.
return s
return force_text(s, encoding, strings_only, errors)
示例15: smart_bytes
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import Promise [as 别名]
def smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Returns a bytestring version of 's', encoded as specified in 'encoding'.
If strings_only is True, don't convert (some) non-string-like objects.
"""
if isinstance(s, Promise):
# The input is the result of a gettext_lazy() call.
return s
return force_bytes(s, encoding, strings_only, errors)