Ticket #6214: models.base-11-01-2008.diff

File models.base-11-01-2008.diff, 4.6 KB (added by Ivan Illarionov, 17 years ago)

filter(lambda b...) changed to list comprehension

  • base.py

     
    2424    def __new__(cls, name, bases, attrs):
    2525        # If this isn't a subclass of Model, don't do anything special.
    2626        try:
    27             if not filter(lambda b: issubclass(b, Model), bases):
     27            parents = [b for b in bases if issubclass(b, Model)]
     28            if not parents:
    2829                return super(ModelBase, cls).__new__(cls, name, bases, attrs)
    2930        except NameError:
    3031            # 'Model' isn't defined yet, meaning we're looking at Django's own
     
    3940            types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned, ), {}))
    4041
    4142        # Build complete list of parents
    42         for base in bases:
    43             # TODO: Checking for the presence of '_meta' is hackish.
    44             if '_meta' in dir(base):
     43        for base in parents:
     44            if base is not Model:
    4545                new_class._meta.parents.append(base)
    4646                new_class._meta.parents.extend(base._meta.parents)
    4747
     
    7979        # registered version.
    8080        return get_model(new_class._meta.app_label, name, False)
    8181
     82    def add_to_class(cls, name, value):
     83        if name == 'Admin':
     84            assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value))
     85            value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')]))
     86        if hasattr(value, 'contribute_to_class'):
     87            value.contribute_to_class(cls, name)
     88        else:
     89            setattr(cls, name, value)
     90
     91    def _prepare(cls):
     92        # Creates some methods once self._meta has been populated.
     93        opts = cls._meta
     94        opts._prepare(cls)
     95
     96        if opts.order_with_respect_to:
     97            cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
     98            cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
     99            setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
     100            setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
     101
     102        # Give the class a docstring -- its definition.
     103        if cls.__doc__ is None:
     104            cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
     105
     106        if hasattr(cls, 'get_absolute_url'):
     107            cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
     108
     109        dispatcher.send(signal=signals.class_prepared, sender=cls)
     110
    82111class Model(object):
    83112    __metaclass__ = ModelBase
    84113
     
    176205                raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
    177206        dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self)
    178207
    179     def add_to_class(cls, name, value):
    180         if name == 'Admin':
    181             assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value))
    182             value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')]))
    183         if hasattr(value, 'contribute_to_class'):
    184             value.contribute_to_class(cls, name)
    185         else:
    186             setattr(cls, name, value)
    187     add_to_class = classmethod(add_to_class)
    188 
    189     def _prepare(cls):
    190         # Creates some methods once self._meta has been populated.
    191         opts = cls._meta
    192         opts._prepare(cls)
    193 
    194         if opts.order_with_respect_to:
    195             cls.get_next_in_order = curry(cls._get_next_or_previous_in_order, is_next=True)
    196             cls.get_previous_in_order = curry(cls._get_next_or_previous_in_order, is_next=False)
    197             setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
    198             setattr(opts.order_with_respect_to.rel.to, 'set_%s_order' % cls.__name__.lower(), curry(method_set_order, cls))
    199 
    200         # Give the class a docstring -- its definition.
    201         if cls.__doc__ is None:
    202             cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
    203 
    204         if hasattr(cls, 'get_absolute_url'):
    205             cls.get_absolute_url = curry(get_absolute_url, opts, cls.get_absolute_url)
    206 
    207         dispatcher.send(signal=signals.class_prepared, sender=cls)
    208 
    209     _prepare = classmethod(_prepare)
    210 
    211208    def save(self, raw=False):
    212209        dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self)
    213210
Back to Top