Opened 17 years ago

Closed 15 years ago

Last modified 13 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: dev
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 17 years ago.
ellipsis-330.2.diff (3.5 KB ) - added by adamfast 16 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 16 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 16 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@…> 15 years ago.

Download all attachments as: .zip

Change History (14)

by anonymous, 17 years ago

Attachment: ellipsis.diff added

comment:1 by Marc Fargas, 17 years ago

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

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 by adamfast, 16 years ago

Owner: changed from nobody to adamfast
Status: newassigned

by adamfast, 16 years ago

Attachment: ellipsis-330.2.diff added

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

by adamfast, 16 years ago

Attachment: ellipsis-330.diff added

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

by adamfast, 16 years ago

Attachment: ellipsis-330.3.diff added

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

comment:3 by adamfast, 16 years ago

milestone: 1.1
Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady 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 by Jacob, 16 years ago

milestone: 1.11.2

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

comment:5 by Chris Beaven, 16 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

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.

by Travis Cline <travis.cline@…>, 15 years ago

Attachment: 6799.r11858.diff added

comment:6 by Travis Cline <travis.cline@…>, 15 years ago

Patch needs improvement: unset

Added Chris' suggestion, moved to unittest.

comment:7 by Chris Beaven, 15 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Jacob, 15 years ago

Resolution: fixed
Status: assignedclosed

(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 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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