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.8KB

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