Django Library Themes
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-digits', 'Clear Digits'),
  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, 'https://validator.w3.org/nu/?doc=' + request.build_absolute_uri(), False, False))
  88. def set_additional_title(self, at):
  89. if self[self.SETTINGS].page_title and at:
  90. self[self.TITLE] = self[self.SETTINGS].page_title + ' - ' + at
  91. else:
  92. self[self.TITLE] = self[self.SETTINGS].page_title + at
  93. class entry(list):
  94. def __init__(self, uid, name, icon, url, left, active):
  95. list.__init__(self)
  96. self.uid = uid
  97. self.name = name
  98. self.icon = icon
  99. self.url = url
  100. self.left = left
  101. self.active = active
  102. @property
  103. def is_dropdownmenu(self):
  104. return len(self) > 0
  105. def __str__(self):
  106. return 'barentry: %s' % self.uid
  107. def empty_entry_parameters(request):
  108. return (
  109. 'empty', # uid
  110. '', # name
  111. transparency_icon_url(request), # icon
  112. None, # url
  113. True, # left
  114. False # active
  115. )
  116. def get_themename(request):
  117. return ThemeSettings().page_theme
  118. class time_measurement(object):
  119. def __init__(self):
  120. self._start_time = time.time()
  121. @property
  122. def time_str(self):
  123. return _('Time consumption') + ': %.2fs' % (time.time() - self._start_time)
  124. def transparency_icon_url(request):
  125. return static('themes/' + get_themename(request) + '/transparency.png')
  126. def color_icon_url(request, icon_filename):
  127. return static('themes/' + get_themename(request) + '/1st-color/%s' % icon_filename)
  128. def gray_icon_url(request, icon_filename):
  129. return static('themes/' + get_themename(request) + '/2nd-color/%s' % icon_filename)