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


Python Truncator.encode方法代码示例

本文整理汇总了Python中django.utils.text.Truncator.encode方法的典型用法代码示例。如果您正苦于以下问题:Python Truncator.encode方法的具体用法?Python Truncator.encode怎么用?Python Truncator.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.utils.text.Truncator的用法示例。


在下文中一共展示了Truncator.encode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: scrape_data

# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import encode [as 别名]
def scrape_data(long_url):
    hdr = {"User-Agent": "Mozilla/5.0"}
    req = urllib2.Request(long_url, headers=hdr)
    response = urllib2.urlopen(req)
    soup = BeautifulSoup(response, "html.parser")

    title = soup.title.string

    meta_tags = soup.findAll("meta", {"property": "og:description"})
    og_desc = meta_tags[0].get("content", "No description")
    description = Truncator(og_desc).chars(200)

    return {"title": title.encode("utf-8"), "description": description.encode("utf-8")}
开发者ID:danielce,项目名称:urlshortener,代码行数:15,代码来源:tasks.py

示例2: get_item_notification

# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import encode [as 别名]
def get_item_notification(notification):
    '''
        This filter return info about
        one notification of one user
    '''
    idobject = notification.idobject
    is_comment = notification.is_comment

    html = ""
    if is_comment:

        try:
            comment = Comment.objects.get(idcomment=idobject)
            forum = comment.topic.forum.name
            slug = comment.topic.slug
            idtopic = comment.topic.idtopic
            description = Truncator(comment.description).chars(100)
            username = comment.user.username

            url_topic = "/topic/" + forum + "/" + \
                slug + "/" + str(idtopic) + "/"

            title = "<h5><a href='"+url_topic+"'><u>" + \
                comment.topic.title+"</u></h5></a>"

            description = "<p>"+description+"</p>"

            # Get params for url profile
            try:
                params = ""
                params = get_params_url_profile(comment.user)
            except Exception:
                params = ""

            # Data profile
            profile = get_id_profile(comment.user.id)
            photo = get_photo_profile(profile)
            if photo:
                path_img = settings.MEDIA_URL + str(photo)
            else:
                path_img = static("img/profile.png")

            url_profile = URL_PROFILE

            if params:
                user = "<a href='"+url_profile+params + \
                    "'><p>" + username + "</p></a>"
            else:
                user = "<a>" + username + "</a>"

            date = get_datetime_topic(notification.date)

            # Notificacion
            html += '<div class="list-group">'
            html += '   <div class="list-group-item">'
            html += '      <div class="row-action-primary">'
            html += '           <img src="'+path_img + \
                '" width=30 height=30 class="img-circle" />'
            html += '       </div>'
            html += '       <div class="row-content">'
            html += '           <div class="least-content">'+date+'</div>'
            html += '           <h4 class="list-group-item-heading">' + \
                title.encode('utf8')+'</h4>'
            html += '           <p class="list-group-item-text">' + \
                description.encode('utf8')+'</p>'
            html += '           <p>'+user.encode('utf8')+'</p>'
            html += '        </div>'
            html += '   </div>'
            html += '   <div class="list-group-separator"></div>'
            html += '</div>'

        except Comment.DoesNotExist:
            html = ""
    else:
        html = ""

    return html
开发者ID:tawanda,项目名称:django-musette,代码行数:79,代码来源:forum_tags.py

示例3: import_entry

# 需要导入模块: from django.utils.text import Truncator [as 别名]
# 或者: from django.utils.text.Truncator import encode [as 别名]
    def import_entry(self, title, content, item_node):
        """
        Importing an entry but some data are missing like
        related entries, start_publication and end_publication.
        start_publication and creation_date will use the same value,
        wich is always in Wordpress $post->post_date.
        """
        try:
            creation_date = datetime.strptime(
                item_node.find('{%s}post_date_gmt' % WP_NS).text,
                '%Y-%m-%d %H:%M:%S')
        except:
            creation_date = datetime.now()
        if settings.USE_TZ:
            creation_date = timezone.make_aware(
                creation_date, pytz.timezone('GMT'))

        excerpt = item_node.find('{%sexcerpt/}encoded' % WP_NS).text
        if excerpt:
            excerpt = strip_tags(excerpt)
        else:
            if self.auto_excerpt:
                excerpt = Truncator(strip_tags(content)).words(50)
            else:
                excerpt = ''

        # Prefer use this function than
        # item_node.find('{%s}post_name' % WP_NS).text
        # Because slug can be not well formated
        slug = slugify(title)[:255] or 'post-%s' % item_node.find(
            '{%s}post_id' % WP_NS).text

        entry_dict = {
            'title': title,
            'content': content.encode('ascii', 'xmlcharrefreplace'),
            'excerpt': excerpt.encode('ascii', 'xmlcharrefreplace'),
            'tags': ', '.join(self.get_entry_tags(item_node.findall(
                'category')))[:255],
            'status': self.REVERSE_STATUS[item_node.find(
                '{%s}status' % WP_NS).text],
            'comment_enabled': item_node.find(
                '{%s}comment_status' % WP_NS).text == 'open',
            'pingback_enabled': item_node.find(
                '{%s}ping_status' % WP_NS).text == 'open',
            'featured': item_node.find('{%s}is_sticky' % WP_NS).text == '1',
            'password': item_node.find('{%s}post_password' % WP_NS).text or '',
            'login_required': item_node.find(
                '{%s}status' % WP_NS).text == 'private',
            'last_update': timezone.now()}
        entry_dict['trackback_enabled'] = entry_dict['pingback_enabled']

        entry, created = Entry.objects.get_or_create(
            slug=slug, creation_date=creation_date,
            defaults=entry_dict)
        if created:
            entry.categories.add(*self.get_entry_categories(
                item_node.findall('category')))
            entry.authors.add(self.authors[item_node.find(
                '{http://purl.org/dc/elements/1.1/}creator').text])
            entry.sites.add(self.SITE)

        return entry, created
开发者ID:nim65s,项目名称:dotfiles,代码行数:64,代码来源:wp2zinnia.py


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