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.templatetags.admin_list and admin.views.main -- and removed multi-line import in templatetags/admin_list, for Python 2.3 compatibility

git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@1430 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-25 04:37:46 +00:00
parent 8be7d7e544
commit 12dec8023d
2 changed files with 173 additions and 181 deletions

View File

@ -1,18 +1,15 @@
from django.core.template.decorators import simple_tag, inclusion_tag from django.contrib.admin.views.main import MAX_SHOW_ALL_ALLOWED, DEFAULT_RESULTS_PER_PAGE, ALL_VAR
from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
from django.contrib.admin.views.main import MAX_SHOW_ALL_ALLOWED, DEFAULT_RESULTS_PER_PAGE, ALL_VAR, \ from django.contrib.admin.views.main import IS_POPUP_VAR, EMPTY_CHANGELIST_VALUE, MONTHS
ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR , SEARCH_VAR , IS_POPUP_VAR, EMPTY_CHANGELIST_VALUE, \ from django.core import meta, template
MONTHS
from django.utils.translation import get_date_formats
from django.core import meta
from django.utils.text import capfirst
from django.utils.html import strip_tags, escape
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.conf.settings import ADMIN_MEDIA_PREFIX from django.core.template.decorators import simple_tag, inclusion_tag
from django.core import template
from django.utils import dateformat from django.utils import dateformat
from django.utils.html import strip_tags, escape
from django.utils.text import capfirst
from django.utils.translation import get_date_formats
from django.conf.settings import ADMIN_MEDIA_PREFIX
DOT = '.' DOT = '.'
#@simple_tag #@simple_tag
@ -22,7 +19,7 @@ def paginator_number(cl,i):
elif i == cl.page_num: elif i == cl.page_num:
return '<span class="this-page">%d</span> ' % (i+1) return '<span class="this-page">%d</span> ' % (i+1)
else: else:
return '<a href="%s"%s>%d</a> ' % (cl.get_query_string( {PAGE_VAR: i}), (i == cl.paginator.pages-1 and ' class="end"' or ''), i+1) return '<a href="%s"%s>%d</a> ' % (cl.get_query_string({PAGE_VAR: i}), (i == cl.paginator.pages-1 and ' class="end"' or ''), i+1)
paginator_number = simple_tag(paginator_number) paginator_number = simple_tag(paginator_number)
#@inclusion_tag('admin/pagination') #@inclusion_tag('admin/pagination')
@ -59,51 +56,50 @@ def pagination(cl):
page_range.extend(range(page_num + 1, paginator.pages)) page_range.extend(range(page_num + 1, paginator.pages))
need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
return {
return {'cl': cl, 'cl': cl,
'pagination_required': pagination_required, 'pagination_required': pagination_required,
'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR:''}), 'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR: ''}),
'page_range': page_range, 'page_range': page_range,
'ALL_VAR': ALL_VAR, 'ALL_VAR': ALL_VAR,
'1': 1 '1': 1,
} }
pagination = inclusion_tag('admin/pagination')(pagination) pagination = inclusion_tag('admin/pagination')(pagination)
def result_headers(cl): def result_headers(cl):
lookup_opts = cl.lookup_opts lookup_opts = cl.lookup_opts
for i, field_name in enumerate(lookup_opts.admin.list_display): for i, field_name in enumerate(lookup_opts.admin.list_display):
try: try:
f = lookup_opts.get_field(field_name) f = lookup_opts.get_field(field_name)
except meta.FieldDoesNotExist: except meta.FieldDoesNotExist:
# For non-field list_display values, check for the function # For non-field list_display values, check for the function
# attribute "short_description". If that doesn't exist, fall # attribute "short_description". If that doesn't exist, fall
# back to the method name. And __repr__ is a special-case. # back to the method name. And __repr__ is a special-case.
if field_name == '__repr__': if field_name == '__repr__':
header = lookup_opts.verbose_name header = lookup_opts.verbose_name
else:
func = getattr(cl.mod.Klass, field_name) # Let AttributeErrors propogate.
try:
header = func.short_description
except AttributeError:
header = func.__name__.replace('_', ' ')
# Non-field list_display values don't get ordering capability.
yield {"text": header}
else: else:
if isinstance(f.rel, meta.ManyToOne) and f.null: func = getattr(cl.mod.Klass, field_name) # Let AttributeErrors propogate.
yield {"text": f.verbose_name} try:
else: header = func.short_description
th_classes = [] except AttributeError:
new_order_type = 'asc' header = func.__name__.replace('_', ' ')
if field_name == cl.order_field: # Non-field list_display values don't get ordering capability.
th_classes.append('sorted %sending' % cl.order_type.lower()) yield {"text": header}
new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()] else:
if isinstance(f.rel, meta.ManyToOne) and f.null:
yield {"text": f.verbose_name}
else:
th_classes = []
new_order_type = 'asc'
if field_name == cl.order_field:
th_classes.append('sorted %sending' % cl.order_type.lower())
new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]
yield {"text" : f.verbose_name, yield {"text": f.verbose_name,
"sortable": True, "sortable": True,
"url" : cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}), "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
"class_attrib" : (th_classes and ' class="%s"' % ' '.join(th_classes) or '') } "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')}
def items_for_result(cl, result): def items_for_result(cl, result):
first = True first = True
@ -185,11 +181,10 @@ def results(cl):
def result_list(cl): def result_list(cl):
res = list(results(cl)) res = list(results(cl))
return {'cl': cl, return {'cl': cl,
'result_headers': list(result_headers(cl)), 'result_headers': list(result_headers(cl)),
'results': list(results(cl)), } 'results': list(results(cl))}
result_list = inclusion_tag("admin/change_list_results")(result_list) result_list = inclusion_tag("admin/change_list_results")(result_list)
#@inclusion_tag("admin/date_hierarchy") #@inclusion_tag("admin/date_hierarchy")
def date_hierarchy(cl): def date_hierarchy(cl):
lookup_opts, params, lookup_params, lookup_mod = \ lookup_opts, params, lookup_params, lookup_mod = \
@ -214,59 +209,67 @@ def date_hierarchy(cl):
if year_lookup and month_lookup and day_lookup: if year_lookup and month_lookup and day_lookup:
month_name = MONTHS[int(month_lookup)] month_name = MONTHS[int(month_lookup)]
return { 'show': True, return {
'back': 'show': True,
{ 'link' : link({year_field: year_lookup, month_field: month_lookup}), 'back': {
'title': "%s %s" % ( month_name, year_lookup), 'link': link({year_field: year_lookup, month_field: month_lookup}),
}, 'title': "%s %s" % (month_name, year_lookup)
'choices': [ {'title': "%s %s" % ( month_name, day_lookup)} ] },
} 'choices': [{'title': "%s %s" % (month_name, day_lookup)}]
}
elif year_lookup and month_lookup: elif year_lookup and month_lookup:
date_lookup_params = lookup_params.copy() date_lookup_params = lookup_params.copy()
date_lookup_params.update({year_field: year_lookup, month_field: month_lookup}) date_lookup_params.update({year_field: year_lookup, month_field: month_lookup})
days = get_dates('day', date_lookup_params) days = get_dates('day', date_lookup_params)
return { 'show': True, return {
'back': 'show': True,
{ 'link' : link({year_field: year_lookup}), 'back': {
'title' : year_lookup 'link': link({year_field: year_lookup}),
}, 'title': year_lookup
'choices': },
[ { 'link' : link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}), 'choices': [{
'title': day.strftime('%B %d') } for day in days ] 'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
} 'title': day.strftime('%B %d')
} for day in days]
}
elif year_lookup: elif year_lookup:
date_lookup_params = lookup_params.copy() date_lookup_params = lookup_params.copy()
date_lookup_params.update({year_field: year_lookup}) date_lookup_params.update({year_field: year_lookup})
months = get_dates('month', date_lookup_params) months = get_dates('month', date_lookup_params)
return { 'show' : True, return {
'back': 'show' : True,
{ 'link' : link({}), 'back': {
'title': _('All dates') 'link' : link({}),
}, 'title': _('All dates')
'choices': },
[ { 'link': link( {year_field: year_lookup, month_field: month.month}), 'choices': [{
'title': "%s %s" % (month.strftime('%B') , month.year) } for month in months ] 'link': link( {year_field: year_lookup, month_field: month.month}),
} 'title': "%s %s" % (month.strftime('%B') , month.year)
} for month in months]
}
else: else:
years = get_dates('year', lookup_params) years = get_dates('year', lookup_params)
return { 'show': True, return {
'choices': 'show': True,
[ { 'link': link( {year_field: year.year}), 'choices': [{
'title': year.year } for year in years ] 'link': link({year_field: year.year}),
} 'title': year.year
} for year in years ]
}
date_hierarchy = inclusion_tag('admin/date_hierarchy')(date_hierarchy) date_hierarchy = inclusion_tag('admin/date_hierarchy')(date_hierarchy)
#@inclusion_tag('admin/search_form') #@inclusion_tag('admin/search_form')
def search_form(cl): def search_form(cl):
return { 'cl': cl, return {
'show_result_count': cl.result_count != cl.full_result_count and not cl.opts.one_to_one_field, 'cl': cl,
'search_var': SEARCH_VAR } 'show_result_count': cl.result_count != cl.full_result_count and not cl.opts.one_to_one_field,
'search_var': SEARCH_VAR
}
search_form = inclusion_tag('admin/search_form')(search_form) search_form = inclusion_tag('admin/search_form')(search_form)
#@inclusion_tag('admin/filter') #@inclusion_tag('admin/filter')
def filter(cl, spec): def filter(cl, spec):
return {'title': spec.title(), return {'title': spec.title(), 'choices' : list(spec.choices(cl))}
'choices' : list(spec.choices(cl))}
filter = inclusion_tag('admin/filter')(filter) filter = inclusion_tag('admin/filter')(filter)
#@inclusion_tag('admin/filters') #@inclusion_tag('admin/filters')

View File

@ -45,7 +45,6 @@ def _get_mod_opts(app_label, module_name):
raise Http404 # This object is valid but has no admin interface. raise Http404 # This object is valid but has no admin interface.
return mod, opts return mod, opts
def index(request): def index(request):
return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=Context(request)) return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=Context(request))
index = staff_member_required(index) index = staff_member_required(index)
@ -69,7 +68,6 @@ class ChangeList(object):
def get_filters(self, request): def get_filters(self, request):
self.filter_specs = [] self.filter_specs = []
if self.lookup_opts.admin.list_filter and not self.opts.one_to_one_field: if self.lookup_opts.admin.list_filter and not self.opts.one_to_one_field:
filter_fields = [self.lookup_opts.get_field(field_name) \ filter_fields = [self.lookup_opts.get_field(field_name) \
for field_name in self.lookup_opts.admin.list_filter] for field_name in self.lookup_opts.admin.list_filter]
@ -77,7 +75,6 @@ class ChangeList(object):
spec = FilterSpec.create(f, request, self.params) spec = FilterSpec.create(f, request, self.params)
if spec.has_output(): if spec.has_output():
self.filter_specs.append(spec) self.filter_specs.append(spec)
self.has_filters = bool(self.filter_specs) self.has_filters = bool(self.filter_specs)
def get_query_string(self, new_params={}, remove=[]): def get_query_string(self, new_params={}, remove=[]):
@ -93,7 +90,6 @@ class ChangeList(object):
p[k] = v p[k] = v
return '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in p.items()]).replace(' ', '%20') return '?' + '&amp;'.join(['%s=%s' % (k, v) for k, v in p.items()]).replace(' ', '%20')
def get_modules_and_options(self, app_label, module_name, request): def get_modules_and_options(self, app_label, module_name, request):
self.mod, self.opts = _get_mod_opts(app_label, module_name) self.mod, self.opts = _get_mod_opts(app_label, module_name)
if not request.user.has_perm(app_label + '.' + self.opts.get_change_permission()): if not request.user.has_perm(app_label + '.' + self.opts.get_change_permission()):
@ -331,7 +327,6 @@ class AdminBoundField(BoundField):
def html_error_list(self): def html_error_list(self):
return " ".join([form_field.html_error_list() for form_field in self.form_fields if form_field.errors]) return " ".join([form_field.html_error_list() for form_field in self.form_fields if form_field.errors])
class AdminBoundFieldLine(BoundFieldLine): class AdminBoundFieldLine(BoundFieldLine):
def __init__(self, field_line, field_mapping, original): def __init__(self, field_line, field_mapping, original):
super(AdminBoundFieldLine, self).__init__(field_line, field_mapping, original, AdminBoundField) super(AdminBoundFieldLine, self).__init__(field_line, field_mapping, original, AdminBoundField)
@ -382,7 +377,6 @@ class AdminBoundManipulator(BoundManipulator):
return "" return ""
def render_change_form(opts, manipulator, app_label, context, add=False, change=False, show_delete=False, form_url=''): def render_change_form(opts, manipulator, app_label, context, add=False, change=False, show_delete=False, form_url=''):
extra_context = { extra_context = {
'add': add, 'add': add,
'change': change, 'change': change,
@ -391,13 +385,10 @@ def render_change_form(opts, manipulator, app_label, context, add=False, change=
'form_url' : form_url, 'form_url' : form_url,
'app_label': app_label, 'app_label': app_label,
} }
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/%s/change_form" % (app_label, opts.object_name.lower() ),
"admin/%s/change_form" % app_label , "admin/%s/change_form" % app_label ,
"admin/change_form"], "admin/change_form"], context_instance=context)
context_instance=context)
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.attname) pk_value = getattr(new_object, opts.pk.attname)
@ -552,8 +543,6 @@ def change_stage(request, app_label, module_name, object_id):
return render_change_form(opts,manipulator, app_label, c, change=True) return render_change_form(opts,manipulator, app_label, c, change=True)
def _nest_help(obj, depth, val): def _nest_help(obj, depth, val):
current = obj current = obj
for i in range(depth): for i in range(depth):