Python django.template.defaultfilters.truncatewords() Examples

The following are 19 code examples of django.template.defaultfilters.truncatewords(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.template.defaultfilters , or try the search function .
Example #1
Source File: opengraph.py    From online-judge with GNU Affero General Public License v3.0 6 votes vote down vote up
def generate_opengraph(cache_key, data, style):
    metadata = cache.get(cache_key)
    if metadata is None:
        description = None
        tree = reference(markdown(data, style)).tree
        for p in tree.iterfind('.//p'):
            text = p.text_content().strip()
            if text:
                description = text
                break
        if description:
            for remove in (r'\[', r'\]', r'\(', r'\)'):
                description = description.replace(remove, '')
        img = tree.xpath('.//img')
        metadata = truncatewords(description, 60), img[0].get('src') if img else None
        cache.set(cache_key, metadata, 86400)
    return metadata 
Example #2
Source File: feeds.py    From Django-2-by-Example with MIT License 5 votes vote down vote up
def item_description(self, item):
        return truncatewords(item.body, 30) 
Example #3
Source File: grid_tags.py    From steemprojects.com with MIT License 5 votes vote down vote up
def style_repo_description(var):
    truncated_desc = truncatewords(var, 20)
    return truncated_desc 
Example #4
Source File: views.py    From django-examples with MIT License 5 votes vote down vote up
def get_social_description(self):
        return truncatewords(striptags(self.object.text), 50) 
Example #5
Source File: test_truncatewords.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_non_string_input(self):
        self.assertEqual(truncatewords(123, 2), '123') 
Example #6
Source File: test_truncatewords.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_invalid_number(self):
        self.assertEqual(
            truncatewords('A sentence with a few words in it', 'not a number'),
            'A sentence with a few words in it',
        ) 
Example #7
Source File: test_truncatewords.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_overtruncate(self):
        self.assertEqual(
            truncatewords('A sentence with a few words in it', 100),
            'A sentence with a few words in it',
        ) 
Example #8
Source File: test_truncatewords.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_truncate2(self):
        self.assertEqual(
            truncatewords('A sentence with a few words in it', 5),
            'A sentence with a few ...',
        ) 
Example #9
Source File: test_truncatewords.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_truncate(self):
        self.assertEqual(truncatewords('A sentence with a few words in it', 1), 'A ...') 
Example #10
Source File: models.py    From openstax-cms with GNU Affero General Public License v3.0 5 votes vote down vote up
def short_detail(self):
        return truncatewords(self.detail, 15) 
Example #11
Source File: feeds.py    From django_blog with MIT License 5 votes vote down vote up
def item_description(self, item):
        return truncatewords(item.content, 30) 
Example #12
Source File: feeds.py    From Django-3-by-Example with MIT License 5 votes vote down vote up
def item_description(self, item):
        return truncatewords(item.body, 30) 
Example #13
Source File: admin.py    From elasticsearch-django with MIT License 5 votes vote down vote up
def search_terms_(self, instance: SearchQuery) -> str:
        """Return truncated version of search_terms."""
        raw = instance.search_terms
        # take first five words, and further truncate to 50 chars if necessary
        return truncatechars(truncatewords(raw, 5), 50) 
Example #14
Source File: bill.py    From python-opencivicdata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_truncated_title(self, obj):
        return defaultfilters.truncatewords(obj.title, 25) 
Example #15
Source File: bill.py    From python-opencivicdata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_truncated_sponsors(self, obj):
        spons = ", ".join(s.name for s in obj.sponsorships.all()[:5])
        return defaultfilters.truncatewords(spons, 10) 
Example #16
Source File: event.py    From python-opencivicdata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_truncated_event_name(self, obj):
        return defaultfilters.truncatewords(obj.event.name, 8) 
Example #17
Source File: event.py    From python-opencivicdata with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_truncated_description(self, obj):
        return defaultfilters.truncatewords(obj.description, 25) 
Example #18
Source File: admin.py    From django-ra-erp with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_change_message(self, obj):
        return truncatewords(obj.get_change_message(), 23) 
Example #19
Source File: github.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def truncated_title(self):
        """
        This PR's title truncated to 4 words
        """
        return truncatewords(self.title, 4)