Django Library Themes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from django.conf import settings
  2. from django.templatetags.static import static
  3. from django.forms.models import model_to_dict
  4. from django.utils.translation import gettext as _
  5. import time
  6. available_themes = [
  7. ('default', 'Default'),
  8. ('clear-red', 'Clear Red'),
  9. ('clear-green', 'Clear Green'),
  10. ('clear-blue', 'Clear Blue'),
  11. ]
  12. default_theme = 'default'
  13. class ThemeSettings(object):
  14. def __init__(self):
  15. from .models import GetSettings
  16. s = GetSettings()
  17. for attr_name in model_to_dict(s):
  18. attr = getattr(s, attr_name)
  19. if attr_name == 'page_theme' and attr == 'default':
  20. setattr(self, attr_name, settings.DEFAULT_THEME)
  21. else:
  22. setattr(self, attr_name, attr)
  23. class bar(list):
  24. def __init__(self, turn_around=False):
  25. self.time_measurement = time_measurement()
  26. list.__init__(self)
  27. self.turn_around = turn_around
  28. self.debug = False
  29. def index(self, entry_uid):
  30. for i in range(0, len(self)):
  31. if self[i].uid == entry_uid:
  32. return i
  33. raise ValueError('not in list')
  34. def append_entry(self, *args, **kwargs):
  35. self.append(entry(*args, **kwargs))
  36. def replace_entry(self, entry_uid, *args, **kwargs):
  37. i = self.index(entry_uid)
  38. self[i] = entry(*args, **kwargs)
  39. def append_entry_to_entry(self, entry_uid, *args, **kwargs):
  40. i = self.index(entry_uid)
  41. self[i].append(entry(*args, **kwargs))
  42. @property
  43. def entries(self):
  44. rv = self[:]
  45. if self.turn_around is True:
  46. rv.reverse()
  47. if self.debug is True:
  48. rv.append(entry('time_measurement', self.time_measurement.time_str, None, None, True, False))
  49. return rv
  50. class Context(dict):
  51. ACTIONBAR = 'actionbar'
  52. BOTTOMBAR = 'bottombar'
  53. MENUBAR = 'menubar'
  54. NAVIGATIONBAR = 'navigationbar'
  55. SETTINGS = 'settings'
  56. TITLE = 'title'
  57. def __init__(self, request):
  58. from .models import BottomBar
  59. #
  60. dict.__init__(
  61. self,
  62. bottombar=bar(),
  63. actionbar=bar(),
  64. navigationbar=bar(turn_around=True),
  65. menubar=bar(),
  66. settings=ThemeSettings(),
  67. title=ThemeSettings().page_title,
  68. )
  69. #
  70. for i in range(1, 6):
  71. for e in BottomBar.objects.filter(sequence_number=i):
  72. if e.icon:
  73. icon_url = e.icon.url
  74. else:
  75. icon_url = None
  76. self[self.BOTTOMBAR].append(entry(
  77. 'bootombar-%d' % e.id,
  78. e.name,
  79. icon_url,
  80. e.url,
  81. e.left,
  82. False
  83. ))
  84. if settings.DEBUG:
  85. self[self.BOTTOMBAR].debug = True
  86. self[self.BOTTOMBAR].append(entry('bottombar-w3-validator', _('W3-Validator'), None, 'https://validator.w3.org/nu/?doc=' + request.build_absolute_uri(), False, False))
  87. def set_additional_title(self, at):
  88. if self[self.SETTINGS].page_title and at:
  89. self[self.TITLE] = self[self.SETTINGS].page_title + ' - ' + at
  90. else:
  91. self[self.TITLE] = self[self.SETTINGS].page_title + at
  92. class entry(list):
  93. def __init__(self, uid, name, icon, url, left, active):
  94. list.__init__(self)
  95. self.uid = uid
  96. self.name = name
  97. self.icon = icon
  98. self.url = url
  99. self.left = left
  100. self.active = active
  101. @property
  102. def is_dropdownmenu(self):
  103. return len(self) > 0
  104. def __str__(self):
  105. return 'barentry: %s' % self.uid
  106. def empty_entry_parameters(request):
  107. return (
  108. 'empty', # uid
  109. '', # name
  110. transparency_icon_url(request), # icon
  111. None, # url
  112. True, # left
  113. False # active
  114. )
  115. def get_themename(request):
  116. return ThemeSettings().page_theme
  117. class time_measurement(object):
  118. def __init__(self):
  119. self._start_time = time.time()
  120. @property
  121. def time_str(self):
  122. return _('Time consumption') + ': %.2fs' % (time.time() - self._start_time)
  123. def transparency_icon_url(request):
  124. return static('themes/' + get_themename(request) + '/transparency.png')
  125. def color_icon_url(request, icon_filename):
  126. return static('themes/' + get_themename(request) + '/1st-color/%s' % icon_filename)
  127. def gray_icon_url(request, icon_filename):
  128. return static('themes/' + get_themename(request) + '/2nd-color/%s' % icon_filename)