Django Library Mycreole
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import config
  2. from django.conf import settings
  3. import importlib
  4. import os
  5. MYCREOLE_ATTACHMENT_ACCESS = "MYCREOLE_ATTACHMENT_ACCESS"
  6. ACCESS_READ = 'read'
  7. ACCESS_MODIFY = 'modify'
  8. MYCREOLE_BAR = "MYCREOLE_BAR"
  9. BAR_MENUBAR = "menubar"
  10. BAR_NAVIBAR = "navibar"
  11. MYCREOLE_ROOT = "MYCREOLE_ROOT"
  12. def no_access(*args, **kwargs):
  13. return False
  14. DEFAULTS = {
  15. MYCREOLE_ATTACHMENT_ACCESS: {
  16. ACCESS_READ: 'mycreole.parameter.no_access',
  17. ACCESS_MODIFY: 'mycreole.parameter.no_access',
  18. },
  19. MYCREOLE_BAR: {
  20. BAR_MENUBAR: None,
  21. BAR_NAVIBAR: None,
  22. },
  23. MYCREOLE_ROOT: os.path.join(settings.BASE_DIR, 'data', 'mycreole'),
  24. }
  25. def __get_object_by_name__(object_name):
  26. class_data = object_name.split(".")
  27. module_path = ".".join(class_data[:-1])
  28. class_str = class_data[-1]
  29. #
  30. module = importlib.import_module(module_path)
  31. return getattr(module, class_str)
  32. def get(key):
  33. # take data from config, settings or defaults
  34. try:
  35. data = getattr(config, key)
  36. except AttributeError:
  37. try:
  38. data = getattr(settings, key)
  39. except AttributeError:
  40. data = DEFAULTS.get(key)
  41. # adapt the data
  42. if key in [MYCREOLE_BAR, MYCREOLE_ATTACHMENT_ACCESS]:
  43. # Change given string to object
  44. rv = {}
  45. for skey in DEFAULTS[key]:
  46. # take the value from data or the default
  47. if skey in data:
  48. value = data[skey]
  49. else:
  50. value = DEFAULTS[key][skey]
  51. # take the object or None
  52. if value is not None:
  53. rv[skey] = __get_object_by_name__(value)
  54. else:
  55. rv[skey] = None
  56. return rv
  57. return data