mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +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:
parent
95564bb882
commit
03d9d060c9
@ -1,28 +1,24 @@
|
|||||||
from django.core import template, template_loader, meta
|
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.html import escape
|
||||||
|
from django.utils.text import capfirst
|
||||||
from django.utils.functional import curry
|
from django.utils.functional import curry
|
||||||
from django.core.template.decorators import simple_tag, inclusion_tag
|
from django.core.template.decorators import simple_tag, inclusion_tag
|
||||||
from django.contrib.admin.views.main import AdminBoundField
|
from django.contrib.admin.views.main import AdminBoundField
|
||||||
from django.core.meta.fields import BoundField, Field
|
from django.core.meta.fields import BoundField, Field
|
||||||
from django.core.meta import BoundRelatedObject, TABULAR, STACKED
|
from django.core.meta import BoundRelatedObject, TABULAR, STACKED
|
||||||
|
from django.conf.settings import ADMIN_MEDIA_PREFIX
|
||||||
import re
|
import re
|
||||||
|
|
||||||
word_re = re.compile('[A-Z][a-z]+')
|
word_re = re.compile('[A-Z][a-z]+')
|
||||||
|
|
||||||
def class_name_to_underscored(name):
|
def class_name_to_underscored(name):
|
||||||
return '_'.join([ s.lower() for s in word_re.findall(name)[:-1] ])
|
return '_'.join([s.lower() for s in word_re.findall(name)[:-1]])
|
||||||
|
|
||||||
#@simple_tag
|
#@simple_tag
|
||||||
def include_admin_script(script_path):
|
def include_admin_script(script_path):
|
||||||
return '<script type="text/javascript" src="%s%s"></script>' % \
|
return '<script type="text/javascript" src="%s%s"></script>' % (ADMIN_MEDIA_PREFIX, script_path)
|
||||||
(ADMIN_MEDIA_PREFIX, script_path)
|
|
||||||
include_admin_script = simple_tag(include_admin_script)
|
include_admin_script = simple_tag(include_admin_script)
|
||||||
|
|
||||||
|
|
||||||
#@inclusion_tag('admin/submit_line', takes_context=True)
|
#@inclusion_tag('admin/submit_line', takes_context=True)
|
||||||
def submit_row(context, bound_manipulator):
|
def submit_row(context, bound_manipulator):
|
||||||
change = context['change']
|
change = context['change']
|
||||||
@ -31,11 +27,11 @@ def submit_row(context, bound_manipulator):
|
|||||||
has_delete_permission = context['has_delete_permission']
|
has_delete_permission = context['has_delete_permission']
|
||||||
is_popup = context['is_popup']
|
is_popup = context['is_popup']
|
||||||
return {
|
return {
|
||||||
'onclick_attrib' : (bound_manipulator.ordered_objects and change
|
'onclick_attrib': (bound_manipulator.ordered_objects and change
|
||||||
and 'onclick="submitOrderForm();"' or ''),
|
and 'onclick="submitOrderForm();"' or ''),
|
||||||
'show_delete_link' : (not is_popup and has_delete_permission
|
'show_delete_link': (not is_popup and has_delete_permission
|
||||||
and (change or show_delete)),
|
and (change or show_delete)),
|
||||||
'show_save_as_new' : not is_popup and change and bound_manipulator.save_as,
|
'show_save_as_new': not is_popup and change and bound_manipulator.save_as,
|
||||||
'show_save_and_add_another': not is_popup and (not bound_manipulator.save_as or add),
|
'show_save_and_add_another': not is_popup and (not bound_manipulator.save_as or add),
|
||||||
'show_save_and_continue': not is_popup,
|
'show_save_and_continue': not is_popup,
|
||||||
'show_save': True
|
'show_save': True
|
||||||
@ -54,14 +50,11 @@ def field_label(bound_field):
|
|||||||
if not bound_field.first:
|
if not bound_field.first:
|
||||||
class_names.append('inline')
|
class_names.append('inline')
|
||||||
colon = ":"
|
colon = ":"
|
||||||
|
|
||||||
class_str = class_names and ' class="%s"' % ' '.join(class_names) or ''
|
class_str = class_names and ' class="%s"' % ' '.join(class_names) or ''
|
||||||
return '<label for="%s"%s>%s%s</label> ' % \
|
return '<label for="%s"%s>%s%s</label> ' % (bound_field.element_id, class_str, \
|
||||||
(bound_field.element_id, class_str,
|
capfirst(bound_field.field.verbose_name), colon)
|
||||||
capfirst(bound_field.field.verbose_name), colon )
|
|
||||||
field_label = simple_tag(field_label)
|
field_label = simple_tag(field_label)
|
||||||
|
|
||||||
|
|
||||||
class FieldWidgetNode(template.Node):
|
class FieldWidgetNode(template.Node):
|
||||||
nodelists = {}
|
nodelists = {}
|
||||||
default = None
|
default = None
|
||||||
@ -92,7 +85,6 @@ class FieldWidgetNode(template.Node):
|
|||||||
get_nodelist = classmethod(get_nodelist)
|
get_nodelist = classmethod(get_nodelist)
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
|
|
||||||
bound_field = template.resolve_variable(self.bound_field_var, context)
|
bound_field = template.resolve_variable(self.bound_field_var, context)
|
||||||
|
|
||||||
context.push()
|
context.push()
|
||||||
@ -114,14 +106,14 @@ class FieldWrapper(object):
|
|||||||
|
|
||||||
def use_raw_id_admin(self):
|
def use_raw_id_admin(self):
|
||||||
return isinstance(self.field.rel, (meta.ManyToOne, meta.ManyToMany)) \
|
return isinstance(self.field.rel, (meta.ManyToOne, meta.ManyToMany)) \
|
||||||
and self.field.rel.raw_id_admin
|
and self.field.rel.raw_id_admin
|
||||||
|
|
||||||
class FormFieldCollectionWrapper(object):
|
class FormFieldCollectionWrapper(object):
|
||||||
def __init__(self, field_mapping, fields):
|
def __init__(self, field_mapping, fields):
|
||||||
self.field_mapping = field_mapping
|
self.field_mapping = field_mapping
|
||||||
self.fields = fields
|
self.fields = fields
|
||||||
self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original'])
|
self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original'])
|
||||||
for field in self.fields ]
|
for field in self.fields]
|
||||||
|
|
||||||
class TabularBoundRelatedObject(BoundRelatedObject):
|
class TabularBoundRelatedObject(BoundRelatedObject):
|
||||||
def __init__(self, related_object, field_mapping, original):
|
def __init__(self, related_object, field_mapping, original):
|
||||||
@ -130,8 +122,8 @@ class TabularBoundRelatedObject(BoundRelatedObject):
|
|||||||
|
|
||||||
fields = self.relation.editable_fields()
|
fields = self.relation.editable_fields()
|
||||||
|
|
||||||
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields)
|
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping, fields)
|
||||||
for field_mapping in self.field_mappings]
|
for field_mapping in self.field_mappings]
|
||||||
self.original_row_needed = max([fw.use_raw_id_admin() for fw in self.field_wrapper_list])
|
self.original_row_needed = max([fw.use_raw_id_admin() for fw in self.field_wrapper_list])
|
||||||
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
||||||
|
|
||||||
@ -143,15 +135,15 @@ class StackedBoundRelatedObject(BoundRelatedObject):
|
|||||||
super(StackedBoundRelatedObject, self).__init__(related_object, field_mapping, original)
|
super(StackedBoundRelatedObject, self).__init__(related_object, field_mapping, original)
|
||||||
fields = self.relation.editable_fields()
|
fields = self.relation.editable_fields()
|
||||||
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields)
|
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields)
|
||||||
for field_mapping in self.field_mappings]
|
for field_mapping in self.field_mappings]
|
||||||
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
||||||
|
|
||||||
def template_name(self):
|
def template_name(self):
|
||||||
return "admin/edit_inline_stacked"
|
return "admin/edit_inline_stacked"
|
||||||
|
|
||||||
bound_related_object_overrides = {
|
bound_related_object_overrides = {
|
||||||
TABULAR : TabularBoundRelatedObject,
|
TABULAR: TabularBoundRelatedObject,
|
||||||
STACKED : StackedBoundRelatedObject
|
STACKED: StackedBoundRelatedObject,
|
||||||
}
|
}
|
||||||
|
|
||||||
class EditInlineNode(template.Node):
|
class EditInlineNode(template.Node):
|
||||||
@ -171,26 +163,24 @@ class EditInlineNode(template.Node):
|
|||||||
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 = template_loader.get_template( bound_related_object.template_name() )
|
t = template_loader.get_template(bound_related_object.template_name())
|
||||||
|
|
||||||
output = t.render(context)
|
output = t.render(context)
|
||||||
|
|
||||||
context.pop()
|
context.pop()
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
#@simple_tag
|
#@simple_tag
|
||||||
def output_all(form_fields):
|
def output_all(form_fields):
|
||||||
return ''.join([str(f) for f in form_fields])
|
return ''.join([str(f) for f in form_fields])
|
||||||
output_all = simple_tag(output_all)
|
output_all = simple_tag(output_all)
|
||||||
|
|
||||||
|
|
||||||
#@simple_tag
|
#@simple_tag
|
||||||
def auto_populated_field_script(auto_pop_fields, change = False):
|
def auto_populated_field_script(auto_pop_fields, change = False):
|
||||||
for field in auto_pop_fields:
|
for field in auto_pop_fields:
|
||||||
t = []
|
t = []
|
||||||
if change:
|
if change:
|
||||||
t.append('document.getElementById("id_%s")._changed = true;' % field.name )
|
t.append('document.getElementById("id_%s")._changed = true;' % field.name)
|
||||||
else:
|
else:
|
||||||
t.append('document.getElementById("id_%s").onchange = function() { this._changed = true; };' % field.name)
|
t.append('document.getElementById("id_%s").onchange = function() { this._changed = true; };' % field.name)
|
||||||
|
|
||||||
@ -199,7 +189,7 @@ def auto_populated_field_script(auto_pop_fields, change = False):
|
|||||||
t.append('document.getElementById("id_%s").onkeyup = function() {' \
|
t.append('document.getElementById("id_%s").onkeyup = function() {' \
|
||||||
' var e = document.getElementById("id_%s");' \
|
' var e = document.getElementById("id_%s");' \
|
||||||
' if(!e._changed) { e.value = URLify(%s, %s);} }; ' % (
|
' if(!e._changed) { e.value = URLify(%s, %s);} }; ' % (
|
||||||
f, field.name, add_values, field.maxlength) )
|
f, field.name, add_values, field.maxlength))
|
||||||
return ''.join(t)
|
return ''.join(t)
|
||||||
auto_populated_field_script = simple_tag(auto_populated_field_script)
|
auto_populated_field_script = simple_tag(auto_populated_field_script)
|
||||||
|
|
||||||
@ -220,22 +210,19 @@ def do_one_arg_tag(node_factory, parser,token):
|
|||||||
raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0])
|
raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0])
|
||||||
return node_factory(tokens[1])
|
return node_factory(tokens[1])
|
||||||
|
|
||||||
|
|
||||||
one_arg_tag_nodes = [
|
|
||||||
FieldWidgetNode,
|
|
||||||
EditInlineNode,
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def register_one_arg_tag(node):
|
def register_one_arg_tag(node):
|
||||||
tag_name = class_name_to_underscored(node.__name__)
|
tag_name = class_name_to_underscored(node.__name__)
|
||||||
parse_func = curry(do_one_arg_tag, node)
|
parse_func = curry(do_one_arg_tag, node)
|
||||||
template.register_tag(tag_name, parse_func)
|
template.register_tag(tag_name, parse_func)
|
||||||
|
|
||||||
|
one_arg_tag_nodes = (
|
||||||
|
FieldWidgetNode,
|
||||||
|
EditInlineNode,
|
||||||
|
)
|
||||||
|
|
||||||
for node in one_arg_tag_nodes:
|
for node in one_arg_tag_nodes:
|
||||||
register_one_arg_tag(node)
|
register_one_arg_tag(node)
|
||||||
|
|
||||||
|
|
||||||
#@inclusion_tag('admin/field_line', takes_context=True)
|
#@inclusion_tag('admin/field_line', takes_context=True)
|
||||||
def admin_field_line(context, argument_val):
|
def admin_field_line(context, argument_val):
|
||||||
if (isinstance(argument_val, BoundField)):
|
if (isinstance(argument_val, BoundField)):
|
||||||
@ -257,10 +244,10 @@ def admin_field_line(context, argument_val):
|
|||||||
class_names.append('checkbox-row')
|
class_names.append('checkbox-row')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'add' : context['add'],
|
'add': context['add'],
|
||||||
'change' : context['change'],
|
'change': context['change'],
|
||||||
'bound_fields' : bound_fields,
|
'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)
|
admin_field_line = inclusion_tag('admin/field_line', takes_context=True)(admin_field_line)
|
||||||
|
|
||||||
|
@ -1,55 +1,53 @@
|
|||||||
from inspect import getargspec
|
|
||||||
from django.core.template import Context, Node, TemplateSyntaxError, register_tag, resolve_variable
|
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 django.utils.functional import curry
|
||||||
|
from inspect import getargspec
|
||||||
|
|
||||||
def gen_compile_func(params, defaults, name, node_class, parser, token):
|
def generic_tag_compiler(params, defaults, name, node_class, parser, token):
|
||||||
#look in tags for
|
"Returns a template.Node subclass."
|
||||||
bits = token.contents.split()[1:]
|
bits = token.contents.split()[1:]
|
||||||
bmax = len(params)
|
bmax = len(params)
|
||||||
def_len = defaults and len(defaults) or 0
|
def_len = defaults and len(defaults) or 0
|
||||||
bmin = bmax - def_len
|
bmin = bmax - def_len
|
||||||
if( len(bits) < bmin or len(bits) > bmax ):
|
if(len(bits) < bmin or len(bits) > bmax):
|
||||||
if bmin == bmax:
|
if bmin == bmax:
|
||||||
message = "%s takes %s arguments" % (name, bmin)
|
message = "%s takes %s arguments" % (name, bmin)
|
||||||
else:
|
else:
|
||||||
message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
|
message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
|
||||||
raise TemplateSyntaxError(message)
|
raise TemplateSyntaxError, message
|
||||||
return node_class(bits)
|
return node_class(bits)
|
||||||
|
|
||||||
|
|
||||||
def simple_tag(func):
|
def simple_tag(func):
|
||||||
(params,_, _, defaults) = getargspec(func)
|
(params, xx, xxx, defaults) = getargspec(func)
|
||||||
|
|
||||||
class SimpleNode(Node):
|
class SimpleNode(Node):
|
||||||
def __init__(self, vars_to_resolve):
|
def __init__(self, vars_to_resolve):
|
||||||
#get the vars to resolve
|
|
||||||
self.vars_to_resolve = vars_to_resolve
|
self.vars_to_resolve = vars_to_resolve
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
resolved_vars = [resolve_variable(var, context)
|
resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
|
||||||
for var in self.vars_to_resolve]
|
|
||||||
return func(*resolved_vars)
|
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__
|
compile_func.__doc__ = func.__doc__
|
||||||
register_tag(func.__name__, compile_func)
|
register_tag(func.__name__, compile_func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def inclusion_tag(file_name, context_class=Context, takes_context=False):
|
def inclusion_tag(file_name, context_class=Context, takes_context=False):
|
||||||
def dec(func):
|
def dec(func):
|
||||||
(params,_, _, defaults) = getargspec(func)
|
(params, xx, xxx, defaults) = getargspec(func)
|
||||||
if takes_context:
|
if takes_context:
|
||||||
if params[0] == 'context':
|
if params[0] == 'context':
|
||||||
params = params[1:]
|
params = params[1:]
|
||||||
else:
|
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):
|
class InclusionNode(Node):
|
||||||
def __init__(self, vars_to_resolve):
|
def __init__(self, vars_to_resolve):
|
||||||
self.vars_to_resolve = vars_to_resolve
|
self.vars_to_resolve = vars_to_resolve
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
resolved_vars = [resolve_variable(var, context)
|
resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
|
||||||
for var in self.vars_to_resolve]
|
|
||||||
if takes_context:
|
if takes_context:
|
||||||
args = [context] + resolved_vars
|
args = [context] + resolved_vars
|
||||||
else:
|
else:
|
||||||
@ -62,9 +60,8 @@ def inclusion_tag(file_name, context_class=Context, takes_context=False):
|
|||||||
self.nodelist = t.nodelist
|
self.nodelist = t.nodelist
|
||||||
return self.nodelist.render(context_class(dict))
|
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__
|
compile_func.__doc__ = func.__doc__
|
||||||
register_tag(func.__name__, compile_func)
|
register_tag(func.__name__, compile_func)
|
||||||
return func
|
return func
|
||||||
return dec
|
return dec
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user