Opened 15 years ago

Closed 15 years ago

Last modified 13 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: dev
Severity: Keywords:
Cc: ales.zoulek@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

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 15 years ago.
Using .capitalize()
12841_no_capitalize.patch (376 bytes ) - added by Ales Zoulek 15 years ago.
not using .capitalize()
pretty_name_complete.diff (589 bytes ) - added by Gabriel Hurley 15 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 by Russell Keith-Magee, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:2 by Ales Zoulek, 15 years ago

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"?

by Ales Zoulek, 15 years ago

Attachment: 12841.patch added

Using .capitalize()

by Ales Zoulek, 15 years ago

Attachment: 12841_no_capitalize.patch added

not using .capitalize()

comment:3 by Jiri Suchan, 15 years ago

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 by Ales Zoulek, 15 years ago

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

So no difference there...

comment:5 by Ales Zoulek, 15 years ago

Cc: ales.zoulek@… added

comment:6 by Jiri Suchan, 15 years ago

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

by Gabriel Hurley, 15 years ago

Attachment: pretty_name_complete.diff added

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

comment:7 by Gabriel Hurley, 15 years ago

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 by Jacob, 15 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Karen Tracey, 15 years ago

Needs tests: set
Triage Stage: Ready for checkinAccepted

comment:10 by Karen Tracey, 15 years ago

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 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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