#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:
> [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.
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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.74a591e43e2ed7305eb5c3865fc5f0d8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.