source: main/waeup.sirp/trunk/src/waeup/sirp/tables.py @ 4987

Last change on this file since 4987 was 4920, checked in by uli, 15 years ago

Make unit tests run again with the new package layout.

File size: 5.2 KB
Line 
1"""Datatables and related components used in different contexts.
2"""
3import grok
4from waeup.sirp.interfaces import (IUniversity, IFacultyContainer, IFaculty,
5                              IDepartment, ICourseContainer)
6from waeup.sirp.widgets.interfaces import ITableProvider
7from waeup.sirp.widgets.table import YUITable as Table
8from waeup.sirp.widgets.table import Col
9from zope.security.management import checkPermission
10
11class CodeAndTitleTableProvider(grok.Adapter):
12    """A table provider for objects that have a code, title and prefix.
13    """
14    grok.baseclass()
15    grok.provides(ITableProvider)
16
17    title = None
18
19    def __init__(self, context):
20        self.context = context
21        self.prepare()
22
23    def prepare(self):
24        self.data_source = self.context
25
26    def getItemURL(self, view, value):
27        context_url = ''
28        if not view is None:
29            context_url = view.url(self.data_source) + '/'
30        result = '<a href="%s%s">%s</a>' % (context_url, value, value)
31        return result
32   
33    def getTables(self, view=None):
34        data = self.data_source
35        context_url = ''
36        if view:
37            context_url = view.url(self.context) + '/'
38        cols = (
39            Col(
40                header='Code', sortable=True,
41                data = tuple([self.getItemURL(view, data[x].code)
42                              for x in data])),
43            Col(
44                header='Title', sortable=True,
45                data = tuple([data.getName(x) for x in data])),
46            )
47        self.table = Table(self.title, cols=cols)
48        return (self.table,)
49   
50class DepartmentTableProvider(CodeAndTitleTableProvider):
51    grok.context(IFaculty)
52    grok.provides(ITableProvider)
53    title = 'Departments'
54
55class FacultyTableProvider(CodeAndTitleTableProvider):
56    grok.context(IFacultyContainer)
57    grok.provides(ITableProvider)
58    title = 'Faculties'
59
60class UniversityTableProvider(CodeAndTitleTableProvider):
61    grok.context(IUniversity)
62    grok.provides(ITableProvider)
63    title = 'Faculties'
64
65    def prepare(self):
66        self.data_source = self.context.faculties
67
68class CourseTableProvider(CodeAndTitleTableProvider):
69    grok.context(IDepartment)
70    grok.provides(ITableProvider)
71    title = 'Courses'
72
73    def prepare(self):
74        self.data_source = self.context.courses
75       
76    def getTables(self, view=None):
77        data = self.data_source
78        context_url = ''
79        if view:
80            context_url = view.url(self.context) + '/'
81
82        # Create a courses table...
83        cols = (
84            Col(
85                header='Code', sortable=True,
86                data = tuple([self.getItemURL(view, data[x].code)
87                              for x in data])),
88            Col(
89                header='Title', sortable=True,
90                data = tuple([data[x].title for x in data])),
91            )
92        delcol = Col(header='', sortable=False,
93                     data = [
94                '<form method="post">'
95                '<input type="submit" name="delcourse" value="Delete" />'
96                '<input type="hidden" name="code" value="%s" />'
97                '</form>' % (data[x].code)
98                for x in data])
99
100        if checkPermission('waeup.manageUniversity', self.context.courses):
101            cols = list(cols)
102            cols.append(delcol)
103            cols = tuple(cols)
104        self.coursetable = Table('Courses', cols=cols)
105
106        # Create a certificates table...
107        self.data_source = self.context.certificates
108        data = self.data_source
109        certcols =(
110            Col(
111                header='Code', sortable=True,
112                data = tuple([self.getItemURL(view, data[x].code)
113                              for x in data])),
114            Col(
115                header='Title', sortable=True,
116                data = tuple([data[x].title for x in data])),
117            )
118
119        delcol = Col(header='', sortable=False,
120                     data = [
121                '<form method="post">'
122                '<input type="submit" name="delcert" value="Delete" />'
123                '<input type="hidden" name="code" value="%s" />'
124                '</form>' % (data[x].code)
125                for x in data])
126
127        if checkPermission('waeup.manageUniversity', self.context.certificates):
128            certcols = list(certcols)
129            certcols.append(delcol)
130            certcols = tuple(certcols)
131
132        self.certtable = Table('Certificates', cols=certcols)
133        return (self.coursetable, self.certtable)
134
135class CourseContainerCourseTableProvider(CodeAndTitleTableProvider):
136    grok.context(ICourseContainer)
137    grok.provides(ITableProvider)
138    title = 'Courses'
139
140    def getTables(self, view=None):
141        data = self.data_source
142        context_url = ''
143        if view:
144            context_url = view.url(self.context) + '/'
145        cols = (
146            Col(
147                header='Code', sortable=True,
148                data = tuple([self.getItemURL(view, data[x].code)
149                              for x in data])),
150            Col(
151                header='Title', sortable=True,
152                data = tuple([data[x].title for x in data])),
153            )
154        self.table = Table(self.title, cols=cols)
155        return (self.table,)
Note: See TracBrowser for help on using the repository browser.