1from funcy import cached_property 2from django.core.exceptions import ImproperlyConfigured 3 4from .conf import settings 5 6 7def get_prefix(**kwargs): 8 return settings.CACHEOPS_PREFIX(PrefixQuery(**kwargs)) 9 10 11class PrefixQuery(object): 12 def __init__(self, **kwargs): 13 assert set(kwargs) <= {'func', '_queryset', '_cond_dnfs', 'dbs', 'tables'} 14 kwargs.setdefault('func', None) 15 self.__dict__.update(kwargs) 16 17 @cached_property 18 def dbs(self): 19 return [self._queryset.db] 20 21 @cached_property 22 def db(self): 23 if len(self.dbs) > 1: 24 dbs_str = ', '.join(self.dbs) 25 raise ImproperlyConfigured('Single db required, but several used: ' + dbs_str) 26 return self.dbs[0] 27 28 # TODO: think if I should expose it and how. Same for queryset. 29 @cached_property 30 def _cond_dnfs(self): 31 return self._queryset._cond_dnfs 32 33 @cached_property 34 def tables(self): 35 return list(self._cond_dnfs) 36 37 @cached_property 38 def table(self): 39 if len(self.tables) > 1: 40 tables_str = ', '.join(self.tables) 41 raise ImproperlyConfigured('Single table required, but several used: ' + tables_str) 42 return self.tables[0] 43