Skip to content

Commit

Permalink
Avoid problems with cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelm committed Jul 9, 2017
1 parent 4a99c95 commit f68057b
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions hierarkey/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.functional import cached_property
from typing import Any, Callable, Optional


Expand Down Expand Up @@ -106,17 +105,20 @@ def init(self, *args, object=None, **kwargs):

hierarkey = self

def prop(self):
def prop(iself):
from .proxy import HierarkeyProxy

return HierarkeyProxy._new(self,
type=kv_model,
hierarkey=hierarkey,
cache_namespace=_cache_namespace)
attrname = '_hierarkey_proxy_{}_{}'.format(_cache_namespace, self.attribute_name)
cached = getattr(iself, attrname, None)
if not cached:
cached = HierarkeyProxy._new(iself, type=kv_model, hierarkey=hierarkey,
cache_namespace=_cache_namespace)
setattr(iself, attrname, cached)
return cached

setattr(sys.modules[wrapped_class.__module__], model_name, kv_model)
setattr(wrapped_class, '_%s_objects' % self.attribute_name, kv_model.objects)
setattr(wrapped_class, self.attribute_name, cached_property(prop))
setattr(wrapped_class, self.attribute_name, property(prop))
self.global_class = wrapped_class
return wrapped_class

Expand Down Expand Up @@ -145,33 +147,38 @@ def wrapper(model):
attrs['object'] = models.ForeignKey(model, related_name='_%s_objects' % self.attribute_name,
on_delete=models.CASCADE)
model_name = '%s_%sStore' % (model.__name__, self.attribute_name.title())
if getattr(sys.modules[model.__module__], model_name, None):
# Already wrapped
return model
kv_model = self._create_model(model_name, attrs)

setattr(sys.modules[model.__module__], model_name, kv_model)

hierarkey = self

def prop(self):
def prop(iself):
from .proxy import HierarkeyProxy

try:
parent = getattr(self, parent_field) if parent_field else None
except models.ObjectDoesNotExist: # pragma: no cover
parent = None

if not parent and hierarkey.global_class:
parent = hierarkey.global_class()

return HierarkeyProxy._new(self,
type=kv_model,
hierarkey=hierarkey,
parent=parent,
cache_namespace=_cache_namespace)

setattr(model, self.attribute_name, cached_property(prop))
attrname = '_hierarkey_proxy_{}_{}'.format(_cache_namespace, self.attribute_name)
cached = getattr(iself, attrname, None)
if not cached:
try:
parent = getattr(iself, parent_field) if parent_field else None
except models.ObjectDoesNotExist: # pragma: no cover
parent = None

if not parent and hierarkey.global_class:
parent = hierarkey.global_class()

cached = HierarkeyProxy._new(
iself,
type=kv_model,
hierarkey=hierarkey,
parent=parent,
cache_namespace=_cache_namespace
)
setattr(iself, attrname, cached)
return cached


setattr(model, self.attribute_name, property(prop))

return model

Expand Down

0 comments on commit f68057b

Please sign in to comment.