Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rationalised use of repr & str. Basically, use %s for any non error m…

…essages going to the user, including log messages. This means people can use __str__ and __repr__ in their models according to normal python conventions.

git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@1042 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0f9d7018a02eeab5466371d3b3441e03e3476893 1 parent 204443c
@rjwittams rjwittams authored
View
2  django/contrib/admin/templates/admin/field_line.html
@@ -13,7 +13,7 @@
{{ bound_field.original_value }}
{% endif %}
{% if bound_field.raw_id_admin %}
- {% if bound_field.existing_repr %}&nbsp;<strong>{{ bound_field.existing_repr|truncatewords:"14" }}</strong>{% endif %}
+ {% if bound_field.existing_display %}&nbsp;<strong>{{ bound_field.existing_display|truncatewords:"14" }}</strong>{% endif %}
{% endif %}
{% endif %}
{% if bound_field.field.help_text %}<p class="help">{{bound_field.field.help_text}}</p>{% endif %}
View
2  django/contrib/admin/templatetags/admin_modify.py
@@ -215,7 +215,7 @@ def filter_interface_script_maybe(bound_field):
f = bound_field.field
if f.rel and isinstance(f.rel, meta.ManyToMany) and f.rel.filter_interface:
return '<script type="text/javascript">addEvent(window, "load", function(e) {' \
- ' SelectFilter.init("id_%s", "%s", %s, %r); });</script>\n' % (
+ ' SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % (
f.name, f.verbose_name, f.rel.filter_interface-1, ADMIN_MEDIA_PREFIX)
else:
return ''
View
50 django/contrib/admin/views/main.py
@@ -78,7 +78,7 @@ def output(self, cl):
t.append('<h3>By %s:</h3>\n<ul>\n' % self.title)
for choice in self.choices:
- t.append('<li%s><a href="%s">%r</a></li>\n' % \
+ t.append('<li%s><a href="%s">%s</a></li>\n' % \
(self.is_selected(choice) and ' class="selected"' or ''),
self.get_query_string(choice) ,
self.get_display(choice) )
@@ -109,7 +109,7 @@ def output(self, cl):
cl.get_query_string({}, [self.lookup_kwarg])))
for val in self.lookup_choices:
pk_val = getattr(val, self.field.rel.to.pk.column)
- t.append('<li%s><a href="%s">%r</a></li>\n' % \
+ t.append('<li%s><a href="%s">%s</a></li>\n' % \
((self.lookup_val == str(pk_val) and ' class="selected"' or ''),
cl.get_query_string( {self.lookup_kwarg: pk_val}), val))
t.append('</ul>\n\n')
@@ -444,26 +444,26 @@ def __init__(self, field, field_mapping, original):
self.cell_class_attribute = ' class="%s" ' % ' '.join(classes)
self._repr_filled = False
- def _fetch_existing_repr(self, func_name):
+ def _fetch_existing_display(self, func_name):
class_dict = self.original.__class__.__dict__
func = class_dict.get(func_name)
return func(self.original)
- def _fill_existing_repr(self):
- if self._repr_filled:
+ def _fill_existing_display(self):
+ if self._display_filled:
return
#HACK
if isinstance(self.field.rel, meta.ManyToOne):
func_name = 'get_%s' % self.field.name
- self._repr = self._fetch_existing_repr(func_name)
+ self._display = self._fetch_existing_display(func_name)
elif isinstance(self.field.rel, meta.ManyToMany):
func_name = 'get_%s_list' % self.field.name
- self._repr = ",".join(self._fetch_existing_repr(func_name))
- self._repr_filled = True
-
- def existing_repr(self):
- self._fill_existing_repr()
- return self._repr
+ self._display = ",".join(self._fetch_existing_display(func_name))
+ self._display_filled = True
+
+ def existing_display(self):
+ self._fill_existing_display()
+ return self._display
def __repr__(self):
return repr(self.__dict__)
@@ -542,7 +542,7 @@ def render_change_form(opts, manipulator, app_label, context, add=False, change=
def log_add_message(user, opts,manipulator,new_object):
pk_value = getattr(new_object, opts.pk.column)
- log.log_action(user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.ADDITION)
+ log.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), log.ADDITION)
def add_stage(request, app_label, module_name, show_delete=False, form_url='', post_url='../', post_url_continue='../%s/', object_id_override=None):
mod, opts = _get_mod_opts(app_label, module_name)
@@ -612,7 +612,7 @@ def log_change_message(user, opts,manipulator,new_object):
change_message = ' '.join(change_message)
if not change_message:
change_message = 'No fields changed.'
- log.log_action(user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.CHANGE, change_message)
+ log.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), log.CHANGE, change_message)
def change_stage(request, app_label, module_name, object_id):
mod, opts = _get_mod_opts(app_label, module_name)
@@ -726,10 +726,10 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current
if related.field.rel.edit_inline or not related.opts.admin:
# Don't display link to edit, because it either has no
# admin or is edited inline.
- nh(deleted_objects, current_depth, ['%s: %r' % (capfirst(related.opts.verbose_name), sub_obj), []])
+ nh(deleted_objects, current_depth, ['%s: %s' % (capfirst(related.opts.verbose_name), sub_obj), []])
else:
# Display a link to the admin page.
- nh(deleted_objects, current_depth, ['%s: <a href="../../../../%s/%s/%s/">%r</a>' % \
+ nh(deleted_objects, current_depth, ['%s: <a href="../../../../%s/%s/%s/">%s</a>' % \
(capfirst(related.opts.verbose_name), related.opts.app_label, related.opts.module_name,
getattr(sub_obj, related.opts.pk.column), sub_obj), []])
_get_deleted_objects(deleted_objects, perms_needed, user, sub_obj, related.opts, current_depth+2)
@@ -740,11 +740,11 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current
if related.field.rel.edit_inline or not related.opts.admin:
# Don't display link to edit, because it either has no
# admin or is edited inline.
- nh(deleted_objects, current_depth, ['%s: %s' % (capfirst(related.opts.verbose_name), strip_tags(repr(sub_obj))), []])
+ nh(deleted_objects, current_depth, ['%s: %s' % (capfirst(related.opts.verbose_name), strip_tags(str(sub_obj))), []])
else:
# Display a link to the admin page.
nh(deleted_objects, current_depth, ['%s: <a href="../../../../%s/%s/%s/">%s</a>' % \
- (capfirst(related.opts.verbose_name), related.opts.app_label, related.opts.module_name, sub_obj.id, strip_tags(repr(sub_obj))), []])
+ (capfirst(related.opts.verbose_name), related.opts.app_label, related.opts.module_name, sub_obj.id, strip_tags(str(sub_obj))), []])
_get_deleted_objects(deleted_objects, perms_needed, user, sub_obj, related.opts, current_depth+2)
# If there were related objects, and the user doesn't have
# permission to delete them, add the missing perm to perms_needed.
@@ -764,11 +764,11 @@ def _get_deleted_objects(deleted_objects, perms_needed, user, obj, opts, current
# Don't display link to edit, because it either has no
# admin or is edited inline.
nh(deleted_objects, current_depth, ['One or more %s in %s: %s' % \
- (related.field.name, related.opts.verbose_name, strip_tags(repr(sub_obj))), []])
+ (related.field.name, related.opts.verbose_name, strip_tags(str(sub_obj))), []])
else:
# Display a link to the admin page.
nh(deleted_objects, current_depth, ['One or more %s in %s: <a href="../../../../%s/%s/%s/">%s</a>' % \
- (related.field.name, related.opts.verbose_name, related.opts.app_label, related.opts.module_name, sub_obj.id, strip_tags(repr(sub_obj))), []])
+ (related.field.name, related.opts.verbose_name, related.opts.app_label, related.opts.module_name, sub_obj.id, strip_tags(str(sub_obj))), []])
# If there were related objects, and the user doesn't have
# permission to change them, add the missing perm to perms_needed.
if related.opts.admin and has_related_objs:
@@ -785,17 +785,17 @@ def delete_stage(request, app_label, module_name, object_id):
# Populate deleted_objects, a data structure of all related objects that
# will also be deleted.
- deleted_objects = ['%s: <a href="../../%s/">%s</a>' % (capfirst(opts.verbose_name), object_id, strip_tags(repr(obj))), []]
+ deleted_objects = ['%s: <a href="../../%s/">%s</a>' % (capfirst(opts.verbose_name), object_id, strip_tags(str(obj))), []]
perms_needed = sets.Set()
_get_deleted_objects(deleted_objects, perms_needed, request.user, obj, opts, 1)
if request.POST: # The user has already confirmed the deletion.
if perms_needed:
raise PermissionDenied
- obj_repr = repr(obj)
+ obj_display = str(obj)
obj.delete()
- log.log_action(request.user.id, opts.get_content_type_id(), object_id, obj_repr, log.DELETION)
- request.user.add_message('The %s "%s" was deleted successfully.' % (opts.verbose_name, obj_repr))
+ log.log_action(request.user.id, opts.get_content_type_id(), object_id, obj_display, log.DELETION)
+ request.user.add_message('The %s "%s" was deleted successfully.' % (opts.verbose_name, obj_display))
return HttpResponseRedirect("../../")
return render_to_response('admin/delete_confirmation', {
"title": "Are you sure?",
@@ -813,7 +813,7 @@ def history(request, app_label, module_name, object_id):
# If no history was found, see whether this object even exists.
obj = get_object_or_404(mod, pk=object_id)
return render_to_response('admin/object_history', {
- 'title': 'Change history: %r' % obj,
+ 'title': 'Change history: %s' % obj,
'action_list': action_list,
'module_name': capfirst(opts.verbose_name_plural),
'object': obj,
View
2  django/core/meta/fields.py
@@ -284,7 +284,7 @@ def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
if self.choices:
return first_choice + list(self.choices)
rel_obj = self.rel.to
- return first_choice + [(getattr(x, rel_obj.pk.column), repr(x)) for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)]
+ return first_choice + [(getattr(x, rel_obj.pk.column), str(x)) for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)]
def get_choices_default(self):
if(self.radio_admin):
View
1  django/core/template/defaulttags.py
@@ -192,6 +192,7 @@ def render(self, context):
for obj in obj_list:
grouper = resolve_variable_with_filters('var.%s' % self.expression, \
Context({'var': obj}))
+ #TODO: Is this a sensible way to determine equality?
if output and repr(output[-1]['grouper']) == repr(grouper):
output[-1]['list'].append(obj)
else:
Please sign in to comment.
Something went wrong with that request. Please try again.