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