"""Datatables and related components used in different contexts. """ import grok from waeup.interfaces import (IUniversity, IFacultyContainer, IFaculty, IDepartment, ICourseContainer) from waeup.widgets.interfaces import ITableProvider from waeup.widgets.table import YUITable as Table from waeup.widgets.table import Col from zope.security.management import checkPermission 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([data.getName(x) for x in data])), ) self.table = Table(self.title, cols=cols) return (self.table,) class DepartmentTableProvider(CodeAndTitleTableProvider): grok.context(IFaculty) grok.provides(ITableProvider) title = 'Departments' class FacultyTableProvider(CodeAndTitleTableProvider): grok.context(IFacultyContainer) grok.provides(ITableProvider) title = 'Faculties' class UniversityTableProvider(CodeAndTitleTableProvider): grok.context(IUniversity) grok.provides(ITableProvider) title = 'Faculties' def prepare(self): self.data_source = self.context.faculties class CourseTableProvider(CodeAndTitleTableProvider): grok.context(IDepartment) grok.provides(ITableProvider) title = 'Courses' def prepare(self): self.data_source = self.context.courses def getTables(self, view=None): data = self.data_source context_url = '' if view: context_url = view.url(self.context) + '/' # Create a courses table... 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([data[x].title for x in data])), ) delcol = Col(header='', sortable=False, data = [ '
' '' '' '
' % (data[x].code) for x in data]) if checkPermission('waeup.manageUniversity', self.context.courses): cols = list(cols) cols.append(delcol) cols = tuple(cols) self.coursetable = Table('Courses', cols=cols) # Create a certificates table... self.data_source = self.context.certificates data = self.data_source certcols =( Col( header='Code', sortable=True, data = tuple([self.getItemURL(view, data[x].code) for x in data])), Col( header='Title', sortable=True, data = tuple([data[x].title for x in data])), ) delcol = Col(header='', sortable=False, data = [ '
' '' '' '
' % (data[x].code) for x in data]) if checkPermission('waeup.manageUniversity', self.context.certificates): certcols = list(certcols) certcols.append(delcol) certcols = tuple(certcols) self.certtable = Table('Certificates', cols=certcols) return (self.coursetable, self.certtable) class CourseContainerCourseTableProvider(CodeAndTitleTableProvider): grok.context(ICourseContainer) grok.provides(ITableProvider) title = 'Courses' 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([data[x].title for x in data])), ) self.table = Table(self.title, cols=cols) return (self.table,)