本文整理汇总了Python中django.utils.text.Truncator.chars方法的典型用法代码示例。如果您正苦于以下问题:Python Truncator.chars方法的具体用法?Python Truncator.chars怎么用?Python Truncator.chars使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.text.Truncator
的用法示例。
在下文中一共展示了Truncator.chars方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: offers
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def offers(doc, categories):
queryset = Product.objects.filter(is_visible=True, is_retail=True, quantity__gt=0, price__gt=0)
doc.write('<offers>')
for product in queryset:
try:
category_id = product.categories.filter(parent__isnull=False, id__in=categories).values_list('id', flat=True)[0]
except IndexError:
try:
category_id = product.categories.filter(parent__isnull=True, id__in=categories).values_list('id', flat=True)[0]
except IndexError:
continue
text = product.description.strip()
if text:
text = strip_tags(strip_spaces_between_tags(product.description)).strip()
text = typograph(WHITESPACE_RE.sub(' ', text))
truncator = Truncator(text)
text = truncator.chars(512)
doc.write('<offer>')
doc.write('<url>http://tomat-podarky.ru{}</url>'.format(product.get_absolute_url()))
doc.write('<price>{}</price>'.format(product.price))
doc.write('<currencyId>RUR</currencyId>')
doc.write('<categoryId>{}</categoryId>'.format(category_id))
doc.write('<delivery>true</delivery>')
doc.write('<name>')
doc.write(_(typograph(product.title)))
doc.write('</name>')
if text:
doc.write('<description>')
doc.write(_(text))
doc.write('</description>')
doc.write('</offer>\n')
doc.write('</offers>')
示例2: save_plugin_data
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def save_plugin_data(self, request=None):
"""Save plugin data.
We want to save the generated lorem ipsum text for later use.
Thus, although we don't show it to the user, in case when
``generate_lipsum`` field is set to True, we silently generate the
text and save it into the plugin data.
"""
if self.cleaned_data.get('generate_lipsum', None):
lipsum_language = self.cleaned_data.get('lipsum_language', None)
try:
if lipsum_language in LANGUAGE_CHOICES_KEYS:
if lipsum_language == 'en':
gen = Generator()
else:
gen = TranslipsumGenerator(
language_code=lipsum_language
)
text = gen.generate_paragraph()
truncator = Truncator(text)
self.cleaned_data['text'] = truncator.chars(
self.cleaned_data.get('lipsum_max_chars',
DEFAULT_MAX_CHARS)
)
except Exception as err:
if DEBUG:
logger.debug(err)
示例3: avoid_truncated_word
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def avoid_truncated_word(self, text):
"""Truncate in a way that text will be shorter than max_length and won't be cut in the middle of a word"""
words = text.split()
if not words:
return text
truncator = Truncator(text)
last_word = text.split()[-1]
text = truncator.chars(self.max_length, '')
truncated_last_word = text.split()[-1]
if truncated_last_word != last_word:
# last word is cut. So, remove it
num_words = len(text.split())
text = truncator.words(num_words - 1)
return text
示例4: truncate_chars
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def truncate_chars(data, maxlen):
"""
Truncate string to at most ``maxlen``, including elipsis.
* uses django.utils.text Truncator class.
:param data: string to truncate
:param maxlen: length to truncate to
:returns: string (truncated if necessary)
"""
from django.utils.text import Truncator
if isinstance(data, six.text_type) and len(data) > maxlen:
truncator = Truncator(data)
data = truncator.chars(maxlen)
return data
示例5: describe_operation
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def describe_operation(operation, backwards):
"""Return a string that describes a migration operation for --plan."""
prefix = ''
if hasattr(operation, 'code'):
code = operation.reverse_code if backwards else operation.code
action = code.__doc__ if code else ''
elif hasattr(operation, 'sql'):
action = operation.reverse_sql if backwards else operation.sql
else:
action = ''
if backwards:
prefix = 'Undo '
if action is None:
action = 'IRREVERSIBLE'
is_error = True
else:
action = str(action).replace('\n', '')
is_error = False
if action:
action = ' -> ' + action
truncated = Truncator(action)
return prefix + operation.describe() + truncated.chars(40), is_error
示例6: ellipse_text
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def ellipse_text(text, max_chars):
truncate = Truncator(text)
return truncate.chars(max_chars, u"\u2026")
示例7: markup
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def markup(text, truncate=None):
if truncate is not None:
t = Truncator(text)
text = t.chars(truncate)
return markdown(text, safe_mode=False)
示例8: __str__
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def __str__(self):
truncated_message = Truncator(self.message)
return truncated_message.chars(30) + " TS: " + str(self.time_stamp)
示例9: __str__
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def __str__(self):
truncated_message = Truncator(self.message)
return truncated_message.chars(30)
示例10: __unicode__
# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import chars [as 别名]
def __unicode__(self):
truncator = Truncator(self.message)
return u'%s: %s' % (self.id, truncator.chars(20))