Django

Code

Show
Ignore:
Timestamp:
03/13/08 01:14:26 (10 months ago)
Author:
jkocherhans
Message:

newforms-admin: Merged from trunk up to [7232]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin

    • Property svnmerge-integrated changed from /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7120 to /django/trunk:1-4345,4350-4357,4359-4365,4371-4372,4374-4377,4380-4386,4388,4390-4391,4400-4402,4404-4408,4410,4412-4419,4426-4427,4430-4432,4434,4441,4443-4444,4446-4447,4450,4452-4453,4455-4458,4476,4503,4546,4564-4569,4580-4586,4617,4630,4641-6390,6392-7232
  • django/branches/newforms-admin/django/contrib/contenttypes/models.py

    r6342 r7233  
    33from django.utils.encoding import smart_unicode 
    44 
    5 CONTENT_TYPE_CACHE = {} 
    65class ContentTypeManager(models.Manager): 
     6 
     7    # Cache to avoid re-looking up ContentType objects all over the place. 
     8    # This cache is shared by all the get_for_* methods. 
     9    _cache = {} 
     10     
    711    def get_for_model(self, model): 
    812        """ 
    9         Returns the ContentType object for the given model, creating the 
    10         ContentType if necessary. 
     13        Returns the ContentType object for a given model, creating the 
     14        ContentType if necessary. Lookups are cached so that subsequent lookups 
     15        for the same model don't hit the database. 
    1116        """ 
    1217        opts = model._meta 
    1318        key = (opts.app_label, opts.object_name.lower()) 
    1419        try: 
    15             ct = CONTENT_TYPE_CACHE[key] 
     20            ct = self.__class__._cache[key] 
    1621        except KeyError: 
    17             # The smart_unicode() is needed around opts.verbose_name_raw because it might 
    18             # be a django.utils.functional.__proxy__ object. 
    19             ct, created = self.model._default_manager.get_or_create(app_label=key[0], 
    20                 model=key[1], defaults={'name': smart_unicode(opts.verbose_name_raw)}) 
    21             CONTENT_TYPE_CACHE[key] = ct 
     22            # Load or create the ContentType entry. The smart_unicode() is  
     23            # needed around opts.verbose_name_raw because name_raw might be a 
     24            # django.utils.functional.__proxy__ object. 
     25            ct, created = self.get_or_create( 
     26                app_label = opts.app_label, 
     27                model = opts.object_name.lower(),  
     28                defaults = {'name': smart_unicode(opts.verbose_name_raw)}, 
     29            ) 
     30            self._add_to_cache(ct) 
     31             
    2232        return ct 
    23  
     33         
     34    def get_for_id(self, id): 
     35        """ 
     36        Lookup a ContentType by ID. Uses the same shared cache as get_for_model 
     37        (though ContentTypes are obviously not created on-the-fly by get_by_id). 
     38        """ 
     39        try: 
     40            ct = self.__class__._cache[id] 
     41        except KeyError: 
     42            # This could raise a DoesNotExist; that's correct behavior and will 
     43            # make sure that only correct ctypes get stored in the cache dict. 
     44            ct = self.get(pk=id) 
     45            self._add_to_cache(ct) 
     46        return ct 
     47             
    2448    def clear_cache(self): 
    2549        """ 
     
    2953        this gets called). 
    3054        """ 
    31         global CONTENT_TYPE_CACHE 
    32         CONTENT_TYPE_CACHE = {} 
     55        self.__class__._cache.clear() 
     56         
     57    def _add_to_cache(self, ct): 
     58        """Insert a ContentType into the cache.""" 
     59        model = ct.model_class() 
     60        key = (model._meta.app_label, model._meta.object_name.lower()) 
     61        self.__class__._cache[key] = ct 
     62        self.__class__._cache[ct.id] = ct 
    3363 
    3464class ContentType(models.Model): 
     
    3767    model = models.CharField(_('python model class name'), max_length=100) 
    3868    objects = ContentTypeManager() 
     69     
    3970    class Meta: 
    4071        verbose_name = _('content type')