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 | from waeup.sirp.utils.helpers import getName |
---|
9 | |
---|
10 | class CodeAndTitleTableProvider(grok.Adapter): |
---|
11 | """A table provider for objects that have a code, title and prefix. |
---|
12 | """ |
---|
13 | grok.baseclass() |
---|
14 | grok.provides(ITableProvider) |
---|
15 | |
---|
16 | title = None |
---|
17 | |
---|
18 | def __init__(self, context): |
---|
19 | self.context = context |
---|
20 | self.prepare() |
---|
21 | |
---|
22 | def prepare(self): |
---|
23 | self.data_source = self.context |
---|
24 | |
---|
25 | def getItemURL(self, view, value): |
---|
26 | context_url = '' |
---|
27 | if not view is None: |
---|
28 | context_url = view.url(self.data_source) + '/' |
---|
29 | result = '<a href="%s%s">%s</a>' % (context_url, value, value) |
---|
30 | return result |
---|
31 | |
---|
32 | def getTables(self, view=None): |
---|
33 | data = self.data_source |
---|
34 | context_url = '' |
---|
35 | if view: |
---|
36 | context_url = view.url(self.context) + '/' |
---|
37 | cols = ( |
---|
38 | Col( |
---|
39 | header='Code', sortable=True, |
---|
40 | data = tuple([self.getItemURL(view, data[x].code) |
---|
41 | for x in data])), |
---|
42 | Col( |
---|
43 | header='Title', sortable=True, |
---|
44 | data = tuple([getName(data[x]) for x in data])), |
---|
45 | ) |
---|
46 | self.table = Table(self.title, cols=cols) |
---|
47 | return (self.table,) |
---|
48 | |
---|
49 | class UniversityTableProvider(CodeAndTitleTableProvider): |
---|
50 | grok.context(IUniversity) |
---|
51 | grok.provides(ITableProvider) |
---|
52 | title = 'Faculties' |
---|
53 | |
---|
54 | def prepare(self): |
---|
55 | self.data_source = self.context.faculties |
---|
56 | |
---|