1 | """Datatables and related components used in different contexts. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from waeup.sirp.interfaces import IUniversity |
---|
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 |
---|
8 | |
---|
9 | class CodeAndTitleTableProvider(grok.Adapter): |
---|
10 | """A table provider for objects that have a code, title and prefix. |
---|
11 | """ |
---|
12 | grok.baseclass() |
---|
13 | grok.provides(ITableProvider) |
---|
14 | |
---|
15 | title = None |
---|
16 | |
---|
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 | |
---|
31 | def getTables(self, view=None): |
---|
32 | data = self.data_source |
---|
33 | context_url = '' |
---|
34 | if view: |
---|
35 | context_url = view.url(self.context) + '/' |
---|
36 | cols = ( |
---|
37 | Col( |
---|
38 | header='Code', sortable=True, |
---|
39 | data = tuple([self.getItemURL(view, data[x].code) |
---|
40 | for x in data])), |
---|
41 | Col( |
---|
42 | header='Title', sortable=True, |
---|
43 | data = tuple([data[x] for x in data])), |
---|
44 | ) |
---|
45 | self.table = Table(self.title, cols=cols) |
---|
46 | return (self.table,) |
---|
47 | |
---|
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 |
---|
55 | |
---|