mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
Fixes for inline editing with custom follow.
git-svn-id: http://code.djangoproject.com/svn/django/branches/new-admin@939 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
913d792878
commit
1819b38ae3
@ -821,7 +821,7 @@ def change_stage_new(request, app_label, module_name, object_id):
|
||||
|
||||
fill_extra_context(opts, app_label, c, change=True)
|
||||
|
||||
return render_to_response('admin_change_form', context_instance=c);
|
||||
return render_to_response('admin_change_form', context_instance=c)
|
||||
change_stage_new = staff_member_required(change_stage_new)
|
||||
|
||||
def _get_template(opts, app_label, add=False, change=False, show_delete=False, form_url=''):
|
||||
|
@ -116,7 +116,7 @@ class FormWrapper:
|
||||
data = field.extract_data(self.data)
|
||||
return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
|
||||
if self.edit_inline:
|
||||
self.fill_inline_collections()
|
||||
self.fill_inline_collections()
|
||||
for inline_collection in self._inline_collections:
|
||||
if inline_collection.name == key:
|
||||
return inline_collection
|
||||
@ -126,15 +126,13 @@ class FormWrapper:
|
||||
def fill_inline_collections(self):
|
||||
if not self._inline_collections:
|
||||
ic = []
|
||||
related_objects = self.manipulator.get_inline_related_objects_wrapped()
|
||||
related_objects = self.manipulator.get_related_objects()
|
||||
for rel_obj in related_objects:
|
||||
data = rel_obj.extract_data(self.data)
|
||||
inline_collection = InlineObjectCollection(self.manipulator, rel_obj, data, self.error_dict)
|
||||
ic.append(inline_collection)
|
||||
self._inline_collections = ic
|
||||
|
||||
|
||||
|
||||
def has_errors(self):
|
||||
return self.error_dict != {}
|
||||
|
||||
|
@ -156,20 +156,22 @@ class RelatedObject(object):
|
||||
self.name = opts.module_name
|
||||
self.var_name = opts.object_name.lower()
|
||||
|
||||
def flatten_data(self,obj = None):
|
||||
def flatten_data(self,follow, obj = None):
|
||||
new_data = {}
|
||||
rel_instances = self.get_list(obj)
|
||||
|
||||
for i, rel_instance in enumerate(rel_instances):
|
||||
instance_data = {}
|
||||
|
||||
for f in self.opts.fields + self.opts.many_to_many:
|
||||
field_data = f.flatten_data(rel_instance)
|
||||
#if hasattr(f, 'editable') and f.editable and f != self.field:
|
||||
for name, value in field_data.items():
|
||||
instance_data['%s.%d.%s' % (self.var_name, i, name)] = value
|
||||
new_data.update(instance_data)
|
||||
#TODO fix for recursive manipulators.
|
||||
fol = follow.get(f.name, None )
|
||||
if fol:
|
||||
field_data = f.flatten_data(fol,rel_instance)
|
||||
for name, value in field_data.items():
|
||||
instance_data['%s.%d.%s' % (self.var_name, i, name)] = value
|
||||
new_data.update(instance_data)
|
||||
|
||||
return new_data
|
||||
return new_data
|
||||
|
||||
def extract_data(self, data):
|
||||
"Pull out the data meant for inline objects of this class, ie anything starting with our module name"
|
||||
@ -426,6 +428,11 @@ class Options:
|
||||
def get_all_related_objects_wrapped(self):
|
||||
return [RelatedObject(self, opts, field) for opts, field in self.get_all_related_objects()]
|
||||
|
||||
def get_followed_related_objects(self, follow=None):
|
||||
if follow == None:
|
||||
follow = self.get_follow()
|
||||
return [f for f in self.get_all_related_objects_wrapped() if follow.get(f.name, None) ]
|
||||
|
||||
def get_data_holders(self, follow=None):
|
||||
if follow == None :
|
||||
follow = self.get_follow()
|
||||
@ -1529,7 +1536,7 @@ def get_manipulator(opts, klass, extra_methods, add=False, change=False):
|
||||
man.__module__ = MODEL_PREFIX + '.' + opts.module_name # Set this explicitly, as above.
|
||||
man.__init__ = curry(manipulator_init, opts, add, change)
|
||||
man.save = curry(manipulator_save, opts, klass, add, change)
|
||||
man.get_inline_related_objects_wrapped = curry(manipulator_get_inline_related_objects_wrapped, opts, klass, add, change)
|
||||
man.get_related_objects = curry(manipulator_get_related_objects, opts, klass, add, change)
|
||||
man.flatten_data = curry(manipulator_flatten_data, opts, klass, add, change)
|
||||
for field_name_list in opts.unique_together:
|
||||
setattr(man, 'isUnique%s' % '_'.join(field_name_list), curry(manipulator_validator_unique_together, field_name_list, opts))
|
||||
@ -1762,14 +1769,15 @@ def manipulator_save(opts, klass, add, change, self, new_data):
|
||||
getattr(new_object, 'set_%s_order' % rel_opts.object_name.lower())(order)
|
||||
return new_object
|
||||
|
||||
def manipulator_get_inline_related_objects_wrapped(opts, klass, add, change, self):
|
||||
return opts.get_inline_related_objects_wrapped()
|
||||
def manipulator_get_related_objects(opts, klass, add, change, self):
|
||||
return opts.get_followed_related_objects(self.follow)
|
||||
|
||||
def manipulator_flatten_data(opts, klass, add, change, self):
|
||||
new_data = {}
|
||||
obj = change and self.original_object or None
|
||||
for f in opts.get_data_holders(self.follow):
|
||||
new_data.update(f.flatten_data(obj))
|
||||
fol = self.follow.get(f.name)
|
||||
new_data.update(f.flatten_data(fol, obj))
|
||||
return new_data
|
||||
|
||||
def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
|
||||
|
@ -283,12 +283,13 @@ class Field(object):
|
||||
else:
|
||||
return self.get_default()
|
||||
|
||||
def flatten_data(self, obj = None):
|
||||
def flatten_data(self, follow, obj = None):
|
||||
"""
|
||||
Returns a dictionary mapping the field's manipulator field names to its
|
||||
"flattened" string values for the admin view. Obj is the instance to extract the
|
||||
values from.
|
||||
"""
|
||||
|
||||
return { self.get_db_column(): self._get_val_from_obj(obj)}
|
||||
|
||||
def get_follow(self, override=None):
|
||||
@ -361,7 +362,7 @@ class DateField(Field):
|
||||
def get_manipulator_field_objs(self):
|
||||
return [formfields.DateField]
|
||||
|
||||
def flatten_data(self, obj = None):
|
||||
def flatten_data(self, follow, obj = None):
|
||||
val = self._get_val_from_obj(obj)
|
||||
return {self.get_db_column(): (val is not None and val.strftime("%Y-%m-%d") or '')}
|
||||
|
||||
@ -394,7 +395,7 @@ class DateTimeField(DateField):
|
||||
return datetime.datetime.combine(d, t)
|
||||
return self.get_default()
|
||||
|
||||
def flatten_data(self,obj = None):
|
||||
def flatten_data(self,follow, obj = None):
|
||||
val = self._get_val_from_obj(obj)
|
||||
date_field, time_field = self.get_manipulator_field_names('')
|
||||
return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''),
|
||||
@ -591,7 +592,7 @@ class TimeField(Field):
|
||||
def get_manipulator_field_objs(self):
|
||||
return [formfields.TimeField]
|
||||
|
||||
def flatten_data(self,obj = None):
|
||||
def flatten_data(self,follow, obj = None):
|
||||
val = self._get_val_from_obj(obj)
|
||||
return {self.get_db_column(): (val is not None and val.strftime("%H:%M:%S") or '')}
|
||||
|
||||
@ -661,7 +662,7 @@ class ForeignKey(Field):
|
||||
else:
|
||||
return int(value)
|
||||
|
||||
def flatten_data(self, obj = None):
|
||||
def flatten_data(self, follow, obj = None):
|
||||
if not obj:
|
||||
# In required many-to-one fields with only one available choice,
|
||||
# select that one available choice. Note: We have to check that
|
||||
@ -671,7 +672,7 @@ class ForeignKey(Field):
|
||||
choice_list = self.get_choices_default()
|
||||
if len(choice_list) == 2:
|
||||
return { self.name : choice_list[1][0] }
|
||||
return Field.flatten_data(self, obj)
|
||||
return Field.flatten_data(self, follow, obj)
|
||||
|
||||
class ManyToManyField(Field):
|
||||
def __init__(self, to, **kwargs):
|
||||
@ -716,7 +717,7 @@ class ManyToManyField(Field):
|
||||
len(badkeys) == 1 and badkeys[0] or tuple(badkeys),
|
||||
len(badkeys) == 1 and "is" or "are")
|
||||
|
||||
def flatten_data(self, obj = None):
|
||||
def flatten_data(self, follow, obj = None):
|
||||
new_data = {}
|
||||
if obj:
|
||||
get_list_func = getattr(obj, 'get_%s_list' % self.rel.singular)
|
||||
|
Loading…
x
Reference in New Issue
Block a user