Django

Code

Changeset 1413

Show
Ignore:
Timestamp:
11/24/05 19:46:07 (3 years ago)
Author:
adrian
Message:

new-admin: Negligible formatting changes to django/core/meta/init.py

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/new-admin/django/core/meta/__init__.py

    r1361 r1413  
    150150 
    151151class BoundRelatedObject(object): 
    152     def __init__(self,related_object, field_mapping, original): 
     152    def __init__(self, related_object, field_mapping, original): 
    153153        self.relation = related_object 
    154154        self.field_mappings = field_mapping[related_object.opts.module_name] 
    155      
     155 
    156156    def template_name(self): 
    157         raise NotImplementedException 
    158      
     157        raise NotImplementedError 
     158 
    159159    def __repr__(self): 
    160160        return repr(self.__dict__) 
    161          
     161 
    162162class RelatedObject(object): 
    163     def __init__(self,parent_opts, opts, field): 
     163    def __init__(self, parent_opts, opts, field): 
    164164        self.parent_opts = parent_opts 
    165165        self.opts = opts 
     
    169169        self.var_name = opts.object_name.lower() 
    170170 
    171     def flatten_data(self,follow, obj = None): 
     171    def flatten_data(self, follow, obj=None): 
    172172        new_data = {} 
    173173        rel_instances = self.get_list(obj) 
    174174        for i, rel_instance in enumerate(rel_instances): 
    175             instance_data = {}  
    176              
     175            instance_data = {} 
    177176            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
    180179                if fol: 
    181                     field_data = f.flatten_data(fol,rel_instance) 
     180                    field_data = f.flatten_data(fol, rel_instance) 
    182181                    for name, value in field_data.items(): 
    183182                        instance_data['%s.%d.%s' % (self.var_name, i, name)] = value 
    184183            new_data.update(instance_data) 
    185      
    186184        return new_data 
    187185 
    188186    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." 
    194195        if parent_instance != None: 
    195196            func_name = 'get_%s_list' % self.get_method_name_part() 
    196197            func = getattr(parent_instance, func_name) 
    197198            list = func() 
    198              
     199 
    199200            count = len(list) + self.field.rel.num_extra_on_change 
    200201            if self.field.rel.min_num_in_admin: 
     
    202203            if self.field.rel.max_num_in_admin: 
    203204               count = min(count, self.field.rel.max_num_in_admin) 
    204         
    205             change = count - len(list)  
     205 
     206            change = count - len(list) 
    206207            if change > 0: 
    207208                return list + [None for _ in range(change)] 
     
    213214            return [None for _ in range(self.field.rel.num_in_admin)] 
    214215 
    215      
     216 
    216217    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 
    221221    def get_follow(self, override=None): 
    222222        if isinstance(override, bool): 
     
    232232            else: 
    233233                return None 
    234          
     234 
    235235        over[self.field.name] = False 
    236236        return self.opts.get_follow(over) 
    237      
     237 
    238238    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) 
    240240 
    241241    def get_manipulator_fields(self, opts, manipulator, change, follow): 
    242         #TODO: remove core fields stuff 
     242        # TODO: Remove core fields stuff. 
    243243        if change: 
    244244            meth_name = 'get_%s_count' % self.get_method_name_part() 
     
    251251        else: 
    252252            count = self.field.rel.num_in_admin 
    253              
     253 
    254254        fields = [] 
    255255        for i in range(count): 
    256256            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)) 
    262260        return fields 
    263      
     261 
    264262    def bind(self, field_mapping, original, bound_related_object_class=BoundRelatedObject): 
    265263        return bound_related_object_class(self, field_mapping, original) 
     
    436434        if follow == None: 
    437435            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)
    439437 
    440438    def get_data_holders(self, follow=None): 
    441         if follow == None
     439        if follow == None
    442440            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)
    444442 
    445443    def get_follow(self, override=None): 
     
    447445        for f in self.fields + self.many_to_many + self.get_all_related_objects(): 
    448446            if override and override.has_key(f.name): 
    449                 child_override = override[f.name]  
     447                child_override = override[f.name] 
    450448            else: 
    451449                child_override = None 
     
    481479        return self._ordered_objects 
    482480 
    483     def has_field_type(self, field_type, follow = None): 
     481    def has_field_type(self, field_type, follow=None): 
    484482        """ 
    485483        Returns True if this object's admin form has at least one of the given 
    486484        field_type (e.g. FileField). 
    487485        """ 
    488         #TODO: follow 
     486        # TODO: follow 
    489487        if not hasattr(self, '_field_types'): 
    490488            self._field_types = {} 
     
    734732 
    735733        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. 
    737735            if f.choices: 
    738736                # Add "get_thingie_display" method to get human-readable value. 
     
    858856                    # Replace all relationships to the old class with 
    859857                    # 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(): 
    862859                        related.field.rel.to = opts 
    863860                    break 
    864  
    865861        return new_class 
    866862 
     
    16271623 
    16281624def 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 
    16311627    if change: 
    16321628        assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." 
     
    16541650 
    16551651    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): 
    16571653            self.fields.extend(f.get_manipulator_fields(opts, self, change)) 
    16581654 
     
    16681664 
    16691665def 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. 
    16711667    from django.utils.datastructures import DotExpandedDict 
    16721668    params = {} 
    1673     for f in opts.fields:         
     1669    for f in opts.fields: 
    16741670        # Fields with auto_now_add should keep their original value in the change stage. 
    16751671        auto_now_add = change and getattr(f, 'auto_now_add', False) 
     
    16811677            else: 
    16821678                param = f.get_default() 
    1683         params[f.attname] = param     
    1684      
     1679        params[f.attname] = param 
     1680 
    16851681 
    16861682    if change: 
     
    17191715        #  ('2', {'id': [''], 'choice': ['']})] 
    17201716        child_follow = self.follow.get(related.name, None) 
    1721          
     1717 
    17221718        if child_follow: 
    17231719            obj_list = expanded_data[related.var_name].items() 
     
    17551751                    # given, use a default value. FileFields are also a special 
    17561752                    # case, because they'll be dealt with later. 
    1757                      
     1753 
    17581754                    if f == related.field: 
    17591755                        param = getattr(new_object, related.field.rel.field_name) 
     
    17691765                    if param != None: 
    17701766                       params[f.attname] = param 
    1771                      
     1767 
    17721768 
    17731769                    # Related links are a special case, because we have to 
     
    17801776                # Create the related item. 
    17811777                new_rel_obj = related.opts.get_model_module().Klass(**params) 
    1782      
    1783                  
     1778 
     1779 
    17841780 
    17851781                # If all the core fields were provided (non-empty), save the item. 
     
    18251821def manipulator_get_related_objects(opts, klass, add, change, self): 
    18261822    return opts.get_followed_related_objects(self.follow) 
    1827          
     1823 
    18281824def manipulator_flatten_data(opts, klass, add, change, self): 
    18291825     new_data = {} 
     
    18361832def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data): 
    18371833    from django.utils.text import get_text_list 
    1838      
     1834 
    18391835    field_list = [opts.get_field(field_name) for field_name in field_name_list] 
    18401836    if isinstance(field_list[0].rel, ManyToOne): 
     
    18431839        kwargs = {'%s__iexact' % field_name_list[0]: field_data} 
    18441840    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. 
    18471843        field_val = all_data.get(f.attname, None) 
    18481844        if field_val is None: