#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.

-- 
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/052.56c4d831dfe6c5839584f5074a0603be%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to