Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#12434 closed (fixed)

django.contrib.admin does not work with blank short_description

Reported by: anonymous Owned by: Gabriel Hurley
Component: contrib.admin Version: master
Severity: Keywords:
Cc: ales.zoulek@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

After [11965] django.contrib.admin is broken for functions with blank short_description attribute.

I think the problem is with this function (django.contrib.forms.forms):

def pretty_name(name):
    "Converts 'first_name' to 'First name'"
    name = name[0].upper() + name[1:]
    return name.replace('_', ' ')

In django.contrib.admin.templatetags.admin_list, line 85, function label_for_field returns blank header for functions with blank short_description:

            header = label_for_field(field_name, cl.model, cl.model_admin)

And line 91 use header as parameter for pretty_name:

            header = pretty_name(header)

Attachments (3)

12841.patch (432 bytes) - added by Ales Zoulek 7 years ago.
Using .capitalize()
12841_no_capitalize.patch (376 bytes) - added by Ales Zoulek 7 years ago.
not using .capitalize()
pretty_name_complete.diff (589 bytes) - added by Gabriel Hurley 7 years ago.
Patches pretty_name to use capitalize() and work with None and/or blank strings as argument.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 7 years ago by Russell Keith-Magee

milestone: 1.2
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by Ales Zoulek

Is there a specific reason not to use .capitalize()

  • That means that pretty_name(some_NamE) is translated to "Some NamE" not to "Some name"?

Changed 7 years ago by Ales Zoulek

Attachment: 12841.patch added

Using .capitalize()

Changed 7 years ago by Ales Zoulek

Attachment: 12841_no_capitalize.patch added

not using .capitalize()

comment:3 Changed 7 years ago by Jiri Suchan

AFAIK capitalizce() doesn't work on non-ascii chars:

yed@deflorenc ~/skript/codegolf $ python
Python 2.6.4 (r264:75706, Feb  2 2010, 11:10:56) 
[GCC 4.4.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'ř'.capitalize()
ř

comment:4 Changed 7 years ago by Ales Zoulek

yedpodtrzitko: Yep, but neither does 'ř'.upper()

So no difference there...

comment:5 Changed 7 years ago by Ales Zoulek

Cc: ales.zoulek@… added

comment:6 Changed 7 years ago by Jiri Suchan

You're right, I didn't try it. Sorry.

Changed 7 years ago by Gabriel Hurley

Attachment: pretty_name_complete.diff added

Patches pretty_name to use capitalize() and work with None and/or blank strings as argument.

comment:7 Changed 7 years ago by Gabriel Hurley

Has patch: set
Owner: changed from nobody to Gabriel Hurley
Status: newassigned

Capitalize works fine for international characters as long as the string is a unicode string:

>>> print u'ñ'.capitalize()
Ñ

Even though it's probably overkill for a small patch, I ran some timeit tests to see how various solutions to this problem compared:

>>> import timeit
>>> t1 = """\
... name = u'el_ñino_season'
... name.capitalize().replace('_', ' ')
... """
>>> timeit.timeit(t1)
2.1008538794735085
>>> t2 = """\
... name = u'el_ñino_season'
... name.upper().replace('_', ' ')
... """
>>> timeit.timeit(t2)
2.2086846233250319
>>> t3 = """\
... name = u'el_ñino_season'
... name = name.upper()
... name.replace('_', ' ')
... """
>>> timeit.timeit(t3)
2.1825877311222515
>>> t4 = """\
... name = u'el_ñino_season'
... if not name:
...     return u''
... name.replace('_', ' ').capitalize()
... """
>>> timeit.timeit(t4)
2.1088863905887484

My patch reflects option 4, which is consistently fast and handles the case of name being None.

comment:8 Changed 7 years ago by Jacob

Triage Stage: AcceptedReady for checkin

comment:9 Changed 7 years ago by Karen Tracey

Needs tests: set
Triage Stage: Ready for checkinAccepted

comment:10 Changed 7 years ago by Karen Tracey

Resolution: fixed
Status: assignedclosed

(In [12794]) Fixed #12434: Made pretty_name handle empty string and None as input. Thanks ales_zoulek and gabrielhurley.

comment:11 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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