当前位置: 首页>>代码示例>>Python>>正文


Python Truncator.chars方法代码示例

本文整理汇总了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>')
开发者ID:Lisaveta-K,项目名称:lisaveta-k.github.io,代码行数:36,代码来源:products_yandex_yml.py

示例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)
开发者ID:barseghyanartur,项目名称:django-dash,代码行数:29,代码来源:forms.py

示例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
开发者ID:clabra,项目名称:awesome-slugify,代码行数:16,代码来源:main.py

示例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
开发者ID:shuiziliu,项目名称:django-garage,代码行数:17,代码来源:text_utils.py

示例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
开发者ID:GravyHands,项目名称:django,代码行数:24,代码来源:migrate.py

示例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")
开发者ID:ianmintz,项目名称:pourover,代码行数:6,代码来源:utils.py

示例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)
开发者ID:Qkolnek,项目名称:pyspain-web,代码行数:8,代码来源:utils.py

示例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)
开发者ID:shunyaorad,项目名称:web-applications,代码行数:5,代码来源:models.py

示例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)
开发者ID:Sbk3824,项目名称:board_django,代码行数:5,代码来源:models.py

示例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))
开发者ID:Nobody109,项目名称:django-js-error-logging,代码行数:5,代码来源:models.py


注:本文中的django.utils.text.Truncator.chars方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。