Django Library Themes
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

__init__.py 4.8KB

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