[4431] | 1 | """Datatables and related components used in different contexts. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5005] | 4 | from waeup.sirp.interfaces import IUniversity |
---|
[4920] | 5 | from waeup.sirp.widgets.interfaces import ITableProvider |
---|
| 6 | from waeup.sirp.widgets.table import YUITable as Table |
---|
| 7 | from waeup.sirp.widgets.table import Col |
---|
[4431] | 8 | |
---|
[4432] | 9 | class CodeAndTitleTableProvider(grok.Adapter): |
---|
[4434] | 10 | """A table provider for objects that have a code, title and prefix. |
---|
| 11 | """ |
---|
[4432] | 12 | grok.baseclass() |
---|
[4431] | 13 | grok.provides(ITableProvider) |
---|
| 14 | |
---|
[4432] | 15 | title = None |
---|
[4431] | 16 | |
---|
[4434] | 17 | def __init__(self, context): |
---|
| 18 | self.context = context |
---|
| 19 | self.prepare() |
---|
| 20 | |
---|
| 21 | def prepare(self): |
---|
| 22 | self.data_source = self.context |
---|
| 23 | |
---|
| 24 | def getItemURL(self, view, value): |
---|
| 25 | context_url = '' |
---|
| 26 | if not view is None: |
---|
| 27 | context_url = view.url(self.data_source) + '/' |
---|
| 28 | result = '<a href="%s%s">%s</a>' % (context_url, value, value) |
---|
| 29 | return result |
---|
| 30 | |
---|
[4461] | 31 | def getTables(self, view=None): |
---|
[4434] | 32 | data = self.data_source |
---|
[4432] | 33 | context_url = '' |
---|
| 34 | if view: |
---|
| 35 | context_url = view.url(self.context) + '/' |
---|
[4431] | 36 | cols = ( |
---|
| 37 | Col( |
---|
| 38 | header='Code', sortable=True, |
---|
[4434] | 39 | data = tuple([self.getItemURL(view, data[x].code) |
---|
| 40 | for x in data])), |
---|
[4431] | 41 | Col( |
---|
| 42 | header='Title', sortable=True, |
---|
[5988] | 43 | data = tuple([data[x] for x in data])), |
---|
[4431] | 44 | ) |
---|
| 45 | self.table = Table(self.title, cols=cols) |
---|
[4461] | 46 | return (self.table,) |
---|
[4432] | 47 | |
---|
[4434] | 48 | class UniversityTableProvider(CodeAndTitleTableProvider): |
---|
| 49 | grok.context(IUniversity) |
---|
| 50 | grok.provides(ITableProvider) |
---|
| 51 | title = 'Faculties' |
---|
| 52 | |
---|
| 53 | def prepare(self): |
---|
| 54 | self.data_source = self.context.faculties |
---|
[4442] | 55 | |
---|