mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09: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)
|
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)
|
change_stage_new = staff_member_required(change_stage_new)
|
||||||
|
|
||||||
def _get_template(opts, app_label, add=False, change=False, show_delete=False, form_url=''):
|
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)
|
data = field.extract_data(self.data)
|
||||||
return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
|
return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
|
||||||
if self.edit_inline:
|
if self.edit_inline:
|
||||||
self.fill_inline_collections()
|
self.fill_inline_collections()
|
||||||
for inline_collection in self._inline_collections:
|
for inline_collection in self._inline_collections:
|
||||||
if inline_collection.name == key:
|
if inline_collection.name == key:
|
||||||
return inline_collection
|
return inline_collection
|
||||||
@ -126,15 +126,13 @@ class FormWrapper:
|
|||||||
def fill_inline_collections(self):
|
def fill_inline_collections(self):
|
||||||
if not self._inline_collections:
|
if not self._inline_collections:
|
||||||
ic = []
|
ic = []
|
||||||
related_objects = self.manipulator.get_inline_related_objects_wrapped()
|
related_objects = self.manipulator.get_related_objects()
|
||||||
for rel_obj in related_objects:
|
for rel_obj in related_objects:
|
||||||
data = rel_obj.extract_data(self.data)
|
data = rel_obj.extract_data(self.data)
|
||||||
inline_collection = InlineObjectCollection(self.manipulator, rel_obj, data, self.error_dict)
|
inline_collection = InlineObjectCollection(self.manipulator, rel_obj, data, self.error_dict)
|
||||||
ic.append(inline_collection)
|
ic.append(inline_collection)
|
||||||
self._inline_collections = ic
|
self._inline_collections = ic
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def has_errors(self):
|
def has_errors(self):
|
||||||
return self.error_dict != {}
|
return self.error_dict != {}
|
||||||
|
|
||||||
|
@ -156,20 +156,22 @@ class RelatedObject(object):
|
|||||||
self.name = opts.module_name
|
self.name = opts.module_name
|
||||||
self.var_name = opts.object_name.lower()
|
self.var_name = opts.object_name.lower()
|
||||||
|
|
||||||
def flatten_data(self,obj = None):
|
def flatten_data(self,follow, obj = None):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
rel_instances = self.get_list(obj)
|
rel_instances = self.get_list(obj)
|
||||||
|
|
||||||
for i, rel_instance in enumerate(rel_instances):
|
for i, rel_instance in enumerate(rel_instances):
|
||||||
instance_data = {}
|
instance_data = {}
|
||||||
|
|
||||||
for f in self.opts.fields + self.opts.many_to_many:
|
for f in self.opts.fields + self.opts.many_to_many:
|
||||||
field_data = f.flatten_data(rel_instance)
|
#TODO fix for recursive manipulators.
|
||||||
#if hasattr(f, 'editable') and f.editable and f != self.field:
|
fol = follow.get(f.name, None )
|
||||||
for name, value in field_data.items():
|
if fol:
|
||||||
instance_data['%s.%d.%s' % (self.var_name, i, name)] = value
|
field_data = f.flatten_data(fol,rel_instance)
|
||||||
new_data.update(instance_data)
|
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):
|
def extract_data(self, data):
|
||||||
"Pull out the data meant for inline objects of this class, ie anything starting with our module name"
|
"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):
|
def get_all_related_objects_wrapped(self):
|
||||||
return [RelatedObject(self, opts, field) for opts, field in self.get_all_related_objects()]
|
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):
|
def get_data_holders(self, follow=None):
|
||||||
if follow == None :
|
if follow == None :
|
||||||
follow = self.get_follow()
|
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.__module__ = MODEL_PREFIX + '.' + opts.module_name # Set this explicitly, as above.
|
||||||
man.__init__ = curry(manipulator_init, opts, add, change)
|
man.__init__ = curry(manipulator_init, opts, add, change)
|
||||||
man.save = curry(manipulator_save, opts, klass, 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)
|
man.flatten_data = curry(manipulator_flatten_data, opts, klass, add, change)
|
||||||
for field_name_list in opts.unique_together:
|
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))
|
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)
|
getattr(new_object, 'set_%s_order' % rel_opts.object_name.lower())(order)
|
||||||
return new_object
|
return new_object
|
||||||
|
|
||||||
def manipulator_get_inline_related_objects_wrapped(opts, klass, add, change, self):
|
def manipulator_get_related_objects(opts, klass, add, change, self):
|
||||||
return opts.get_inline_related_objects_wrapped()
|
return opts.get_followed_related_objects(self.follow)
|
||||||
|
|
||||||
def manipulator_flatten_data(opts, klass, add, change, self):
|
def manipulator_flatten_data(opts, klass, add, change, self):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
obj = change and self.original_object or None
|
obj = change and self.original_object or None
|
||||||
for f in opts.get_data_holders(self.follow):
|
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
|
return new_data
|
||||||
|
|
||||||
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):
|
||||||
|
@ -283,12 +283,13 @@ class Field(object):
|
|||||||
else:
|
else:
|
||||||
return self.get_default()
|
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
|
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
|
"flattened" string values for the admin view. Obj is the instance to extract the
|
||||||
values from.
|
values from.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return { self.get_db_column(): self._get_val_from_obj(obj)}
|
return { self.get_db_column(): self._get_val_from_obj(obj)}
|
||||||
|
|
||||||
def get_follow(self, override=None):
|
def get_follow(self, override=None):
|
||||||
@ -361,7 +362,7 @@ class DateField(Field):
|
|||||||
def get_manipulator_field_objs(self):
|
def get_manipulator_field_objs(self):
|
||||||
return [formfields.DateField]
|
return [formfields.DateField]
|
||||||
|
|
||||||
def flatten_data(self, obj = None):
|
def flatten_data(self, follow, obj = None):
|
||||||
val = self._get_val_from_obj(obj)
|
val = self._get_val_from_obj(obj)
|
||||||
return {self.get_db_column(): (val is not None and val.strftime("%Y-%m-%d") or '')}
|
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 datetime.datetime.combine(d, t)
|
||||||
return self.get_default()
|
return self.get_default()
|
||||||
|
|
||||||
def flatten_data(self,obj = None):
|
def flatten_data(self,follow, obj = None):
|
||||||
val = self._get_val_from_obj(obj)
|
val = self._get_val_from_obj(obj)
|
||||||
date_field, time_field = self.get_manipulator_field_names('')
|
date_field, time_field = self.get_manipulator_field_names('')
|
||||||
return {date_field: (val is not None and val.strftime("%Y-%m-%d") or ''),
|
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):
|
def get_manipulator_field_objs(self):
|
||||||
return [formfields.TimeField]
|
return [formfields.TimeField]
|
||||||
|
|
||||||
def flatten_data(self,obj = None):
|
def flatten_data(self,follow, obj = None):
|
||||||
val = self._get_val_from_obj(obj)
|
val = self._get_val_from_obj(obj)
|
||||||
return {self.get_db_column(): (val is not None and val.strftime("%H:%M:%S") or '')}
|
return {self.get_db_column(): (val is not None and val.strftime("%H:%M:%S") or '')}
|
||||||
|
|
||||||
@ -661,7 +662,7 @@ class ForeignKey(Field):
|
|||||||
else:
|
else:
|
||||||
return int(value)
|
return int(value)
|
||||||
|
|
||||||
def flatten_data(self, obj = None):
|
def flatten_data(self, follow, obj = None):
|
||||||
if not obj:
|
if not obj:
|
||||||
# In required many-to-one fields with only one available choice,
|
# In required many-to-one fields with only one available choice,
|
||||||
# select that one available choice. Note: We have to check that
|
# select that one available choice. Note: We have to check that
|
||||||
@ -671,7 +672,7 @@ class ForeignKey(Field):
|
|||||||
choice_list = self.get_choices_default()
|
choice_list = self.get_choices_default()
|
||||||
if len(choice_list) == 2:
|
if len(choice_list) == 2:
|
||||||
return { self.name : choice_list[1][0] }
|
return { self.name : choice_list[1][0] }
|
||||||
return Field.flatten_data(self, obj)
|
return Field.flatten_data(self, follow, obj)
|
||||||
|
|
||||||
class ManyToManyField(Field):
|
class ManyToManyField(Field):
|
||||||
def __init__(self, to, **kwargs):
|
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 badkeys[0] or tuple(badkeys),
|
||||||
len(badkeys) == 1 and "is" or "are")
|
len(badkeys) == 1 and "is" or "are")
|
||||||
|
|
||||||
def flatten_data(self, obj = None):
|
def flatten_data(self, follow, obj = None):
|
||||||
new_data = {}
|
new_data = {}
|
||||||
if obj:
|
if obj:
|
||||||
get_list_func = getattr(obj, 'get_%s_list' % self.rel.singular)
|
get_list_func = getattr(obj, 'get_%s_list' % self.rel.singular)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user