1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

new-admin: Negligible formatting changes to admin.templatetgas.admin_modify and template.decorators

git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@1395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-24 04:58:46 +00:00
parent 95564bb882
commit 03d9d060c9
2 changed files with 82 additions and 98 deletions

View File

@ -1,14 +1,12 @@
from django.core import template, template_loader, meta
from django.core.template_loader import render_to_string
from django.conf.settings import ADMIN_MEDIA_PREFIX
from django.utils.text import capfirst
from django.utils.html import escape
from django.utils.text import capfirst
from django.utils.functional import curry
from django.core.template.decorators import simple_tag, inclusion_tag
from django.contrib.admin.views.main import AdminBoundField
from django.core.meta.fields import BoundField, Field
from django.core.meta import BoundRelatedObject, TABULAR, STACKED
from django.conf.settings import ADMIN_MEDIA_PREFIX
import re
word_re = re.compile('[A-Z][a-z]+')
@ -18,11 +16,9 @@ def class_name_to_underscored(name):
#@simple_tag
def include_admin_script(script_path):
return '<script type="text/javascript" src="%s%s"></script>' % \
(ADMIN_MEDIA_PREFIX, script_path)
return '<script type="text/javascript" src="%s%s"></script>' % (ADMIN_MEDIA_PREFIX, script_path)
include_admin_script = simple_tag(include_admin_script)
#@inclusion_tag('admin/submit_line', takes_context=True)
def submit_row(context, bound_manipulator):
change = context['change']
@ -54,14 +50,11 @@ def field_label(bound_field):
if not bound_field.first:
class_names.append('inline')
colon = ":"
class_str = class_names and ' class="%s"' % ' '.join(class_names) or ''
return '<label for="%s"%s>%s%s</label> ' % \
(bound_field.element_id, class_str,
return '<label for="%s"%s>%s%s</label> ' % (bound_field.element_id, class_str, \
capfirst(bound_field.field.verbose_name), colon)
field_label = simple_tag(field_label)
class FieldWidgetNode(template.Node):
nodelists = {}
default = None
@ -92,7 +85,6 @@ class FieldWidgetNode(template.Node):
get_nodelist = classmethod(get_nodelist)
def render(self, context):
bound_field = template.resolve_variable(self.bound_field_var, context)
context.push()
@ -151,7 +143,7 @@ class StackedBoundRelatedObject(BoundRelatedObject):
bound_related_object_overrides = {
TABULAR: TabularBoundRelatedObject,
STACKED : StackedBoundRelatedObject
STACKED: StackedBoundRelatedObject,
}
class EditInlineNode(template.Node):
@ -178,13 +170,11 @@ class EditInlineNode(template.Node):
context.pop()
return output
#@simple_tag
def output_all(form_fields):
return ''.join([str(f) for f in form_fields])
output_all = simple_tag(output_all)
#@simple_tag
def auto_populated_field_script(auto_pop_fields, change = False):
for field in auto_pop_fields:
@ -220,22 +210,19 @@ def do_one_arg_tag(node_factory, parser,token):
raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0])
return node_factory(tokens[1])
one_arg_tag_nodes = [
FieldWidgetNode,
EditInlineNode,
]
def register_one_arg_tag(node):
tag_name = class_name_to_underscored(node.__name__)
parse_func = curry(do_one_arg_tag, node)
template.register_tag(tag_name, parse_func)
one_arg_tag_nodes = (
FieldWidgetNode,
EditInlineNode,
)
for node in one_arg_tag_nodes:
register_one_arg_tag(node)
#@inclusion_tag('admin/field_line', takes_context=True)
def admin_field_line(context, argument_val):
if (isinstance(argument_val, BoundField)):
@ -260,7 +247,7 @@ def admin_field_line(context, argument_val):
'add': context['add'],
'change': context['change'],
'bound_fields': bound_fields,
'class_names' : " ".join(class_names)
'class_names': " ".join(class_names),
}
admin_field_line = inclusion_tag('admin/field_line', takes_context=True)(admin_field_line)

View File

@ -1,10 +1,10 @@
from inspect import getargspec
from django.core.template import Context, Node, TemplateSyntaxError, register_tag, resolve_variable
from django.core.template_loader import render_to_string, get_template
from django.core.template_loader import get_template
from django.utils.functional import curry
from inspect import getargspec
def gen_compile_func(params, defaults, name, node_class, parser, token):
#look in tags for
def generic_tag_compiler(params, defaults, name, node_class, parser, token):
"Returns a template.Node subclass."
bits = token.contents.split()[1:]
bmax = len(params)
def_len = defaults and len(defaults) or 0
@ -14,42 +14,40 @@ def gen_compile_func(params, defaults, name, node_class, parser, token):
message = "%s takes %s arguments" % (name, bmin)
else:
message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
raise TemplateSyntaxError(message)
raise TemplateSyntaxError, message
return node_class(bits)
def simple_tag(func):
(params,_, _, defaults) = getargspec(func)
(params, xx, xxx, defaults) = getargspec(func)
class SimpleNode(Node):
def __init__(self, vars_to_resolve):
#get the vars to resolve
self.vars_to_resolve = vars_to_resolve
def render(self, context):
resolved_vars = [resolve_variable(var, context)
for var in self.vars_to_resolve]
resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
return func(*resolved_vars)
compile_func = curry(gen_compile_func, params, defaults, func.__name__, SimpleNode)
compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, SimpleNode)
compile_func.__doc__ = func.__doc__
register_tag(func.__name__, compile_func)
return func
def inclusion_tag(file_name, context_class=Context, takes_context=False):
def dec(func):
(params,_, _, defaults) = getargspec(func)
(params, xx, xxx, defaults) = getargspec(func)
if takes_context:
if params[0] == 'context':
params = params[1:]
else:
raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'" )
raise TemplateSyntaxError, "Any tag function decorated with takes_context=True must have a first argument of 'context'"
class InclusionNode(Node):
def __init__(self, vars_to_resolve):
self.vars_to_resolve = vars_to_resolve
def render(self, context):
resolved_vars = [resolve_variable(var, context)
for var in self.vars_to_resolve]
resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
if takes_context:
args = [context] + resolved_vars
else:
@ -62,9 +60,8 @@ def inclusion_tag(file_name, context_class=Context, takes_context=False):
self.nodelist = t.nodelist
return self.nodelist.render(context_class(dict))
compile_func = curry(gen_compile_func, params, defaults, func.__name__, InclusionNode)
compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, InclusionNode)
compile_func.__doc__ = func.__doc__
register_tag(func.__name__, compile_func)
return func
return dec