magic-removal: Tiny refactoring/reformatting of admin code
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1829 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1885443c82
commit
3802561731
|
@ -9,8 +9,8 @@
|
||||||
{% block userlinks %}<a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
|
{% block userlinks %}<a href="../../../password_change/">{% trans 'Change password' %}</a> / <a href="../../../logout/">{% trans 'Log out' %}</a>{% endblock %}
|
||||||
{% block breadcrumbs %}{% if not is_popup %}
|
{% block breadcrumbs %}{% if not is_popup %}
|
||||||
<div class="breadcrumbs">
|
<div class="breadcrumbs">
|
||||||
{% path_breadcrumbs path "Home" "1" "1" %}
|
{% path_breadcrumbs path "Home" "1" "1" %}
|
||||||
{% if add %}{% trans "Add" %} {{ bound_manipulator.verbose_name }}{% else %}{{ bound_manipulator.original|striptags|truncatewords:"18" }}{% endif %}
|
{% if add %}{% trans "Add" %} {{ bound_manipulator.verbose_name }}{% else %}{{ bound_manipulator.original|striptags|truncatewords:"18" }}{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}{% endblock %}
|
{% endif %}{% endblock %}
|
||||||
{% block content %}<div id="content-main">
|
{% block content %}<div id="content-main">
|
||||||
|
|
|
@ -10,7 +10,7 @@ def path_breadcrumbs(path, overrides="", front=0, back=0):
|
||||||
text = []
|
text = []
|
||||||
for comp, ov in zip(comps, overs):
|
for comp, ov in zip(comps, overs):
|
||||||
label = ov or comp
|
label = ov or comp
|
||||||
text.append("<a href='%s'>%s</a>›\n" % ("../" * backs, label))
|
text.append("<a href='%s'>%s</a> › \n" % ("../" * backs, label))
|
||||||
backs -= 1
|
backs -= 1
|
||||||
return "".join(text)
|
return "".join(text)
|
||||||
path_breadcrumbs = register.simple_tag(path_breadcrumbs)
|
path_breadcrumbs = register.simple_tag(path_breadcrumbs)
|
||||||
|
|
|
@ -58,18 +58,17 @@ def find_model(mod, remaining):
|
||||||
|
|
||||||
def get_app_label(mod):
|
def get_app_label(mod):
|
||||||
#HACK
|
#HACK
|
||||||
modcomps = mod.__name__.split('.')
|
return mod.__name__.split('.')[-2]
|
||||||
return modcomps[-2]
|
|
||||||
|
|
||||||
def get_model_and_app(path):
|
def get_model_and_app(path):
|
||||||
comps = path.split('/')
|
comps = path.split('/')
|
||||||
comps = comps[:-1] # remove '' after final /
|
comps = comps[:-1] # remove '' after final /
|
||||||
for mod in models.get_installed_models():
|
for mod in models.get_installed_models():
|
||||||
remaining, matched = matches_app(mod, comps)
|
remaining, matched = matches_app(mod, comps)
|
||||||
if matched and len(remaining) > 0:
|
if matched and len(remaining) > 0:
|
||||||
# print "matched ", mod
|
# print "matched ", mod
|
||||||
# print "left", remaining
|
# print "left", remaining
|
||||||
return ( find_model(mod, remaining), get_app_label(mod) )
|
return (find_model(mod, remaining), get_app_label(mod))
|
||||||
|
|
||||||
raise Http404 # Couldn't find app
|
raise Http404 # Couldn't find app
|
||||||
|
|
||||||
|
@ -83,11 +82,11 @@ def url_for_model(model):
|
||||||
for mod in models.get_installed_models():
|
for mod in models.get_installed_models():
|
||||||
remaining, matched = matches_app(mod, comps)
|
remaining, matched = matches_app(mod, comps)
|
||||||
if matched and len(remaining) > 0:
|
if matched and len(remaining) > 0:
|
||||||
comps = comps[: - len(remaining)] + remaining[1:]
|
comps = comps[:-len(remaining)] + remaining[1:]
|
||||||
url = "%s%s/%s/" % (ADMIN_PREFIX, '/'.join(comps) , model.__name__.lower() )
|
url = "%s%s/%s/" % (ADMIN_PREFIX, '/'.join(comps) , model.__name__.lower())
|
||||||
_model_urls[model] = url
|
_model_urls[model] = url
|
||||||
return url
|
return url
|
||||||
raise ImproperlyConfigured('%s is not a model in an installed app' % model.__name__ )
|
raise ImproperlyConfigured, '%s is not a model in an installed app' % model.__name__
|
||||||
|
|
||||||
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))
|
||||||
|
|
|
@ -3,6 +3,7 @@ from django.contrib.admin.views.decorators import staff_member_required
|
||||||
from django.contrib.admin.views.main import get_model_and_app
|
from django.contrib.admin.views.main import get_model_and_app
|
||||||
from django.contrib.admin.views.stages.modify import render_change_form
|
from django.contrib.admin.views.stages.modify import render_change_form
|
||||||
from django.core import formfields, template
|
from django.core import formfields, template
|
||||||
|
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
|
||||||
from django.core.extensions import DjangoContext as Context
|
from django.core.extensions import DjangoContext as Context
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
|
from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect
|
||||||
|
@ -27,15 +28,15 @@ def add_stage(request, path, show_delete=False, form_url='', post_url='../', pos
|
||||||
new_data = request.POST.copy()
|
new_data = request.POST.copy()
|
||||||
if opts.has_field_type(models.FileField):
|
if opts.has_field_type(models.FileField):
|
||||||
new_data.update(request.FILES)
|
new_data.update(request.FILES)
|
||||||
|
|
||||||
#save a copy of the data to use for errors later.
|
#save a copy of the data to use for errors later.
|
||||||
data = new_data.copy()
|
data = new_data.copy()
|
||||||
|
|
||||||
manipulator.do_html2python(new_data)
|
manipulator.do_html2python(new_data)
|
||||||
#update the manipulator with the effects of previous commands.
|
#update the manipulator with the effects of previous commands.
|
||||||
manipulator.update(new_data)
|
manipulator.update(new_data)
|
||||||
#get the errors on the updated shape of the manipulator
|
#get the errors on the updated shape of the manipulator
|
||||||
#HACK - validators should not work on POSTED data directly...
|
#HACK - validators should not work on POSTED data directly...
|
||||||
errors = manipulator.get_validation_errors(data)
|
errors = manipulator.get_validation_errors(data)
|
||||||
if request.POST.has_key("_preview"):
|
if request.POST.has_key("_preview"):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from django.contrib.admin.views.main import get_model_and_app
|
from django.contrib.admin.views.main import get_model_and_app
|
||||||
from django.core import formfields, template
|
from django.core import formfields, template
|
||||||
|
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist, PermissionDenied
|
||||||
from django.core.extensions import DjangoContext as Context
|
from django.core.extensions import DjangoContext as Context
|
||||||
from django.contrib.admin.views.stages.modify import render_change_form
|
from django.contrib.admin.views.stages.modify import render_change_form
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
@ -11,8 +12,6 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise ImproperlyConfigured, "You don't have 'django.contrib.admin' in INSTALLED_APPS."
|
raise ImproperlyConfigured, "You don't have 'django.contrib.admin' in INSTALLED_APPS."
|
||||||
|
|
||||||
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist
|
|
||||||
|
|
||||||
def log_change_message(user, opts, manipulator, new_object):
|
def log_change_message(user, opts, manipulator, new_object):
|
||||||
pk_value = getattr(new_object, opts.pk.column)
|
pk_value = getattr(new_object, opts.pk.column)
|
||||||
# Construct the change message.
|
# Construct the change message.
|
||||||
|
@ -36,7 +35,7 @@ def change_stage(request, path, object_id):
|
||||||
raise PermissionDenied
|
raise PermissionDenied
|
||||||
if request.POST and request.POST.has_key("_saveasnew"):
|
if request.POST and request.POST.has_key("_saveasnew"):
|
||||||
return add_stage(request, path, form_url='../../add/')
|
return add_stage(request, path, form_url='../../add/')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
manipulator = model.ChangeManipulator(object_id)
|
manipulator = model.ChangeManipulator(object_id)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
|
@ -46,15 +45,15 @@ def change_stage(request, path, object_id):
|
||||||
new_data = request.POST.copy()
|
new_data = request.POST.copy()
|
||||||
if opts.has_field_type(models.FileField):
|
if opts.has_field_type(models.FileField):
|
||||||
new_data.update(request.FILES)
|
new_data.update(request.FILES)
|
||||||
|
|
||||||
#save a copy of the data to use for errors later.
|
#save a copy of the data to use for errors later.
|
||||||
data = new_data.copy()
|
data = new_data.copy()
|
||||||
manipulator.do_html2python(new_data)
|
manipulator.do_html2python(new_data)
|
||||||
#update the manipulator with the effects of previous commands.
|
#update the manipulator with the effects of previous commands.
|
||||||
manipulator.update(new_data)
|
manipulator.update(new_data)
|
||||||
#get the errors on the updated shape of the manipulator
|
#get the errors on the updated shape of the manipulator
|
||||||
#HACK - validators should not work on POSTED data directly...
|
#HACK - validators should not work on POSTED data directly...
|
||||||
|
|
||||||
if request.POST.has_key("_preview"):
|
if request.POST.has_key("_preview"):
|
||||||
errors = manipulator.get_validation_errors(data)
|
errors = manipulator.get_validation_errors(data)
|
||||||
elif request.POST.has_key("command"):
|
elif request.POST.has_key("command"):
|
||||||
|
@ -86,11 +85,11 @@ def change_stage(request, path, object_id):
|
||||||
else:
|
else:
|
||||||
request.user.add_message(msg)
|
request.user.add_message(msg)
|
||||||
return HttpResponseRedirect("../../")
|
return HttpResponseRedirect("../../")
|
||||||
else:
|
else:
|
||||||
# Populate new_data with a "flattened" version of the current data.
|
# Populate new_data with a "flattened" version of the current data.
|
||||||
new_data = manipulator.flatten_data()
|
new_data = manipulator.flatten_data()
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
# Populate the FormWrapper.
|
# Populate the FormWrapper.
|
||||||
form = formfields.FormWrapper(manipulator, new_data, errors)
|
form = formfields.FormWrapper(manipulator, new_data, errors)
|
||||||
form.original = manipulator.original_object
|
form.original = manipulator.original_object
|
||||||
|
|
Loading…
Reference in New Issue