1
0
mirror of https://github.com/django/django.git synced 2025-06-29 15:29:13 +00:00

magic-removal: Simplified some code in admin.templatetags.admin_modify

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2090 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-01-21 05:15:29 +00:00
parent 92d2b01a44
commit 05c01c71a4
2 changed files with 23 additions and 34 deletions

View File

@ -4,10 +4,9 @@ from django.template import loader
from django.utils.html import escape from django.utils.html import escape
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.functional import curry from django.utils.functional import curry
from django.db.models import TABULAR, STACKED from django.db import models
from django.db.models.fields import BoundField, Field from django.db.models.fields import BoundField, Field
from django.db.models.related import BoundRelatedObject from django.db.models.related import BoundRelatedObject
from django.db import models
from django.conf import settings from django.conf import settings
import re import re
@ -65,8 +64,7 @@ class FieldWidgetNode(template.Node):
if not cls.nodelists.has_key(klass): if not cls.nodelists.has_key(klass):
try: try:
field_class_name = klass.__name__ field_class_name = klass.__name__
template_name = "widget/%s" % \ template_name = "widget/%s" % class_name_to_underscored(field_class_name)
class_name_to_underscored(field_class_name)
nodelist = loader.get_template(template_name).nodelist nodelist = loader.get_template(template_name).nodelist
except template.TemplateDoesNotExist: except template.TemplateDoesNotExist:
super_klass = bool(klass.__bases__) and klass.__bases__[0] or None super_klass = bool(klass.__bases__) and klass.__bases__[0] or None
@ -142,32 +140,22 @@ class StackedBoundRelatedObject(BoundRelatedObject):
def template_name(self): def template_name(self):
return "admin/edit_inline_stacked" return "admin/edit_inline_stacked"
bound_related_object_overrides = {
TABULAR: TabularBoundRelatedObject,
STACKED: StackedBoundRelatedObject,
}
class EditInlineNode(template.Node): class EditInlineNode(template.Node):
def __init__(self, rel_var): def __init__(self, rel_var):
self.rel_var = rel_var self.rel_var = rel_var
def render(self, context): def render(self, context):
relation = template.resolve_variable(self.rel_var, context) relation = template.resolve_variable(self.rel_var, context)
context.push() context.push()
if relation.field.rel.edit_inline == models.TABULAR:
klass = relation.field.rel.edit_inline bound_related_object_class = TabularBoundRelatedObject
bound_related_object_class = bound_related_object_overrides.get(klass, klass) else:
bound_related_object_class = StackedBoundRelatedObject
original = context.get('original', None) original = context.get('original', None)
bound_related_object = relation.bind(context['form'], original, bound_related_object_class) bound_related_object = relation.bind(context['form'], original, bound_related_object_class)
context['bound_related_object'] = bound_related_object context['bound_related_object'] = bound_related_object
t = loader.get_template(bound_related_object.template_name()) t = loader.get_template(bound_related_object.template_name())
output = t.render(context) output = t.render(context)
context.pop() context.pop()
return output return output
@ -202,22 +190,22 @@ def filter_interface_script_maybe(bound_field):
return '' return ''
filter_interface_script_maybe = register.simple_tag(filter_interface_script_maybe) filter_interface_script_maybe = register.simple_tag(filter_interface_script_maybe)
def do_one_arg_tag(node_factory, parser,token): def field_widget(parser, token):
tokens = token.contents.split() bits = token.contents.split()
if len(tokens) != 2: if len(bits) != 2:
raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0]) raise template.TemplateSyntaxError, "%s takes 1 argument" % bits[0]
return node_factory(tokens[1]) return FieldWidgetNode(bits[1])
field_widget = register.tag(field_widget)
def register_one_arg_tag(node): def edit_inline(parser, token):
tag_name = class_name_to_underscored(node.__name__) bits = token.contents.split()
parse_func = curry(do_one_arg_tag, node) if len(bits) != 2:
register.tag(tag_name, parse_func) raise template.TemplateSyntaxError, "%s takes 1 argument" % bits[0]
return EditInlineNode(bits[1])
register_one_arg_tag(FieldWidgetNode) edit_inline = register.tag(edit_inline)
register_one_arg_tag(EditInlineNode)
def admin_field_line(context, argument_val): def admin_field_line(context, argument_val):
if (isinstance(argument_val, BoundField)): if isinstance(argument_val, BoundField):
bound_fields = [argument_val] bound_fields = [argument_val]
else: else:
bound_fields = [bf for bf in argument_val] bound_fields = [bf for bf in argument_val]

View File

@ -175,9 +175,10 @@ def render_change_form(model, manipulator, context, add=False, change=False, for
'opts': opts, 'opts': opts,
} }
context.update(extra_context) context.update(extra_context)
return render_to_response(["admin/%s/%s/change_form" % (app_label, opts.object_name.lower() ), return render_to_response([
"admin/%s/change_form" % app_label , "admin/%s/%s/change_form" % (app_label, opts.object_name.lower()),
"admin/change_form"], context_instance=context) "admin/%s/change_form" % app_label,
"admin/change_form"], context_instance=context)
def index(request): def index(request):
return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=template.RequestContext(request)) return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=template.RequestContext(request))