"""Datatables and related components used in different contexts. """ import grok from waeup.sirp.interfaces import IUniversity from waeup.sirp.widgets.interfaces import ITableProvider from waeup.sirp.widgets.table import YUITable as Table from waeup.sirp.widgets.table import Col from waeup.sirp.utils.helpers import getName class CodeAndTitleTableProvider(grok.Adapter): """A table provider for objects that have a code, title and prefix. """ grok.baseclass() grok.provides(ITableProvider) title = None def __init__(self, context): self.context = context self.prepare() def prepare(self): self.data_source = self.context def getItemURL(self, view, value): context_url = '' if not view is None: context_url = view.url(self.data_source) + '/' result = '%s' % (context_url, value, value) return result def getTables(self, view=None): data = self.data_source context_url = '' if view: context_url = view.url(self.context) + '/' cols = ( Col( header='Code', sortable=True, data = tuple([self.getItemURL(view, data[x].code) for x in data])), Col( header='Title', sortable=True, data = tuple([getName(data[x]) for x in data])), ) self.table = Table(self.title, cols=cols) return (self.table,) class UniversityTableProvider(CodeAndTitleTableProvider): grok.context(IUniversity) grok.provides(ITableProvider) title = 'Faculties' def prepare(self): self.data_source = self.context.faculties