Python django.utils.text 模块,Truncator() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.text.Truncator()

项目:NarshaTech    作者:KimJangHyeon    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:django-happenings    作者:natgeosociety    | 项目源码 | 文件源码
def label_for_value(self, value):
        rel_to = self.rel.to
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            related_url = reverse('admin:%s_%s_change' %
                                    (rel_to._meta.app_label,
                                    rel_to._meta.model_name),
                                    args=(value, ),
                                    current_app=self.admin_site.name)
            edit_str = '&nbsp;&nbsp;<a href="%s" title="View" target="_blank">View %s</a>' % (related_url, rel_to._meta.model_name)
            return '&nbsp;<strong>%s</strong>%s' % (
                escape(Truncator(obj).words(14, truncate='...')),
                edit_str
            )

        except (ValueError, self.rel.to.DoesNotExist):
            return ''
项目:Scrum    作者:prakharchoudhary    | 项目源码 | 文件源码
def label_and_url_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return '', ''

        try:
            url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            url = ''  # Admin not registered for target model.

        return Truncator(obj).words(14, truncate='...'), url
项目:tumanov_castleoaks    作者:Roamdev    | 项目源码 | 文件源码
def set_chars(instance, field, max_length=255):
    """ CharField """
    possibles = []
    possibles.extend(get_field_choices(field))
    if possibles:
        setattr(instance, field.name, random.choice(possibles))
        return

    max_length = min(max_length, field.max_length or 16 * 1024)
    for validator in field.validators:
        if isinstance(validator, MaxLengthValidator):
            max_length = min(max_length, validator.limit_value)

    value = generate_lorem_ipsum(1, min_len=1, max_len=max(5, max_length // 8), html=False)
    value = Truncator(value).chars(max_length, html=False)
    setattr(instance, field.name, value)
项目:Gypsy    作者:benticarlos    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def label_for_value(self, value, name=None):
        m_value = value.split(',')
        m_value = [e for e in m_value if e]
        key = self.t_name
        objs = self.r_model._default_manager.using(self.db).filter(**{key+'__in': m_value})
        li_format = '''<a class="btn btn-sm" onclick="removeSingleObject(this,'%s', '%s');">%s</a>'''
        tar_list = ''
        for obj in objs:
            if self.s_name:
                show_val = getattr(obj, self.s_name)
                if callable(show_val):show_val = show_val()
                show_val = escape(Truncator(show_val).words(14, truncate='...'))
            else:
                show_val = escape(Truncator(obj).words(14, truncate='...'))
            val = getattr(obj, key)
            tar_list += li_format%( 'id_'+name, val, show_val)
        return self.label_format %(name, tar_list )
项目:ims    作者:ims-team    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:lifesoundtrack    作者:MTG    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:django-open-lecture    作者:DmLitov4    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:travlr    作者:gauravkulkarni96    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:liberator    作者:libscie    | 项目源码 | 文件源码
def label_and_url_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return '', ''

        try:
            url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            url = ''  # Admin not registered for target model.

        return Truncator(obj).words(14, truncate='...'), url
项目:CSCE482-WordcloudPlus    作者:ggaytan00    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:producthunt    作者:davidgengler    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return ''

        label = '&nbsp;<strong>{}</strong>'
        text = Truncator(obj).words(14, truncate='...')
        try:
            change_url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            pass  # Admin not registered for target model.
        else:
            text = format_html('<a href="{}">{}</a>', change_url, text)

        return format_html(label, text)
项目:django-rtc    作者:scifiswapnil    | 项目源码 | 文件源码
def label_and_url_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return '', ''

        try:
            url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            url = ''  # Admin not registered for target model.

        return Truncator(obj).words(14, truncate='...'), url
项目:LatinSounds_AppEnviaMail    作者:G3ek-aR    | 项目源码 | 文件源码
def label_and_url_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
        except (ValueError, self.rel.model.DoesNotExist):
            return '', ''

        try:
            url = reverse(
                '%s:%s_%s_change' % (
                    self.admin_site.name,
                    obj._meta.app_label,
                    obj._meta.object_name.lower(),
                ),
                args=(obj.pk,)
            )
        except NoReverseMatch:
            url = ''  # Admin not registered for target model.

        return Truncator(obj).words(14, truncate='...'), url
项目:thorn    作者:robinhood    | 项目源码 | 文件源码
def __str__(self):
        return '{0} -> {1}'.format(
            self.event, text.Truncator(self.url).chars(43),
        )
项目:ecs_sclm    作者:meaningful    | 项目源码 | 文件源码
def truncate_words(s, num, end_text='...'):
        truncate = end_text and ' %s' % end_text or ''
        return Truncator(s).words(num, truncate=truncate)
项目:CodingDojo    作者:ComputerSocietyUNB    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.model.DoesNotExist):
            return ''
项目:blog_django    作者:chnpmy    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:blog_django    作者:chnpmy    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:valentina    作者:valentinavc    | 项目源码 | 文件源码
def __str__(self):
        return Truncator(self.content).words(7)
项目:dream_blog    作者:fanlion    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:dream_blog    作者:fanlion    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:MxOnline    作者:myTeemo    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:MxOnline    作者:myTeemo    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:feincms3    作者:matthiask    | 项目源码 | 文件源码
def __str__(self):
        # Return the first few words of the content (with tags stripped)
        return Truncator(strip_tags(self.text)).words(10, truncate=' ...')
项目:djangoblog    作者:liuhuipy    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:djangoblog    作者:liuhuipy    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:sdining    作者:Lurance    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:arbre_competences    作者:nils-van-zuijlen    | 项目源码 | 文件源码
def details_court(self, detail):
        """
        Retourne les 40 premiers caractères du contenu de l'article, suivi de
        points de suspension si le texte est plus long.
        """
        return Truncator(detail.details).chars(40, truncate='...')
项目:djangocms-text-mediumeditor    作者:RauliL    | 项目源码 | 文件源码
def __str__(self):
        return Truncator(strip_tags(self.body)).words(3, truncate="...")
项目:tumanov_castleoaks    作者:Roamdev    | 项目源码 | 文件源码
def set_text(instance, field, paragraphs=1, html=False):
    """ TextField """
    max_len = field.max_length or 16 * 1024
    for validator in field.validators:
        if isinstance(validator, MaxLengthValidator):
            max_len = min(max_len, validator.limit_value)

    value = generate_lorem_ipsum(paragraphs, html=html)
    value = Truncator(value).chars(max_len, html=html)
    setattr(instance, field.name, value)
项目:tumanov_castleoaks    作者:Roamdev    | 项目源码 | 文件源码
def set_slug(instance, field):
    """ SlugField """
    possibles = []
    possibles.extend(get_field_choices(field))
    if possibles:
        setattr(instance, field.name, random.choice(possibles))
        return

    value = slugify(generate_lorem_ipsum(1, min_len=1, max_len=6))
    value = Truncator(value).chars(field.max_length, html=False)
    setattr(instance, field.name, value)
项目:DjangoBlog    作者:0daybug    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ''
项目:xadmin-markdown-editor    作者:bluenknight    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:xadmin-markdown-editor    作者:bluenknight    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:wanblog    作者:wanzifa    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.model.DoesNotExist):
            return ''
项目:tabmaster    作者:NicolasMinghetti    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.model.DoesNotExist):
            return ''
项目:trydjango18    作者:lucifer-yqh    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ''
项目:trydjango18    作者:wei0104    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            return '&nbsp;<strong>%s</strong>' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ''
项目:esdc-ce    作者:erigones    | 项目源码 | 文件源码
def log_name(self):  # Required by task_log
        return Truncator(self.url).chars(32)
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(
                self.db).get(**{key: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, self.rel.to.DoesNotExist):
            return ""
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def _render_label(self, name, value):
        return self.label_format %(name, escape(Truncator(value).words(14, truncate='...')) )
项目:YouPBX    作者:JoneXiong    | 项目源码 | 文件源码
def label_for_value(self, value, name=None):
        m_value = value.split(',')
        m_value = [e for e in m_value if e]
        key = self.rel.get_related_field().name
        objs = objs = self.rel.to._default_manager.using(self.db).filter(**{key+'__in': m_value})
        li_format = '''<a class="btn btn-sm" onclick="removeSingleObject(this,'%s', '%s');">%s</a>'''
        tar_list = ''
        for obj in objs:
            show_val = escape(Truncator(obj).words(14, truncate='...'))
            val = getattr(obj, key)
            tar_list += li_format%( 'id_'+name, val, show_val)
        return self.label_format %(name, tar_list )
项目:docker-compose-tor-demo    作者:rwestergren    | 项目源码 | 文件源码
def video_title(self, obj):
        return Truncator(obj.video.title).chars(50)
项目:django-icekit    作者:ic-labs    | 项目源码 | 文件源码
def __str__(self):
        return Truncator(strip_tags(self.text)).words(20)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def short_description(self, obj):
        return Truncator(obj.description).chars(65)
项目:eduDjango    作者:yuzhou6    | 项目源码 | 文件源码
def label_for_value(self, other_model, rel_name, value):
        try:
            obj = other_model._default_manager.get(**{rel_name: value})
            return '%s' % escape(Truncator(obj).words(14, truncate='...'))
        except (ValueError, other_model.DoesNotExist):
            return ""