1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

Rationalised use of repr & str. Basically, use %s for any non error messages 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
This commit is contained in:
Robert Wittams 2005-11-01 15:49:13 +00:00
parent 204443cf4c
commit 0f9d7018a0
5 changed files with 29 additions and 28 deletions

View File

@ -13,7 +13,7 @@
{{ bound_field.original_value }} {{ bound_field.original_value }}
{% endif %} {% endif %}
{% if bound_field.raw_id_admin %} {% 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 %}
{% endif %} {% endif %}
{% if bound_field.field.help_text %}<p class="help">{{bound_field.field.help_text}}</p>{% endif %} {% if bound_field.field.help_text %}<p class="help">{{bound_field.field.help_text}}</p>{% endif %}

View File

@ -215,7 +215,7 @@ def filter_interface_script_maybe(bound_field):
f = bound_field.field f = bound_field.field
if f.rel and isinstance(f.rel, meta.ManyToMany) and f.rel.filter_interface: if f.rel and isinstance(f.rel, meta.ManyToMany) and f.rel.filter_interface:
return '<script type="text/javascript">addEvent(window, "load", function(e) {' \ 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) f.name, f.verbose_name, f.rel.filter_interface-1, ADMIN_MEDIA_PREFIX)
else: else:
return '' return ''

View File

@ -78,7 +78,7 @@ class FilterSpec(object):
t.append('<h3>By %s:</h3>\n<ul>\n' % self.title) t.append('<h3>By %s:</h3>\n<ul>\n' % self.title)
for choice in self.choices: 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.is_selected(choice) and ' class="selected"' or ''),
self.get_query_string(choice) , self.get_query_string(choice) ,
self.get_display(choice) ) self.get_display(choice) )
@ -109,7 +109,7 @@ class RelatedFilterSpec(FilterSpec):
cl.get_query_string({}, [self.lookup_kwarg]))) cl.get_query_string({}, [self.lookup_kwarg])))
for val in self.lookup_choices: for val in self.lookup_choices:
pk_val = getattr(val, self.field.rel.to.pk.column) 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 ''), ((self.lookup_val == str(pk_val) and ' class="selected"' or ''),
cl.get_query_string( {self.lookup_kwarg: pk_val}), val)) cl.get_query_string( {self.lookup_kwarg: pk_val}), val))
t.append('</ul>\n\n') t.append('</ul>\n\n')
@ -444,26 +444,26 @@ class AdminBoundField(BoundField):
self.cell_class_attribute = ' class="%s" ' % ' '.join(classes) self.cell_class_attribute = ' class="%s" ' % ' '.join(classes)
self._repr_filled = False self._repr_filled = False
def _fetch_existing_repr(self, func_name): def _fetch_existing_display(self, func_name):
class_dict = self.original.__class__.__dict__ class_dict = self.original.__class__.__dict__
func = class_dict.get(func_name) func = class_dict.get(func_name)
return func(self.original) return func(self.original)
def _fill_existing_repr(self): def _fill_existing_display(self):
if self._repr_filled: if self._display_filled:
return return
#HACK #HACK
if isinstance(self.field.rel, meta.ManyToOne): if isinstance(self.field.rel, meta.ManyToOne):
func_name = 'get_%s' % self.field.name 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): elif isinstance(self.field.rel, meta.ManyToMany):
func_name = 'get_%s_list' % self.field.name func_name = 'get_%s_list' % self.field.name
self._repr = ",".join(self._fetch_existing_repr(func_name)) self._display = ",".join(self._fetch_existing_display(func_name))
self._repr_filled = True self._display_filled = True
def existing_repr(self): def existing_display(self):
self._fill_existing_repr() self._fill_existing_display()
return self._repr return self._display
def __repr__(self): def __repr__(self):
return repr(self.__dict__) 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): def log_add_message(user, opts,manipulator,new_object):
pk_value = getattr(new_object, opts.pk.column) 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): 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) 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) change_message = ' '.join(change_message)
if not change_message: if not change_message:
change_message = 'No fields changed.' 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): def change_stage(request, app_label, module_name, object_id):
mod, opts = _get_mod_opts(app_label, module_name) 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: if related.field.rel.edit_inline or not related.opts.admin:
# Don't display link to edit, because it either has no # Don't display link to edit, because it either has no
# admin or is edited inline. # 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: else:
# Display a link to the admin page. # 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, (capfirst(related.opts.verbose_name), related.opts.app_label, related.opts.module_name,
getattr(sub_obj, related.opts.pk.column), sub_obj), []]) getattr(sub_obj, related.opts.pk.column), sub_obj), []])
_get_deleted_objects(deleted_objects, perms_needed, user, sub_obj, related.opts, current_depth+2) _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: if related.field.rel.edit_inline or not related.opts.admin:
# Don't display link to edit, because it either has no # Don't display link to edit, because it either has no
# admin or is edited inline. # 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: else:
# Display a link to the admin page. # Display a link to the admin page.
nh(deleted_objects, current_depth, ['%s: <a href="../../../../%s/%s/%s/">%s</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, 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) _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 # If there were related objects, and the user doesn't have
# permission to delete them, add the missing perm to perms_needed. # 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 # Don't display link to edit, because it either has no
# admin or is edited inline. # admin or is edited inline.
nh(deleted_objects, current_depth, ['One or more %s in %s: %s' % \ 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: else:
# Display a link to the admin page. # 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>' % \ 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 # If there were related objects, and the user doesn't have
# permission to change them, add the missing perm to perms_needed. # permission to change them, add the missing perm to perms_needed.
if related.opts.admin and has_related_objs: 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 # Populate deleted_objects, a data structure of all related objects that
# will also be deleted. # 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() perms_needed = sets.Set()
_get_deleted_objects(deleted_objects, perms_needed, request.user, obj, opts, 1) _get_deleted_objects(deleted_objects, perms_needed, request.user, obj, opts, 1)
if request.POST: # The user has already confirmed the deletion. if request.POST: # The user has already confirmed the deletion.
if perms_needed: if perms_needed:
raise PermissionDenied raise PermissionDenied
obj_repr = repr(obj) obj_display = str(obj)
obj.delete() obj.delete()
log.log_action(request.user.id, opts.get_content_type_id(), object_id, obj_repr, log.DELETION) 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_repr)) request.user.add_message('The %s "%s" was deleted successfully.' % (opts.verbose_name, obj_display))
return HttpResponseRedirect("../../") return HttpResponseRedirect("../../")
return render_to_response('admin/delete_confirmation', { return render_to_response('admin/delete_confirmation', {
"title": "Are you sure?", "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. # If no history was found, see whether this object even exists.
obj = get_object_or_404(mod, pk=object_id) obj = get_object_or_404(mod, pk=object_id)
return render_to_response('admin/object_history', { return render_to_response('admin/object_history', {
'title': 'Change history: %r' % obj, 'title': 'Change history: %s' % obj,
'action_list': action_list, 'action_list': action_list,
'module_name': capfirst(opts.verbose_name_plural), 'module_name': capfirst(opts.verbose_name_plural),
'object': obj, 'object': obj,

View File

@ -284,7 +284,7 @@ class Field(object):
if self.choices: if self.choices:
return first_choice + list(self.choices) return first_choice + list(self.choices)
rel_obj = self.rel.to 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): def get_choices_default(self):
if(self.radio_admin): if(self.radio_admin):

View File

@ -192,6 +192,7 @@ class RegroupNode(Node):
for obj in obj_list: for obj in obj_list:
grouper = resolve_variable_with_filters('var.%s' % self.expression, \ grouper = resolve_variable_with_filters('var.%s' % self.expression, \
Context({'var': obj})) Context({'var': obj}))
#TODO: Is this a sensible way to determine equality?
if output and repr(output[-1]['grouper']) == repr(grouper): if output and repr(output[-1]['grouper']) == repr(grouper):
output[-1]['list'].append(obj) output[-1]['list'].append(obj)
else: else: