__init__.py 857 B

12345678910111213141516171819202122232425262728293031
  1. from apps.setting.models import GlobalConfig
  2. from functools import lru_cache
  3. class ProxySetting(object):
  4. @lru_cache()
  5. def __read_setting_by_key(self, key):
  6. line = GlobalConfig.query.filter_by(name=key).first()
  7. if line:
  8. return line.value
  9. raise AttributeError('No such config %r' % key)
  10. @staticmethod
  11. def has(key):
  12. if GlobalConfig.query.filter_by(name=key).first():
  13. return True
  14. return False
  15. def __getattr__(self, item):
  16. if item in self.__dict__:
  17. return self.__dict__[item]
  18. return self.__read_setting_by_key(item)
  19. def __setattr__(self, key, value):
  20. raise AttributeError('Does not support overwrite config')
  21. def __delattr__(self, item):
  22. raise AttributeError('Does not support delete config')
  23. Setting = ProxySetting()