def __init__(self, request, page): self.title = () self.request = request self.page = page self.dict = {} self._breadcrumbs = Breadcrumbs() self.buttons = [] self._theme = 'bootstrap' self.activenav = () self.activetab = () self.loader = ResourceLoader() # Load typical things first self.use('jquery') self.use('diff.css') # @todo We can move this somewhere else now, right?
class GarethView(): def __init__(self, request, page): self.title = () self.request = request self.page = page self.dict = {} self._breadcrumbs = Breadcrumbs() self.buttons = [] self._theme = 'bootstrap' self.activenav = () self.activetab = () self.loader = ResourceLoader() # Load typical things first self.use('jquery') self.use('diff.css') # @todo We can move this somewhere else now, right? def set(self, *args, **kwargs): for k in kwargs: self.dict[k] = kwargs[k] def crumb(self, *crumbs): for crumb in crumbs: self._breadcrumbs.add(crumb) @property def theme(self): _theme = getattr(self, '_theme', None) if isinstance(_theme, basestring): _theme = theme.get(_theme) self._theme = _theme return _theme @theme.setter def theme(self, name): self._theme = theme.get(name) @property def title_text(self): title = [] for x in self.title: if isinstance(x, tuple): title.append(x[1]) else: title.append(x) if len(title) > 0: title.append('-') title.append('Gareth') # @fixme Sitename return ' '.join(title) @property def title_html(self): title = [] for x in self.title: if isinstance(x, tuple): title.append(escape(x[1])) else: title.append(escape(x)) return ' '.join(title) @property def breadcrumbs(self): return self._breadcrumbs.crumbs @property def navigation(self): navi = [] for nav in _navigation: navi.append(nav.copy()) navi.sort(key=lambda n: n['order']) return navi @property def personalbar(self): if self.request.currentuser: return [ { 'name': 'profile', 'active': ('user-%s' % self.request.currentuser.username), 'href': reverse('profile'), 'text': 'Profile' }, { 'name': 'settings', 'href': reverse('settings'), 'text': 'Settings' }, { 'name': 'logout', 'href': reverse('logout'), 'text': 'Logout' }, ] else: return [ { 'name': 'login', 'href': reverse('login'), 'text': 'Login' }, ] @property def tabs(self): return () def add_button(self, href=None, post=False, text=None, name=None): b = {} if not name: name = text.lower() b['href'] = href b['text'] = text b['name'] = name if post: b['method'] = 'POST' if isinstance(post, dict): b['data'] = post self.buttons.append(b) def use(self, resource): self.loader.require(resource) @property def template(self): return get_template(self.theme, ('view',)+self.page) # return get_template(':'.join(('view',)+self.page)) @property def context(self): def isactive(d, active): if type(active) == str: active = (active,) name = d.get('name', None) if d.get('active', None) == True: return True if name in active: return True a = d.get('active', None) if a: return len([x for x in active if x in a]) > 0 return False d = self.dict.copy() d['page_title'] = self.title_text d['title_html'] = self.title_html d['breadcrumbs'] = self.breadcrumbs d['navigation'] = self.navigation for nav in d['navigation']: nav['active'] = isactive(nav, self.activenav) d['personalbar'] = self.personalbar for bar in d['personalbar']: bar['active'] = isactive(bar, self.activenav) d['tabs'] = [] for origtab in self.tabs: tab = origtab.copy() tab['active'] = isactive(tab, self.activetab) d['tabs'].append(tab) d['buttons'] = self.buttons d['loader'] = self.loader return RequestContext(self.request, d) @property def response(self): self.theme.view_before_render(self) return HttpResponse(self.template.render(self.context)) def __call__(self): return self.response