mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
newforms-admin: Implemented ModelAdminView.change_view(). This is strictly a copy of the preview change_stage() view -- it does not yet use newforms
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4322 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
414f34d9e8
commit
774362dc31
@ -137,7 +137,99 @@ class ModelAdminView(object):
|
||||
|
||||
def change_view(self, request, object_id):
|
||||
"The 'change' admin view for this model."
|
||||
raise NotImplementedError('Change view with object %r' % object_id)
|
||||
model = self.model
|
||||
opts = model._meta
|
||||
app_label = opts.app_label
|
||||
|
||||
if not request.user.has_perm(app_label + '.' + opts.get_change_permission()):
|
||||
raise PermissionDenied
|
||||
|
||||
if request.POST and request.POST.has_key("_saveasnew"):
|
||||
return add_stage(request, app_label, opts.object_name.lower(), form_url='../../add/')
|
||||
|
||||
try:
|
||||
manipulator = model.ChangeManipulator(object_id)
|
||||
except model.DoesNotExist:
|
||||
raise Http404('%s object with primary key %r does not exist' % (model_name, escape(object_id)))
|
||||
|
||||
if request.POST:
|
||||
new_data = request.POST.copy()
|
||||
|
||||
if opts.has_field_type(models.FileField):
|
||||
new_data.update(request.FILES)
|
||||
|
||||
errors = manipulator.get_validation_errors(new_data)
|
||||
manipulator.do_html2python(new_data)
|
||||
|
||||
if not errors:
|
||||
new_object = manipulator.save(new_data)
|
||||
pk_value = new_object._get_pk_val()
|
||||
|
||||
# Construct the change message.
|
||||
change_message = []
|
||||
if manipulator.fields_added:
|
||||
change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and')))
|
||||
if manipulator.fields_changed:
|
||||
change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and')))
|
||||
if manipulator.fields_deleted:
|
||||
change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and')))
|
||||
change_message = ' '.join(change_message)
|
||||
if not change_message:
|
||||
change_message = _('No fields changed.')
|
||||
LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), CHANGE, change_message)
|
||||
|
||||
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
||||
if request.POST.has_key("_continue"):
|
||||
request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
|
||||
if request.REQUEST.has_key('_popup'):
|
||||
return HttpResponseRedirect(request.path + "?_popup=1")
|
||||
else:
|
||||
return HttpResponseRedirect(request.path)
|
||||
elif request.POST.has_key("_saveasnew"):
|
||||
request.user.message_set.create(message=_('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': opts.verbose_name, 'obj': new_object})
|
||||
return HttpResponseRedirect("../%s/" % pk_value)
|
||||
elif request.POST.has_key("_addanother"):
|
||||
request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name))
|
||||
return HttpResponseRedirect("../add/")
|
||||
else:
|
||||
request.user.message_set.create(message=msg)
|
||||
return HttpResponseRedirect("../")
|
||||
else:
|
||||
# Populate new_data with a "flattened" version of the current data.
|
||||
new_data = manipulator.flatten_data()
|
||||
|
||||
# TODO: do this in flatten_data...
|
||||
# If the object has ordered objects on its admin page, get the existing
|
||||
# order and flatten it into a comma-separated list of IDs.
|
||||
id_order_list = []
|
||||
for rel_obj in opts.get_ordered_objects():
|
||||
id_order_list.extend(getattr(manipulator.original_object, 'get_%s_order' % rel_obj.object_name.lower())())
|
||||
if id_order_list:
|
||||
new_data['order_'] = ','.join(map(str, id_order_list))
|
||||
errors = {}
|
||||
|
||||
# Populate the FormWrapper.
|
||||
form = oldforms.FormWrapper(manipulator, new_data, errors)
|
||||
form.original = manipulator.original_object
|
||||
form.order_objects = []
|
||||
|
||||
# TODO: Should be done in flatten_data / FormWrapper construction
|
||||
for related in opts.get_followed_related_objects():
|
||||
wrt = related.opts.order_with_respect_to
|
||||
if wrt and wrt.rel and wrt.rel.to == opts:
|
||||
func = getattr(manipulator.original_object, 'get_%s_list' %
|
||||
related.get_accessor_name())
|
||||
orig_list = func()
|
||||
form.order_objects.extend(orig_list)
|
||||
|
||||
c = template.RequestContext(request, {
|
||||
'title': _('Change %s') % opts.verbose_name,
|
||||
'form': form,
|
||||
'object_id': object_id,
|
||||
'original': manipulator.original_object,
|
||||
'is_popup': request.REQUEST.has_key('_popup'),
|
||||
})
|
||||
return render_change_form(model, manipulator, c, change=True)
|
||||
|
||||
def change_list_view(self, request):
|
||||
"The 'change list' admin view for this model."
|
||||
@ -414,105 +506,6 @@ def add_stage(request, app_label, model_name, show_delete=False, form_url='', po
|
||||
return render_change_form(model, manipulator, c, add=True)
|
||||
add_stage = staff_member_required(never_cache(add_stage))
|
||||
|
||||
def change_stage(request, app_label, model_name, object_id):
|
||||
model = models.get_model(app_label, model_name)
|
||||
object_id = unquote(object_id)
|
||||
if model is None:
|
||||
raise Http404("App %r, model %r, not found" % (app_label, model_name))
|
||||
opts = model._meta
|
||||
|
||||
if not request.user.has_perm(app_label + '.' + opts.get_change_permission()):
|
||||
raise PermissionDenied
|
||||
|
||||
if request.POST and request.POST.has_key("_saveasnew"):
|
||||
return add_stage(request, app_label, model_name, form_url='../../add/')
|
||||
|
||||
try:
|
||||
manipulator = model.ChangeManipulator(object_id)
|
||||
except model.DoesNotExist:
|
||||
raise Http404('%s object with primary key %r does not exist' % (model_name, escape(object_id)))
|
||||
|
||||
if request.POST:
|
||||
new_data = request.POST.copy()
|
||||
|
||||
if opts.has_field_type(models.FileField):
|
||||
new_data.update(request.FILES)
|
||||
|
||||
errors = manipulator.get_validation_errors(new_data)
|
||||
manipulator.do_html2python(new_data)
|
||||
|
||||
if not errors:
|
||||
new_object = manipulator.save(new_data)
|
||||
pk_value = new_object._get_pk_val()
|
||||
|
||||
# Construct the change message.
|
||||
change_message = []
|
||||
if manipulator.fields_added:
|
||||
change_message.append(_('Added %s.') % get_text_list(manipulator.fields_added, _('and')))
|
||||
if manipulator.fields_changed:
|
||||
change_message.append(_('Changed %s.') % get_text_list(manipulator.fields_changed, _('and')))
|
||||
if manipulator.fields_deleted:
|
||||
change_message.append(_('Deleted %s.') % get_text_list(manipulator.fields_deleted, _('and')))
|
||||
change_message = ' '.join(change_message)
|
||||
if not change_message:
|
||||
change_message = _('No fields changed.')
|
||||
LogEntry.objects.log_action(request.user.id, ContentType.objects.get_for_model(model).id, pk_value, str(new_object), CHANGE, change_message)
|
||||
|
||||
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
||||
if request.POST.has_key("_continue"):
|
||||
request.user.message_set.create(message=msg + ' ' + _("You may edit it again below."))
|
||||
if request.REQUEST.has_key('_popup'):
|
||||
return HttpResponseRedirect(request.path + "?_popup=1")
|
||||
else:
|
||||
return HttpResponseRedirect(request.path)
|
||||
elif request.POST.has_key("_saveasnew"):
|
||||
request.user.message_set.create(message=_('The %(name)s "%(obj)s" was added successfully. You may edit it again below.') % {'name': opts.verbose_name, 'obj': new_object})
|
||||
return HttpResponseRedirect("../%s/" % pk_value)
|
||||
elif request.POST.has_key("_addanother"):
|
||||
request.user.message_set.create(message=msg + ' ' + (_("You may add another %s below.") % opts.verbose_name))
|
||||
return HttpResponseRedirect("../add/")
|
||||
else:
|
||||
request.user.message_set.create(message=msg)
|
||||
return HttpResponseRedirect("../")
|
||||
else:
|
||||
# Populate new_data with a "flattened" version of the current data.
|
||||
new_data = manipulator.flatten_data()
|
||||
|
||||
# TODO: do this in flatten_data...
|
||||
# If the object has ordered objects on its admin page, get the existing
|
||||
# order and flatten it into a comma-separated list of IDs.
|
||||
|
||||
id_order_list = []
|
||||
for rel_obj in opts.get_ordered_objects():
|
||||
id_order_list.extend(getattr(manipulator.original_object, 'get_%s_order' % rel_obj.object_name.lower())())
|
||||
if id_order_list:
|
||||
new_data['order_'] = ','.join(map(str, id_order_list))
|
||||
errors = {}
|
||||
|
||||
# Populate the FormWrapper.
|
||||
form = oldforms.FormWrapper(manipulator, new_data, errors)
|
||||
form.original = manipulator.original_object
|
||||
form.order_objects = []
|
||||
|
||||
#TODO Should be done in flatten_data / FormWrapper construction
|
||||
for related in opts.get_followed_related_objects():
|
||||
wrt = related.opts.order_with_respect_to
|
||||
if wrt and wrt.rel and wrt.rel.to == opts:
|
||||
func = getattr(manipulator.original_object, 'get_%s_list' %
|
||||
related.get_accessor_name())
|
||||
orig_list = func()
|
||||
form.order_objects.extend(orig_list)
|
||||
|
||||
c = template.RequestContext(request, {
|
||||
'title': _('Change %s') % opts.verbose_name,
|
||||
'form': form,
|
||||
'object_id': object_id,
|
||||
'original': manipulator.original_object,
|
||||
'is_popup': request.REQUEST.has_key('_popup'),
|
||||
})
|
||||
return render_change_form(model, manipulator, c, change=True)
|
||||
change_stage = staff_member_required(never_cache(change_stage))
|
||||
|
||||
def _nest_help(obj, depth, val):
|
||||
current = obj
|
||||
for i in range(depth):
|
||||
|
Loading…
x
Reference in New Issue
Block a user