magic-removal: Command fixes. Add & remove now work properly with errors, and it works in both stacked and tabular views.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1772 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
4d3374c652
commit
4bf7d2c81c
|
@ -7,12 +7,16 @@ 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
|
||||||
from django.utils.text import capfirst, get_text_list
|
from django.utils.text import capfirst, get_text_list
|
||||||
|
try:
|
||||||
|
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
|
||||||
|
except ImportError:
|
||||||
|
raise ImproperlyConfigured, "You don't have 'django.contrib.admin' in INSTALLED_APPS."
|
||||||
|
|
||||||
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)
|
||||||
LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), ADDITION)
|
LogEntry.objects.log_action(user.id, opts.get_content_type_id(), pk_value, str(new_object), ADDITION)
|
||||||
|
|
||||||
def add_stage(request, path, show_delete=False, form_url='', post_url='../change/', post_url_continue='../%s/', object_id_override=None):
|
def add_stage(request, path, show_delete=False, form_url='', post_url='../', post_url_continue='../%s/change', object_id_override=None):
|
||||||
model, app_label = get_model_and_app(path)
|
model, app_label = get_model_and_app(path)
|
||||||
opts = model._meta
|
opts = model._meta
|
||||||
|
|
||||||
|
@ -23,11 +27,34 @@ def add_stage(request, path, show_delete=False, form_url='', post_url='../change
|
||||||
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)
|
||||||
|
|
||||||
|
if request.POST.has_key("command"):
|
||||||
|
#save a copy of the data to use for errors later.
|
||||||
|
data = new_data.copy()
|
||||||
|
|
||||||
|
manipulator.do_html2python(new_data)
|
||||||
|
command_name = request.POST.get("command")
|
||||||
|
manipulator.do_command(new_data, command_name)
|
||||||
|
new_data = manipulator.flatten_data()
|
||||||
|
|
||||||
|
#HACK - validators should not work on POSTED data directly...
|
||||||
|
errors = manipulator.get_validation_errors(data)
|
||||||
|
elif request.POST.has_key("_preview"):
|
||||||
errors = manipulator.get_validation_errors(new_data)
|
errors = manipulator.get_validation_errors(new_data)
|
||||||
manipulator.do_html2python(new_data)
|
manipulator.do_html2python(new_data)
|
||||||
|
else:
|
||||||
|
#save a copy of the data to use for errors later.
|
||||||
|
data = new_data.copy()
|
||||||
|
|
||||||
if not errors and not request.POST.has_key("_preview"):
|
manipulator.do_html2python(new_data)
|
||||||
new_object = manipulator.save(new_data)
|
manipulator.update(new_data)
|
||||||
|
errors = manipulator.get_validation_errors(data)
|
||||||
|
if errors:
|
||||||
|
data = manipulator.flatten_data()
|
||||||
|
data.update(new_data)
|
||||||
|
new_data = data
|
||||||
|
else:
|
||||||
|
new_object = manipulator.save_from_update()
|
||||||
log_add_message(request.user, opts, manipulator, new_object)
|
log_add_message(request.user, opts, manipulator, new_object)
|
||||||
msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
||||||
pk_value = getattr(new_object, opts.pk.attname)
|
pk_value = getattr(new_object, opts.pk.attname)
|
||||||
|
|
|
@ -47,16 +47,33 @@ def change_stage(request, path, object_id):
|
||||||
if opts.has_field_type(models.FileField):
|
if opts.has_field_type(models.FileField):
|
||||||
new_data.update(request.FILES)
|
new_data.update(request.FILES)
|
||||||
|
|
||||||
errors = manipulator.get_validation_errors(new_data)
|
if request.POST.has_key("command"):
|
||||||
|
#save a copy of the data to use for errors later.
|
||||||
|
data = new_data.copy()
|
||||||
|
|
||||||
manipulator.do_html2python(new_data)
|
manipulator.do_html2python(new_data)
|
||||||
if not errors:
|
|
||||||
if request.POST.has_key("command"):
|
|
||||||
command_name = request.POST.get("command")
|
command_name = request.POST.get("command")
|
||||||
manipulator.do_command(new_data, command_name)
|
manipulator.do_command(new_data, command_name)
|
||||||
new_data = manipulator.flatten_data()
|
new_data = manipulator.flatten_data()
|
||||||
elif not request.POST.has_key("_preview"):
|
|
||||||
new_object = manipulator.save(new_data)
|
#HACK - validators should not work on POSTED data directly...
|
||||||
|
errors = manipulator.get_validation_errors(data)
|
||||||
|
elif request.POST.has_key("_preview"):
|
||||||
|
errors = manipulator.get_validation_errors(new_data)
|
||||||
|
manipulator.do_html2python(new_data)
|
||||||
|
else:
|
||||||
|
#save a copy of the data to use for errors later.
|
||||||
|
data = new_data.copy()
|
||||||
|
|
||||||
|
manipulator.do_html2python(new_data)
|
||||||
|
manipulator.update(new_data)
|
||||||
|
errors = manipulator.get_validation_errors(data)
|
||||||
|
if errors:
|
||||||
|
flat_data = manipulator.flatten_data()
|
||||||
|
flat_data.update(new_data)
|
||||||
|
new_data = flat_data
|
||||||
|
else:
|
||||||
|
new_object = manipulator.save_from_update()
|
||||||
log_change_message(request.user, opts, manipulator, new_object)
|
log_change_message(request.user, opts, manipulator, new_object)
|
||||||
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj': new_object}
|
||||||
pk_value = getattr(new_object, opts.pk.attname)
|
pk_value = getattr(new_object, opts.pk.attname)
|
||||||
|
@ -78,15 +95,6 @@ def change_stage(request, path, object_id):
|
||||||
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()
|
||||||
# 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 = {}
|
errors = {}
|
||||||
|
|
||||||
# Populate the FormWrapper.
|
# Populate the FormWrapper.
|
||||||
|
@ -94,15 +102,6 @@ def change_stage(request, path, object_id):
|
||||||
form.original = manipulator.original_object
|
form.original = manipulator.original_object
|
||||||
form.order_objects = []
|
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._meta == opts:
|
|
||||||
func = getattr(manipulator.original_object, 'get_%s_list' %
|
|
||||||
related.get_method_name_part())
|
|
||||||
orig_list = func()
|
|
||||||
form.order_objects.extend(orig_list)
|
|
||||||
|
|
||||||
c = Context(request, {
|
c = Context(request, {
|
||||||
'title': _('Change %s') % opts.verbose_name,
|
'title': _('Change %s') % opts.verbose_name,
|
||||||
'form': form,
|
'form': form,
|
||||||
|
|
|
@ -247,7 +247,7 @@ class InlineObjectCollection:
|
||||||
#orig_list = self.rel_obj.get_list(orig)
|
#orig_list = self.rel_obj.get_list(orig)
|
||||||
|
|
||||||
for i, manip in enumerate(self.child_manips) :
|
for i, manip in enumerate(self.child_manips) :
|
||||||
if manip:
|
if manip and not manip.needs_deletion:
|
||||||
collection = {'original': manip.original_object}
|
collection = {'original': manip.original_object}
|
||||||
for field in manip.fields:
|
for field in manip.fields:
|
||||||
errors = self.errors.get(field.field_name, [])
|
errors = self.errors.get(field.field_name, [])
|
||||||
|
@ -314,12 +314,12 @@ class FormField:
|
||||||
except ValueError:
|
except ValueError:
|
||||||
converted_data = d
|
converted_data = d
|
||||||
new_data.setlist(name, converted_data)
|
new_data.setlist(name, converted_data)
|
||||||
else:
|
# else:
|
||||||
try:
|
# try:
|
||||||
# individual fields deal with None values themselves
|
# # individual fields deal with None values themselves
|
||||||
new_data.setlist(name, [self.__class__.html2python(None)])
|
# new_data.setlist(name, [self.__class__.html2python(None)])
|
||||||
except EmptyValue:
|
# except EmptyValue:
|
||||||
new_data.setlist(name, [])
|
# new_data.setlist(name, [])
|
||||||
|
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
"Returns the HTML 'id' attribute for this form field."
|
"Returns the HTML 'id' attribute for this form field."
|
||||||
|
|
|
@ -222,7 +222,7 @@ class Field(object):
|
||||||
change = manipulator.change
|
change = manipulator.change
|
||||||
rel = manipulator.name_prefix != ''
|
rel = manipulator.name_prefix != ''
|
||||||
name_prefix = manipulator.name_prefix
|
name_prefix = manipulator.name_prefix
|
||||||
return (self.get_manipulator_fields(opts, manipulator, change,name_prefix, rel, follow), [] )
|
return (self.get_manipulator_fields(opts, manipulator, change,name_prefix, rel, follow), None )
|
||||||
|
|
||||||
def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
|
def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -52,7 +52,7 @@ class FillHelper(ManipulatorHelper):
|
||||||
child_manip._fill_data(obj_data)
|
child_manip._fill_data(obj_data)
|
||||||
|
|
||||||
def missing_item(self, index, child_manip):
|
def missing_item(self, index, child_manip):
|
||||||
self.child_manipulators[index] = None
|
child_manip.needs_deletion = True
|
||||||
|
|
||||||
def new_item(self, obj_data):
|
def new_item(self, obj_data):
|
||||||
child_manip = self.manip._add_manipulator_for_child(self.related)
|
child_manip = self.manip._add_manipulator_for_child(self.related)
|
||||||
|
@ -63,12 +63,11 @@ class SaveHelper(ManipulatorHelper):
|
||||||
child_manip._save_expanded(obj_data)
|
child_manip._save_expanded(obj_data)
|
||||||
|
|
||||||
def missing_item(self, index, child_manip):
|
def missing_item(self, index, child_manip):
|
||||||
child_manip.manip.original_object.delete(ignore_objects=[parent.original_object])
|
child_manip.original_object.delete(ignore_objects=[parent.original_object])
|
||||||
|
|
||||||
def new_item(self, obj_data):
|
def new_item(self, obj_data):
|
||||||
child_manip = self.manip._add_manipulator_for_child(self.related)
|
child_manip = self.manip._add_manipulator_for_child(self.related)
|
||||||
overrides = { self.related.field :
|
overrides = { self.related.field : self.manip.obj_key }
|
||||||
getattr(self.manip.original_object, self.manip.opts.pk.attname) }
|
|
||||||
child_manip._save_expanded(obj_data, overrides)
|
child_manip._save_expanded(obj_data, overrides)
|
||||||
|
|
||||||
class AutomaticManipulator(Manipulator):
|
class AutomaticManipulator(Manipulator):
|
||||||
|
@ -102,12 +101,17 @@ class AutomaticManipulator(Manipulator):
|
||||||
for f in self.opts.get_data_holders(self.follow):
|
for f in self.opts.get_data_holders(self.follow):
|
||||||
fol = self.follow[f.name]
|
fol = self.follow[f.name]
|
||||||
fields,manipulators = f.get_fields_and_manipulators(self.opts, self, follow=fol)
|
fields,manipulators = f.get_fields_and_manipulators(self.opts, self, follow=fol)
|
||||||
#fields = f.get_manipulator_fields(self.opts, self, self.change, follow=fol)
|
|
||||||
|
if fields != None:
|
||||||
self.fields_.extend(fields)
|
self.fields_.extend(fields)
|
||||||
if manipulators:
|
if manipulators != None:
|
||||||
self.children[f] = manipulators
|
self.children[f] = manipulators
|
||||||
|
self.needs_deletion = False
|
||||||
|
|
||||||
def get_fields(self):
|
def get_fields(self):
|
||||||
|
if self.needs_deletion:
|
||||||
|
return []
|
||||||
|
else:
|
||||||
l = list(self.fields_)
|
l = list(self.fields_)
|
||||||
for child_manips in self.children.values():
|
for child_manips in self.children.values():
|
||||||
for manip in child_manips:
|
for manip in child_manips:
|
||||||
|
@ -134,9 +138,7 @@ class AutomaticManipulator(Manipulator):
|
||||||
param = f.get_manipulator_new_data(expanded_data)
|
param = f.get_manipulator_new_data(expanded_data)
|
||||||
else:
|
else:
|
||||||
param = self.get_original_value(f)
|
param = self.get_original_value(f)
|
||||||
|
|
||||||
params[f.attname] = param
|
params[f.attname] = param
|
||||||
|
|
||||||
if self.change:
|
if self.change:
|
||||||
params[self.opts.pk.attname] = self.obj_key
|
params[self.opts.pk.attname] = self.obj_key
|
||||||
return self.model(**params)
|
return self.model(**params)
|
||||||
|
@ -144,11 +146,12 @@ class AutomaticManipulator(Manipulator):
|
||||||
def _fill_related_objects(self, expanded_data, helper_factory):
|
def _fill_related_objects(self, expanded_data, helper_factory):
|
||||||
for related, manips in self.children.items():
|
for related, manips in self.children.items():
|
||||||
helper = helper_factory(self, related, manips)
|
helper = helper_factory(self, related, manips)
|
||||||
child_data = MultiValueDict(expanded_data[related.var_name])
|
child_data = MultiValueDict(expanded_data.get(related.var_name, MultiValueDict()) )
|
||||||
# existing objects
|
# existing objects
|
||||||
for index,manip in enumerate(manips):
|
for index,manip in enumerate(manips):
|
||||||
obj_data = child_data.get(str(index), None)
|
obj_data = child_data.get(str(index), None)
|
||||||
child_data.pop(str(index), None)
|
child_data.pop(str(index), None)
|
||||||
|
|
||||||
if obj_data != None:
|
if obj_data != None:
|
||||||
#the object has new data
|
#the object has new data
|
||||||
helper.matched_item(index,manip, obj_data )
|
helper.matched_item(index,manip, obj_data )
|
||||||
|
@ -157,14 +160,38 @@ class AutomaticManipulator(Manipulator):
|
||||||
helper.missing_item(index,manip)
|
helper.missing_item(index,manip)
|
||||||
if child_data:
|
if child_data:
|
||||||
# There are new objects in the data
|
# There are new objects in the data
|
||||||
for index, obj_data in child_data.items():
|
items = sorted(child_data.items(),cmp = lambda x, y: cmp(x[0], y[0]))
|
||||||
|
for index, obj_data in items:
|
||||||
helper.new_item(obj_data)
|
helper.new_item(obj_data)
|
||||||
|
|
||||||
def _fill_data(self, expanded_data):
|
def _fill_data(self, expanded_data):
|
||||||
|
if self.needs_deletion:
|
||||||
|
raise BadCommand, "Filling %s with %r when it needs deletion" % (self, expanded_data)
|
||||||
self.original_object = self.get_new_object(expanded_data)
|
self.original_object = self.get_new_object(expanded_data)
|
||||||
# TODO: many_to_many
|
# TODO: many_to_many
|
||||||
self._fill_related_objects(expanded_data,FillHelper)
|
self._fill_related_objects(expanded_data,FillHelper)
|
||||||
|
|
||||||
|
def update(self, new_data):
|
||||||
|
expanded_data = dot_expand(new_data, MultiValueDict)
|
||||||
|
# Deal with the effects of previous commands
|
||||||
|
self._fill_data(expanded_data)
|
||||||
|
|
||||||
|
def save_from_update(self):
|
||||||
|
if self.needs_deletion:
|
||||||
|
if self.original_object != None:
|
||||||
|
self.original_object.delete()
|
||||||
|
return
|
||||||
|
|
||||||
|
self.original_object.save()
|
||||||
|
if not hasattr(self, 'obj_key'):
|
||||||
|
self.obj_key = getattr(self.original_object, self.opts.pk.attname)
|
||||||
|
|
||||||
|
for related, manips in self.children.items():
|
||||||
|
for i, manip in enumerate(manips):
|
||||||
|
setattr(manip.original_object, related.field.attname , self.obj_key)
|
||||||
|
manip.save_from_update()
|
||||||
|
return self.original_object
|
||||||
|
|
||||||
def do_command(self, new_data, command):
|
def do_command(self, new_data, command):
|
||||||
expanded_data = dot_expand(new_data, MultiValueDict)
|
expanded_data = dot_expand(new_data, MultiValueDict)
|
||||||
# Deal with the effects of previous commands
|
# Deal with the effects of previous commands
|
||||||
|
@ -190,9 +217,7 @@ class AutomaticManipulator(Manipulator):
|
||||||
if child_manips == None:
|
if child_manips == None:
|
||||||
raise BadCommand, "'%s' : unknown manipulator collection name." % (part,)
|
raise BadCommand, "'%s' : unknown manipulator collection name." % (part,)
|
||||||
|
|
||||||
child_data = expanded_data.get(part, None)
|
child_data = expanded_data.get(part, MultiValueDict())
|
||||||
if child_data == None:
|
|
||||||
raise BadCommand, "'%s' : could not find data for manipulator collection." % (part,)
|
|
||||||
|
|
||||||
# The next part could be an index of a manipulator,
|
# The next part could be an index of a manipulator,
|
||||||
# or it could be a command on the collection.
|
# or it could be a command on the collection.
|
||||||
|
@ -241,6 +266,10 @@ class AutomaticManipulator(Manipulator):
|
||||||
# First, save the basic object itself.
|
# First, save the basic object itself.
|
||||||
new_object.save()
|
new_object.save()
|
||||||
|
|
||||||
|
# Save the key for use in creating new related objects.
|
||||||
|
if not hasattr(self, 'obj_key'):
|
||||||
|
self.obj_key = getattr(new_object, self.opts.pk.attname)
|
||||||
|
|
||||||
# Now that the object's been saved, save any uploaded files.
|
# Now that the object's been saved, save any uploaded files.
|
||||||
for f in opts.fields:
|
for f in opts.fields:
|
||||||
if isinstance(f, FileField):
|
if isinstance(f, FileField):
|
||||||
|
@ -304,12 +333,16 @@ class ModelAddManipulator(AutomaticManipulator):
|
||||||
def get_original_value(self, field):
|
def get_original_value(self, field):
|
||||||
return field.get_default()
|
return field.get_default()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Automatic AddManipulator for %s>" % (self.model)
|
||||||
|
|
||||||
class ModelChangeManipulator(AutomaticManipulator):
|
class ModelChangeManipulator(AutomaticManipulator):
|
||||||
change = True
|
change = True
|
||||||
add = False
|
add = False
|
||||||
|
|
||||||
def __init__(self, obj_key=None, follow=None, name_prefix=''):
|
def __init__(self, obj_key=None, follow=None, name_prefix=''):
|
||||||
assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter."
|
assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter."
|
||||||
|
opts = self.model._meta
|
||||||
if isinstance(obj_key, self.model):
|
if isinstance(obj_key, self.model):
|
||||||
original_object = obj_key
|
original_object = obj_key
|
||||||
self.obj_key = getattr(original_object, self.model._meta.pk.attname)
|
self.obj_key = getattr(original_object, self.model._meta.pk.attname)
|
||||||
|
@ -346,6 +379,9 @@ class ModelChangeManipulator(AutomaticManipulator):
|
||||||
def get_original_value(self, field):
|
def get_original_value(self, field):
|
||||||
return getattr(self.original_object, field.attname)
|
return getattr(self.original_object, field.attname)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Automatic ChangeManipulator for %s:%r >" % (self.model, self.obj_key)
|
||||||
|
|
||||||
def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
|
def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
|
||||||
from django.utils.text import get_text_list
|
from django.utils.text import get_text_list
|
||||||
field_list = [opts.get_field(field_name) for field_name in field_name_list]
|
field_list = [opts.get_field(field_name) for field_name in field_name_list]
|
||||||
|
|
Loading…
Reference in New Issue