[Django] #28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen

38 views
Skip to first unread message

Django

unread,
Jun 9, 2017, 9:56:42 PM6/9/17
to django-...@googlegroups.com
#28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen
-------------------------------------+-------------------------------------
Reporter: monotonee | Owner: monotonee
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: 1.11
contrib.admin | Keywords: admin, ModelAdmin,
Severity: Normal | prepopulated_fields, urlify, slug
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
[https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields
ModelAdmin.prepopulated_fields]

After concatenating the contents of all the source fields, the JavaScript
behind ModelAdmin.prepopulated_fields enforces the output string's maximum
length according to the max_length of the destination field, if defined.
Unfortunately, the truncation operation currently occurs after converting
the source string into URL-safe characters and replacing whitespace with
hyphen characters. When the source string is truncated at an index
immediately following a hyphen (formerly a whitespace character), the
string returned by ModelAdmin.prepopulated_fields contains a single,
trailing hyphen that is unnecessary, meaningless, unsightly, and possibly
problematic.

Example of current behavior using URLify from
[https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
django/contrib/admin/static/admin/js/urlify.js]:

{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';

// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;

// Outputs string 'reading-chicken-entrails-for-project-completion-'
// Note trailing hyphen.
console.log(URLify(source_string, max_length, False));
}}}

Example of new, desired behavior:

{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';

// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;

// Outputs string 'reading-chicken-entrails-for-project-completion'
// Note omission of trailing hyphen.
console.log(URLify(source_string, max_length, False));
}}}

This issue can, of course, be solved by custom
[https://docs.djangoproject.com/en/dev/ref/forms/validation/ form field
validation] but, given the spirit and purpose behind
ModelAdmin.prepopulated_fields (slug generation and convenience), I
believe the returns are worth the effort to ensure that the JavaScript
does not return trailing hyphens.

--
Ticket URL: <https://code.djangoproject.com/ticket/28295>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 9, 2017, 10:14:36 PM6/9/17
to django-...@googlegroups.com
#28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen
-------------------------------------+-------------------------------------
Reporter: monotonee | Owner: monotonee
Type: | Status: assigned
Cleanup/optimization |
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: admin, ModelAdmin, | Triage Stage:
prepopulated_fields, urlify, slug | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by monotonee):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:1>

Django

unread,
Jun 9, 2017, 10:31:55 PM6/9/17
to django-...@googlegroups.com
#28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen
-------------------------------------+-------------------------------------
Reporter: monotonee | Owner: monotonee
Type: | Status: assigned
Cleanup/optimization |
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: admin, ModelAdmin, | Triage Stage:
prepopulated_fields, urlify, slug | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Old description:

New description:

[https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields
ModelAdmin.prepopulated_fields]

After concatenating the contents of all the source fields, the JavaScript
behind ModelAdmin.prepopulated_fields enforces the output string's maximum
length according to the max_length of the destination field, if defined.
Unfortunately, the truncation operation currently occurs after converting
the source string into URL-safe characters and replacing whitespace with
hyphen characters. When the source string is truncated at an index
immediately following a hyphen (formerly a whitespace character), the
string returned by ModelAdmin.prepopulated_fields contains a single,
trailing hyphen that is unnecessary, meaningless, unsightly, and possibly
problematic.

Example of current behavior using URLify from
[https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
django/contrib/admin/static/admin/js/urlify.js]:

{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';

// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;

// Outputs string 'reading-chicken-entrails-for-project-completion-'
// Note trailing hyphen.

console.log(URLify(source_string, max_length, false));
}}}

Example of new, desired behavior:

{{{
// source_string.length === 57
source_string = 'Reading Chicken Entrails For Project Completion
Estimates';

// This length will result in source_string truncation to: 'Reading
Chicken Entrails For Project Completion '
max_length = 48;

// Outputs string 'reading-chicken-entrails-for-project-completion'
// Note omission of trailing hyphen.

console.log(URLify(source_string, max_length, false));
}}}

This issue can, of course, be solved by custom
[https://docs.djangoproject.com/en/dev/ref/forms/validation/ form field
validation] but, given the spirit and purpose behind
ModelAdmin.prepopulated_fields (slug generation and convenience), I
believe the returns are worth the effort to ensure that the JavaScript
does not return trailing hyphens.

--

Comment (by monotonee):

Converted Python-style Boolean values to those of JavaScript.

--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:2>

Django

unread,
Jun 10, 2017, 8:24:27 PM6/10/17
to django-...@googlegroups.com
#28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen
-------------------------------------+-------------------------------------
Reporter: monotonee | Owner: monotonee
Type: | Status: assigned
Cleanup/optimization |
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution:
Keywords: admin, ModelAdmin, | Triage Stage: Accepted
prepopulated_fields, urlify, slug |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:3>

Django

unread,
Jun 13, 2017, 3:36:48 PM6/13/17
to django-...@googlegroups.com
#28295: ModelAdmin.prepopulated_fields generates a string with a trailing hyphen
-------------------------------------+-------------------------------------
Reporter: monotonee | Owner: monotonee
Type: | Status: closed

Cleanup/optimization |
Component: contrib.admin | Version: 1.11
Severity: Normal | Resolution: fixed

Keywords: admin, ModelAdmin, | Triage Stage: Accepted
prepopulated_fields, urlify, slug |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7c4f05fae2ae12d698dc28421f5d4659162f928a" 7c4f05fa]:
{{{
#!CommitTicketReference repository=""
revision="7c4f05fae2ae12d698dc28421f5d4659162f928a"
Fixed #28295 -- Made admin's URLify.js trim trailing hyphens.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28295#comment:4>

Reply all
Reply to author
Forward
0 new messages