Opened 4 years ago

Closed 3 years ago

#15661 closed New feature (fixed)

LogEntry objects have no unicode method

Reported by: Keryn Knight <keryn@…> Owned by: ShawnMilo
Component: contrib.admin Version: master
Severity: Normal Keywords: logentry unicode
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

This only, as far as I can tell, affects the admin view for deleting objects. When get_deleted_objects is called on an object (eg: an auth.models.User instance) it can find the associated log entries, and display them as part of the list of objects to be culled; as it stands at the moment, when the template displays the data, it just says LogEntry object or some such.

I attach a simple patch that attempts to resolve that by providing a unicode representation of the logentry type, based on whether its an addition, change or deletion. The wording and string formatting takes its cues from the construct_change_message method on ModelAdmin.

As far as I can foresee, having a default representation of some sort would not provide any obstacles, as any usage of log entries currently in use will either be assembling the data itself (as the get_admin_log templatetag does), or using a proxy object to do it, which would have to provide its own unicode (which would take precedence).

I'm not sure there's a legitimate case for a LogEntry not having an object_repr or change_message (both of which are utilised in the patch), but if there is, I've not handled it at current.

Attachments (4)

django-logentry-unicode.diff (780 bytes) - added by Keryn Knight <keryn@…> 4 years ago.
svn diff against revision 15894
django-logentry-unicode.2.diff (780 bytes) - added by Keryn Knight <keryn@…> 4 years ago.
svn diff against revision 15894
15661_logentry_unicode.diff (2.2 KB) - added by ShawnMilo 4 years ago.
Patch modification and regression test, per Luke's suggestion.
15661_refactor.diff (2.1 KB) - added by ShawnMilo 4 years ago.
Better patch after Jacob's comments.

Download all attachments as: .zip

Change History (20)

Changed 4 years ago by Keryn Knight <keryn@…>

svn diff against revision 15894

Changed 4 years ago by Keryn Knight <keryn@…>

svn diff against revision 15894

comment:1 Changed 4 years ago by lukeplant

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

Sounds reasonable. The patch can be very simply optimised - it currently looks up 3 translations and throws 2 away. The easiest solution, if slightly less elegant, is a simple if/elif statement.

This needs a simple test - just create a new class LogEntryTests in tests/regressiontests/admin_util/tests.py, or somewhere else if you can find a better place.

comment:2 Changed 4 years ago by lukeplant

  • Type set to New feature

comment:3 Changed 4 years ago by lukeplant

  • Severity set to Normal

Changed 4 years ago by ShawnMilo

Patch modification and regression test, per Luke's suggestion.

comment:4 Changed 4 years ago by ShawnMilo

  • Easy pickings set

comment:5 Changed 4 years ago by jacob

Sorry to lay on another yet nitpick, but it'd be more Pythonic to just have multiple return statements. So:

if self.action_flag == ADDITION: 
    return ...
elif ...:
    return ...
...
else:
    return ...

(Yes, your CS101 professor taught you that multiple return statements were wrong. He wasn't a Python developer, clearly.)

Changed 4 years ago by ShawnMilo

Better patch after Jacob's comments.

comment:6 Changed 4 years ago by ShawnMilo

Thanks, Jacob. I've updated the patch accordingly.

comment:7 Changed 4 years ago by Alex

The tests really shouldn't call __unicode__() directly, they should call unicode(), also please remove the gratuitous whitespace.

comment:8 Changed 4 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

Close enough, though - marking RFC. Shawn'll probably update the patch before we get a chance to commit anyway :)

comment:9 Changed 4 years ago by ShawnMilo

  • Owner changed from nobody to ShawnMilo

comment:10 Changed 4 years ago by lukeplant

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

In [16120]:

Fixed #15661 - LogEntry objects have no unicode method

Thanks to Keryn Knight for the report and initial patch, and ShawnMilo for
additional work on the patch.

comment:11 Changed 4 years ago by philippedelorme

There is a spelling error in the final patch (due to code reworking I think). In case entry is not ADDITION, CHANGE, DELETION, no value is returned.

Code

return_value = _('LogEntry Object')

Must be replaced by

return _('LogEntry Object')

comment:12 Changed 4 years ago by lukeplant

In [16151]:

Fixed up bad return value introduced in [16120].

Refs #15661. Thanks to philippedelorme for the catch.

comment:13 Changed 3 years ago by anonymous

  • UI/UX unset

I'm getting errors in production (django 1.4) (not local development django 1.3) when I try to delete an AuthUser that has an associated LogEntry. The preview screen that shows all the linked objects does not appear. Instead I get

TypeError: coercing to Unicode: need string or buffer, proxy found

I suspect it is because the unicode method added in this patch is returning a proxy object and not a unicode string.

Here is the full stacktrace...

Traceback (most recent call last):

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/core/handlers/base.py", line 111, in get_response

response = callback(request, *callback_args, callback_kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/options.py", line 366, in wrapper

return self.admin_site.admin_view(view)(*args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/utils/decorators.py", line 91, in _wrapped_view

response = view_func(request, *args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/views/decorators/cache.py", line 89, in _wrapped_view_func

response = view_func(request, *args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/sites.py", line 196, in inner

return view(request, *args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/utils/decorators.py", line 25, in _wrapper

return bound_func(*args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/utils/decorators.py", line 91, in _wrapped_view

response = view_func(request, *args, kwargs)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/utils/decorators.py", line 21, in bound_func

return func(self, *args2, kwargs2)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/options.py", line 1153, in changelist_view

response = self.response_action(request, queryset=cl.get_query_set(request))

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/options.py", line 908, in response_action

response = func(self, request, queryset)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/actions.py", line 35, in delete_selected

queryset, opts, request.user, modeladmin.admin_site, using)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/util.py", line 132, in get_deleted_objects

to_delete = collector.nested(format_callback)

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/util.py", line 186, in nested

roots.extend(self._nested(root, seen, format_callback))

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/util.py", line 169, in _nested

children.extend(self._nested(child, seen, format_callback))

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/util.py", line 171, in _nested

ret = [format_callback(obj)]

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/contrib/admin/util.py", line 130, in format_callback

force_unicode(obj))

File "/home/fieldagent/webapps/fieldagent_no_3/lib/python2.7/django/utils/encoding.py", line 71, in force_unicode

s = unicode(s)

TypeError: coercing to Unicode: need string or buffer, proxy found

comment:14 Changed 3 years ago by cspinelive

I'm no longer anonymous.

I have custom action_flags in my django_admin_log table (something other than 1,2,3 Add, Change, Delete). It was breaking when I deleted a user that had a LogEntry with one of those custom action_flags. The code was falling thru to the final

return _('LogEntry Object')

I imported ugettext at the top and changed that line to

return ugettext('LogEntry Object')

and all is well.

comment:15 Changed 3 years ago by vsf

  • Resolution fixed deleted
  • Status changed from closed to reopened

I've the same problem but I don't have custom action_flags. That solution is no working for me.

comment:16 Changed 3 years ago by claudep

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

This issue (from comment:13) is now tracked in #19114.

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