Opened 14 years ago
Closed 12 years ago
#15661 closed New feature (fixed)
LogEntry objects have no unicode method
Reported by: | Owned by: | ShawnMilo | |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
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)
Change History (20)
by , 14 years ago
Attachment: | django-logentry-unicode.diff added |
---|
comment:1 by , 14 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → 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 by , 14 years ago
Type: | → New feature |
---|
comment:3 by , 14 years ago
Severity: | → Normal |
---|
by , 14 years ago
Attachment: | 15661_logentry_unicode.diff added |
---|
Patch modification and regression test, per Luke's suggestion.
comment:4 by , 14 years ago
Easy pickings: | set |
---|
comment:5 by , 14 years ago
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.)
comment:7 by , 14 years ago
The tests really shouldn't call __unicode__()
directly, they should call unicode()
, also please remove the gratuitous whitespace.
comment:8 by , 14 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Close enough, though - marking RFC. Shawn'll probably update the patch before we get a chance to commit anyway :)
comment:9 by , 14 years ago
Owner: | changed from | to
---|
comment:11 by , 14 years ago
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:13 by , 12 years ago
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 by , 12 years ago
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 by , 12 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
I've the same problem but I don't have custom action_flags. That solution is no working for me.
comment:16 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
This issue (from comment:13) is now tracked in #19114.
svn diff against revision 15894