Exemplo n.º 1
0
    def test_add_model_field(self):
        album = Album.objects.get(pk=1)
        result = {
            f.attname: getattr(album, f.attname) for f in album._meta.fields
        }
        result['another_field'] = '1'

        key = cache_keys.key_of_model(Album, pk=1)
        cache.set(key, result)
        key = cache_keys.key_of_model_list(Album, artist="Taylor Swift")
        cache.set(key, [result])

        album = cache.get_model(Album, pk=1)
        self.assertRaises(AttributeError, lambda: album.another_field)
Exemplo n.º 2
0
    def test_add_model_field(self):
        album = Album.objects.get(pk=1)
        result = {
            f.attname: getattr(album, f.attname)
            for f in album._meta.fields
        }
        result['another_field'] = '1'

        key = cache_keys.key_of_model(Album, pk=1)
        cache.set(key, result)
        key = cache_keys.key_of_model_list(Album, artist="Taylor Swift")
        cache.set(key, [result])

        album = cache.get_model(Album, pk=1)
        self.assertRaises(AttributeError, lambda: album.another_field)
Exemplo n.º 3
0
 def get_model(self, cls, cache_exc=False, **kwargs):
     key = cache_keys.key_of_model(cls, **kwargs)
     attrs = self.get(key)
     if not attrs:
         try:
             model = cls.objects.get(**kwargs)
             self.set(key, self.get_attrs(model))
         except cls.DoesNotExist:
             if not cache_exc:
                 raise cls.DoesNotExist
             model = None
             self.set(key, ModelNotExist())
     elif isinstance(attrs, ModelNotExist):
         if not cache_exc:
             raise cls.DoesNotExist
         model = None
     else:
         fields = set([field.attname for field in cls._meta.fields])
         attrs_keys = set(attrs.keys())
         diff = attrs_keys - fields
         for key in diff:
             attrs.pop(key)
         model = cls(**attrs)
     return model
Exemplo n.º 4
0
 def get_model(self, cls, cache_exc=False, **kwargs):
     key = cache_keys.key_of_model(cls, **kwargs)
     attrs = self.get(key)
     if not attrs:
         try:
             model = cls.objects.get(**kwargs)
             self.set(key, self.get_attrs(model))
         except cls.DoesNotExist:
             if not cache_exc:
                 raise cls.DoesNotExist
             model = None
             self.set(key, ModelNotExist())
     elif isinstance(attrs, ModelNotExist):
         if not cache_exc:
             raise cls.DoesNotExist
         model = None
     else:
         fields = set([field.attname for field in cls._meta.fields])
         attrs_keys = set(attrs.keys())
         diff = attrs_keys - fields
         for key in diff:
             attrs.pop(key)
         model = cls(**attrs)
     return model
Exemplo n.º 5
0
 def clear_models(self, instance, field, vals):
     keys = [cache_keys.key_of_model(
         instance, **{field: val}) for val in vals]
     self.delete_many(keys)
Exemplo n.º 6
0
 def _make_model_key(self, instance, *args):
     args = args or ['pk']
     kwargs = dict([(field, getattr(instance, field)) for field in args])
     return cache_keys.key_of_model(instance.__class__, **kwargs)
Exemplo n.º 7
0
 def clear_models(self, instance, field, vals):
     keys = [
         cache_keys.key_of_model(instance, **{field: val}) for val in vals
     ]
     self.delete_many(keys)
Exemplo n.º 8
0
 def _make_model_key(self, instance, *args):
     args = args or ['pk']
     kwargs = dict([(field, getattr(instance, field)) for field in args])
     return cache_keys.key_of_model(instance.__class__, **kwargs)