from django.conf import settings from django.templatetags.static import static from django.forms.models import model_to_dict from django.utils.translation import gettext as _ import time available_themes = [ ('default', 'Default'), ('clear-red', 'Clear Red'), ('clear-green', 'Clear Green'), ('clear-blue', 'Clear Blue'), ] default_theme = 'default' class ThemeSettings(object): def __init__(self): from .models import GetSettings s = GetSettings() for attr_name in model_to_dict(s): attr = getattr(s, attr_name) if attr_name == 'page_theme' and attr == 'default': setattr(self, attr_name, settings.DEFAULT_THEME) else: setattr(self, attr_name, attr) class bar(list): def __init__(self, turn_around=False): self.time_measurement = time_measurement() list.__init__(self) self.turn_around = turn_around self.debug = False def index(self, entry_uid): for i in range(0, len(self)): if self[i].uid == entry_uid: return i raise ValueError('not in list') def append_entry(self, *args, **kwargs): self.append(entry(*args, **kwargs)) def replace_entry(self, entry_uid, *args, **kwargs): i = self.index(entry_uid) self[i] = entry(*args, **kwargs) def append_entry_to_entry(self, entry_uid, *args, **kwargs): i = self.index(entry_uid) self[i].append(entry(*args, **kwargs)) @property def entries(self): rv = self[:] if self.turn_around is True: rv.reverse() if self.debug is True: rv.append(entry('time_measurement', self.time_measurement.time_str, None, None, True, False)) return rv class Context(dict): ACTIONBAR = 'actionbar' BOTTOMBAR = 'bottombar' MENUBAR = 'menubar' NAVIGATIONBAR = 'navigationbar' SETTINGS = 'settings' TITLE = 'title' def __init__(self, request): from .models import BottomBar # dict.__init__( self, bottombar=bar(), actionbar=bar(), navigationbar=bar(turn_around=True), menubar=bar(), settings=ThemeSettings(), title=ThemeSettings().page_title, ) # for i in range(1, 6): for e in BottomBar.objects.filter(sequence_number=i): if e.icon: icon_url = e.icon.url else: icon_url = None self[self.BOTTOMBAR].append(entry( 'bootombar-%d' % e.id, e.name, icon_url, e.url, e.left, False )) if settings.DEBUG: self[self.BOTTOMBAR].debug = True self[self.BOTTOMBAR].append(entry('bottombar-w3-validator', _('W3-Validator'), None, 'https://validator.w3.org/nu/?doc=' + request.build_absolute_uri(), False, False)) def set_additional_title(self, at): if self[self.SETTINGS].page_title and at: self[self.TITLE] = self[self.SETTINGS].page_title + ' - ' + at else: self[self.TITLE] = self[self.SETTINGS].page_title + at class entry(list): def __init__(self, uid, name, icon, url, left, active): list.__init__(self) self.uid = uid self.name = name self.icon = icon self.url = url self.left = left self.active = active @property def is_dropdownmenu(self): return len(self) > 0 def __str__(self): return 'barentry: %s' % self.uid def empty_entry_parameters(request): return ( 'empty', # uid '', # name transparency_icon_url(request), # icon None, # url True, # left False # active ) def get_themename(request): return ThemeSettings().page_theme class time_measurement(object): def __init__(self): self._start_time = time.time() @property def time_str(self): return _('Time consumption') + ': %.2fs' % (time.time() - self._start_time) def transparency_icon_url(request): return static('themes/' + get_themename(request) + '/transparency.png') def color_icon_url(request, icon_filename): return static('themes/' + get_themename(request) + '/1st-color/%s' % icon_filename) def gray_icon_url(request, icon_filename): return static('themes/' + get_themename(request) + '/2nd-color/%s' % icon_filename)