#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)
Change History (14)
by , 17 years ago
Attachment: | ellipsis.diff added |
---|
comment:1 by , 17 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 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 , 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 , 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 , 16 years ago
milestone: | → 1.1 |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
Triage Stage: | Accepted → 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 by , 16 years ago
milestone: | 1.1 → 1.2 |
---|
Unfortunately this is a feature add and we're past feature freeze. Kicking to 1.2.
comment:5 by , 16 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Ready for checkin → 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.
by , 15 years ago
Attachment: | 6799.r11858.diff added |
---|
comment:6 by , 15 years ago
Patch needs improvement: | unset |
---|
Added Chris' suggestion, moved to unittest.
comment:7 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:8 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → 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.
Seems usefull, but: