Changeset 1413
- Timestamp:
- 11/24/05 19:46:07 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/new-admin/django/core/meta/__init__.py
r1361 r1413 150 150 151 151 class BoundRelatedObject(object): 152 def __init__(self, related_object, field_mapping, original):152 def __init__(self, related_object, field_mapping, original): 153 153 self.relation = related_object 154 154 self.field_mappings = field_mapping[related_object.opts.module_name] 155 155 156 156 def template_name(self): 157 raise NotImplementedE xception158 157 raise NotImplementedError 158 159 159 def __repr__(self): 160 160 return repr(self.__dict__) 161 161 162 162 class RelatedObject(object): 163 def __init__(self, parent_opts, opts, field):163 def __init__(self, parent_opts, opts, field): 164 164 self.parent_opts = parent_opts 165 165 self.opts = opts … … 169 169 self.var_name = opts.object_name.lower() 170 170 171 def flatten_data(self, follow, obj =None):171 def flatten_data(self, follow, obj=None): 172 172 new_data = {} 173 173 rel_instances = self.get_list(obj) 174 174 for i, rel_instance in enumerate(rel_instances): 175 instance_data = {} 176 175 instance_data = {} 177 176 for f in self.opts.fields + self.opts.many_to_many: 178 # TODO fix for recursive manipulators.179 fol = follow.get(f.name, None )177 # TODO: Fix for recursive manipulators. 178 fol = follow.get(f.name, None) 180 179 if fol: 181 field_data = f.flatten_data(fol, rel_instance)180 field_data = f.flatten_data(fol, rel_instance) 182 181 for name, value in field_data.items(): 183 182 instance_data['%s.%d.%s' % (self.var_name, i, name)] = value 184 183 new_data.update(instance_data) 185 186 184 return new_data 187 185 188 186 def extract_data(self, data): 189 "Pull out the data meant for inline objects of this class, ie anything starting with our module name" 190 return data # TODO 191 192 def get_list(self, parent_instance = None): 193 "Get the list of this type of object from an instance of the parent class" 187 """ 188 Pull out the data meant for inline objects of this class, 189 i.e. anything starting with our module name. 190 """ 191 return data # TODO 192 193 def get_list(self, parent_instance=None): 194 "Get the list of this type of object from an instance of the parent class." 194 195 if parent_instance != None: 195 196 func_name = 'get_%s_list' % self.get_method_name_part() 196 197 func = getattr(parent_instance, func_name) 197 198 list = func() 198 199 199 200 count = len(list) + self.field.rel.num_extra_on_change 200 201 if self.field.rel.min_num_in_admin: … … 202 203 if self.field.rel.max_num_in_admin: 203 204 count = min(count, self.field.rel.max_num_in_admin) 204 205 change = count - len(list) 205 206 change = count - len(list) 206 207 if change > 0: 207 208 return list + [None for _ in range(change)] … … 213 214 return [None for _ in range(self.field.rel.num_in_admin)] 214 215 215 216 216 217 def editable_fields(self): 217 """Get the fields in this class that should be edited inline.""" 218 219 return [f for f in self.opts.fields + self.opts.many_to_many if f.editable and f != self.field ] 220 218 "Get the fields in this class that should be edited inline." 219 return [f for f in self.opts.fields + self.opts.many_to_many if f.editable and f != self.field] 220 221 221 def get_follow(self, override=None): 222 222 if isinstance(override, bool): … … 232 232 else: 233 233 return None 234 234 235 235 over[self.field.name] = False 236 236 return self.opts.get_follow(over) 237 237 238 238 def __repr__(self): 239 return "<RelatedObject: %s related to %s>" % ( self.name, self.field.name) 239 return "<RelatedObject: %s related to %s>" % ( self.name, self.field.name) 240 240 241 241 def get_manipulator_fields(self, opts, manipulator, change, follow): 242 # TODO: remove core fields stuff242 # TODO: Remove core fields stuff. 243 243 if change: 244 244 meth_name = 'get_%s_count' % self.get_method_name_part() … … 251 251 else: 252 252 count = self.field.rel.num_in_admin 253 253 254 254 fields = [] 255 255 for i in range(count): 256 256 for f in self.opts.fields + self.opts.many_to_many: 257 if follow.get(f.name, False): 258 prefix = '%s.%d.' % (self.var_name, i) 259 fields.extend( 260 f.get_manipulator_fields(self.opts, manipulator, change, name_prefix=prefix, rel=True)) 261 257 if follow.get(f.name, False): 258 prefix = '%s.%d.' % (self.var_name, i) 259 fields.extend(f.get_manipulator_fields(self.opts, manipulator, change, name_prefix=prefix, rel=True)) 262 260 return fields 263 261 264 262 def bind(self, field_mapping, original, bound_related_object_class=BoundRelatedObject): 265 263 return bound_related_object_class(self, field_mapping, original) … … 436 434 if follow == None: 437 435 follow = self.get_follow() 438 return [f for f in self.get_all_related_objects() if follow.get(f.name, None) ]436 return [f for f in self.get_all_related_objects() if follow.get(f.name, None)] 439 437 440 438 def get_data_holders(self, follow=None): 441 if follow == None :439 if follow == None: 442 440 follow = self.get_follow() 443 return [f for f in self.fields + self.many_to_many + self.get_all_related_objects() if follow.get(f.name, None) ]441 return [f for f in self.fields + self.many_to_many + self.get_all_related_objects() if follow.get(f.name, None)] 444 442 445 443 def get_follow(self, override=None): … … 447 445 for f in self.fields + self.many_to_many + self.get_all_related_objects(): 448 446 if override and override.has_key(f.name): 449 child_override = override[f.name] 447 child_override = override[f.name] 450 448 else: 451 449 child_override = None … … 481 479 return self._ordered_objects 482 480 483 def has_field_type(self, field_type, follow =None):481 def has_field_type(self, field_type, follow=None): 484 482 """ 485 483 Returns True if this object's admin form has at least one of the given 486 484 field_type (e.g. FileField). 487 485 """ 488 # TODO: follow486 # TODO: follow 489 487 if not hasattr(self, '_field_types'): 490 488 self._field_types = {} … … 734 732 735 733 for f in opts.fields: 736 #TODO : change this into a virtual function so that user defined fields will be able to add methods to module or class. 734 #TODO : change this into a virtual function so that user defined fields will be able to add methods to module or class. 737 735 if f.choices: 738 736 # Add "get_thingie_display" method to get human-readable value. … … 858 856 # Replace all relationships to the old class with 859 857 # relationships to the new one. 860 for related in model._meta.get_all_related_objects() + \ 861 model._meta.get_all_related_many_to_many_objects(): 858 for related in model._meta.get_all_related_objects() + model._meta.get_all_related_many_to_many_objects(): 862 859 related.field.rel.to = opts 863 860 break 864 865 861 return new_class 866 862 … … 1627 1623 1628 1624 def manipulator_init(opts, add, change, self, obj_key=None, follow=None): 1629 self.follow = opts.get_follow(follow) 1630 1625 self.follow = opts.get_follow(follow) 1626 1631 1627 if change: 1632 1628 assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." … … 1654 1650 1655 1651 for f in opts.fields + opts.many_to_many: 1656 if self.follow.get(f.name, False): 1652 if self.follow.get(f.name, False): 1657 1653 self.fields.extend(f.get_manipulator_fields(opts, self, change)) 1658 1654 … … 1668 1664 1669 1665 def manipulator_save(opts, klass, add, change, self, new_data): 1670 # TODO: big cleanup when core fields go -> use recursive manipulators. 1666 # TODO: big cleanup when core fields go -> use recursive manipulators. 1671 1667 from django.utils.datastructures import DotExpandedDict 1672 1668 params = {} 1673 for f in opts.fields: 1669 for f in opts.fields: 1674 1670 # Fields with auto_now_add should keep their original value in the change stage. 1675 1671 auto_now_add = change and getattr(f, 'auto_now_add', False) … … 1681 1677 else: 1682 1678 param = f.get_default() 1683 params[f.attname] = param 1684 1679 params[f.attname] = param 1680 1685 1681 1686 1682 if change: … … 1719 1715 # ('2', {'id': [''], 'choice': ['']})] 1720 1716 child_follow = self.follow.get(related.name, None) 1721 1717 1722 1718 if child_follow: 1723 1719 obj_list = expanded_data[related.var_name].items() … … 1755 1751 # given, use a default value. FileFields are also a special 1756 1752 # case, because they'll be dealt with later. 1757 1753 1758 1754 if f == related.field: 1759 1755 param = getattr(new_object, related.field.rel.field_name) … … 1769 1765 if param != None: 1770 1766 params[f.attname] = param 1771 1767 1772 1768 1773 1769 # Related links are a special case, because we have to … … 1780 1776 # Create the related item. 1781 1777 new_rel_obj = related.opts.get_model_module().Klass(**params) 1782 1783 1778 1779 1784 1780 1785 1781 # If all the core fields were provided (non-empty), save the item. … … 1825 1821 def manipulator_get_related_objects(opts, klass, add, change, self): 1826 1822 return opts.get_followed_related_objects(self.follow) 1827 1823 1828 1824 def manipulator_flatten_data(opts, klass, add, change, self): 1829 1825 new_data = {} … … 1836 1832 def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data): 1837 1833 from django.utils.text import get_text_list 1838 1834 1839 1835 field_list = [opts.get_field(field_name) for field_name in field_name_list] 1840 1836 if isinstance(field_list[0].rel, ManyToOne): … … 1843 1839 kwargs = {'%s__iexact' % field_name_list[0]: field_data} 1844 1840 for f in field_list[1:]: 1845 # This is really not going to work for fields that have different form fields, eg DateTime 1846 # This validation needs to occur after html2python to be effective. 1841 # This is really not going to work for fields that have different form fields, eg DateTime 1842 # This validation needs to occur after html2python to be effective. 1847 1843 field_val = all_data.get(f.attname, None) 1848 1844 if field_val is None:
