Opened 7 years ago

Closed 5 years ago

Last modified 4 years ago

#6799 closed (fixed)

Update to truncate words methods to make them more callable

Reported by: bret Owned by: adamfast
Component: Contrib apps Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In certain situations, the functionality of the truncate_words and truncate_html_words functions are needed, but the default action of appending an ellipsis is not desirable.

Currently, truncate_words and truncate_html_words functions only accept a single parameter -- an int representing the number of words to allow.

If these functions replace the hard coded ellipsis with a value passed in as a parameter, it would increase their usefulness.
The attached patch does this by adding an end_text parameter with a default value of '...'. If the function is called without being passed the end_text parameter, it will function as it always has.

Adding this parameter would allow a user to easily utilize the contributed truncating code in his own code.

An example:

from django.contrib.utils.text import truncate_html_words
class Entry(models.Model):
    body = models.TextField()

    def truncated(self):
        return truncate_html_words(self.body, 100, '<a href="' + self.get_absolute_url + '">Continue...</a>')

    def get_absolute_url(self):
        ....

object.truncated could then be used in a template in place of object.body|truncatewords_html.

Attachments (5)

ellipsis.diff (1.3 KB) - added by anonymous 7 years ago.
ellipsis-330.2.diff (3.5 KB) - added by adamfast 6 years ago.
Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures.
ellipsis-330.diff (3.5 KB) - added by adamfast 6 years ago.
Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures.
ellipsis-330.3.diff (3.5 KB) - added by adamfast 6 years ago.
Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures it introduced.
6799.r11858.diff (4.9 KB) - added by Travis Cline <travis.cline@…> 5 years ago.

Download all attachments as: .zip

Change History (14)

Changed 7 years ago by anonymous

comment:1 Changed 7 years ago by telenieko

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

Seems usefull, but:

  • The patch *must* include the documentation refering to the change introduced.
  • The patch *should* include tests for the chances introduced.

comment:2 Changed 6 years ago by adamfast

  • Owner changed from nobody to adamfast
  • Status changed from new to assigned

Changed 6 years ago by adamfast

Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures.

Changed 6 years ago by adamfast

Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures.

Changed 6 years ago by adamfast

Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures it introduced.

comment:3 Changed 6 years ago by adamfast

  • milestone set to 1.1
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

The latest diff (is the right one, I screwed up entering the other ones)

Can't find documentation for django.contrib.utils to update the documentation, but the change is not made to the truncatewords builtin filter so no public doc change necessary there (the docstrings of the affected functions have been updated, however).

I did fix earlier patch's truncate_words_html method to not cause existing tests to fail.

There were no existing tests (aside from the template tests, which wouldn't have access to this method) for any of the functions in django.contrib.utils.text, so I've added regressiontests.utils.text to give them a workout and updated the regressiontests.utils.tests file to run them.

comment:4 Changed 6 years ago by jacob

  • milestone changed from 1.1 to 1.2

Unfortunately this is a feature add and we're past feature freeze. Kicking to 1.2.

comment:5 Changed 6 years ago by SmileyChris

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

If you're going to do this, you may as well only do the appending of end_text if it's not false. This will avoid adding trailing whitespace.

Changed 5 years ago by Travis Cline <travis.cline@…>

comment:6 Changed 5 years ago by Travis Cline <travis.cline@…>

  • Patch needs improvement unset

Added Chris' suggestion, moved to unittest.

comment:7 Changed 5 years ago by SmileyChris

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 5 years ago by jacob

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [12431]) Fixed #6799 - added an end_text argument to truncate_words/truncate_html_words.

This allows customizing the standard "..." end text. Yes, this is technically a
feature sneaking in after the deadline, but I just couldn't bring myself to punt
it again: we already used that excuse for not getting it into 1.1.

Thanks to Adam Fast and Travis Cline for work on this patch.

comment:9 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top