Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#12434 closed (fixed)

django.contrib.admin does not work with blank short_description

Reported by: anonymous Owned by: gabrielhurley
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 5 years ago.
Using .capitalize()
12841_no_capitalize.patch (376 bytes) - added by ales_zoulek 5 years ago.
not using .capitalize()
pretty_name_complete.diff (589 bytes) - added by gabrielhurley 5 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 5 years ago by russellm

  • milestone set to 1.2
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 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 5 years ago by ales_zoulek

Using .capitalize()

Changed 5 years ago by ales_zoulek

not using .capitalize()

comment:3 Changed 5 years ago by yedpodtrzitko

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 5 years ago by ales_zoulek

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

So no difference there...

comment:5 Changed 5 years ago by ales_zoulek

  • Cc ales.zoulek@… added

comment:6 Changed 5 years ago by yedpodtrzitko

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

Changed 5 years ago by gabrielhurley

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

comment:7 Changed 5 years ago by gabrielhurley

  • Has patch set
  • Owner changed from nobody to gabrielhurley
  • Status changed from new to assigned

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 5 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

comment:9 Changed 5 years ago by kmtracey

  • Needs tests set
  • Triage Stage changed from Ready for checkin to Accepted

comment:10 Changed 5 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from assigned to closed

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

comment:11 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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