1 | import sys |
---|
2 | import grok |
---|
3 | from zope.component import getUtility |
---|
4 | from zope.component.factory import Factory |
---|
5 | from zope.component.interfaces import Invalid, IFactory |
---|
6 | from zope.component import createObject |
---|
7 | from zope.exceptions import DuplicationError |
---|
8 | from zope.interface import implementedBy |
---|
9 | from waeup.interfaces import IFacultyContainer, IFaculty, IWAeUPCSVImporter |
---|
10 | from waeup.viewlets import (MainArea, LeftSidebar, Index, Add, Manage, |
---|
11 | FormWrapMixin) |
---|
12 | from waeup.widgets.interfaces import ITableProvider |
---|
13 | |
---|
14 | class FacultyContainer(grok.Container): |
---|
15 | """See interfaces for description. |
---|
16 | """ |
---|
17 | grok.implements(IFacultyContainer) |
---|
18 | grok.require('waeup.manageUniversity') |
---|
19 | |
---|
20 | def addFaculty(self, faculty): |
---|
21 | if not IFaculty.providedBy(faculty): |
---|
22 | raise TypeError('FacultyContainers contain only IFaculty instances') |
---|
23 | self[faculty.code] = faculty |
---|
24 | return |
---|
25 | |
---|
26 | def clear(self): |
---|
27 | keys = self.keys() |
---|
28 | for key in keys: |
---|
29 | del self[key] |
---|
30 | |
---|
31 | def getName(self, key): |
---|
32 | if key in self.keys(): |
---|
33 | fac = self[key] |
---|
34 | prefix = fac.title_prefix |
---|
35 | prefix = prefix[0].upper() + prefix[1:] |
---|
36 | return '%s of %s' % (prefix, fac.title) |
---|
37 | |
---|
38 | class FacultyContainerFactory(grok.GlobalUtility): |
---|
39 | """A factory for faculty containers. |
---|
40 | """ |
---|
41 | grok.implements(IFactory) |
---|
42 | grok.name(u'waeup.FacultyContainer') |
---|
43 | title = u"Create a new faculty container.", |
---|
44 | description = u"This factory instantiates new faculty containers." |
---|
45 | |
---|
46 | def __call__(self): |
---|
47 | return FacultyContainer() |
---|
48 | |
---|
49 | def getInterfaces(self): |
---|
50 | return implementedBy(FacultyContainer) |
---|
51 | |
---|
52 | |
---|
53 | # |
---|
54 | # Viewing / Layout stuff... |
---|
55 | # |
---|
56 | class Content(grok.Viewlet): |
---|
57 | grok.viewletmanager(MainArea) |
---|
58 | grok.context(IFacultyContainer) |
---|
59 | grok.view(Index) |
---|
60 | |
---|
61 | def update(self): |
---|
62 | self.table = ITableProvider(self.context).getTable(self.view) |
---|
63 | self.table.need() |
---|
64 | |
---|
65 | def getFaculties(self): |
---|
66 | """Convinience method to create a sorted list of faculties. |
---|
67 | |
---|
68 | It provides a list of dicts with entries for all data needed by |
---|
69 | usual list templates. |
---|
70 | """ |
---|
71 | result = [] |
---|
72 | for key, val in self.context.items(): |
---|
73 | result.append(dict(id=key, name=val.title)) |
---|
74 | return result |
---|
75 | |
---|
76 | class ManageFacultyContainer(grok.Viewlet): |
---|
77 | grok.viewletmanager(MainArea) |
---|
78 | grok.context(IFacultyContainer) |
---|
79 | grok.view(Manage) |
---|
80 | grok.template('manage') |
---|
81 | |
---|
82 | def update(self): |
---|
83 | form = self.request.form |
---|
84 | if 'CANCEL' in form.keys(): |
---|
85 | self.view.redirect(self.view.url(self.context)) |
---|
86 | if not 'DELETE' in form.keys(): |
---|
87 | return |
---|
88 | fac_id = form['fac_id'] |
---|
89 | if not isinstance(fac_id, list): |
---|
90 | fac_id = [fac_id] |
---|
91 | deleted = [] |
---|
92 | for id in fac_id: |
---|
93 | try: |
---|
94 | del self.context[id] |
---|
95 | deleted.append(id) |
---|
96 | except: |
---|
97 | self.view.flash('Could not delete %s: %s: %s' % ( |
---|
98 | id, sys.exc_info()[0], sys.exc_info()[1])) |
---|
99 | if len(deleted): |
---|
100 | self.view.flash('Successfully deleted: %s' % ', '.join(deleted)) |
---|
101 | # We have to redirect to let flash messages appear immediately... |
---|
102 | self.view.redirect(self.view.url()) |
---|
103 | return |
---|
104 | |
---|
105 | class AddFacultyForm(grok.AddForm): |
---|
106 | grok.context(IFacultyContainer) |
---|
107 | form_fields = grok.AutoFields(IFaculty) |
---|
108 | label = 'Add a faculty' |
---|
109 | |
---|
110 | @grok.action('Add faculty') |
---|
111 | def addFaculty(self, **data): |
---|
112 | faculty = createObject(u'waeup.Faculty') |
---|
113 | self.applyData(faculty, **data) |
---|
114 | try: |
---|
115 | self.context.addFaculty(faculty) |
---|
116 | except DuplicationError: |
---|
117 | self.status = Invalid('The name chosen already exists ' |
---|
118 | 'in the database') |
---|
119 | return |
---|
120 | self.redirect(self.url(self.context)) |
---|
121 | |
---|
122 | |
---|
123 | class AddFaculty(FormWrapMixin, grok.Viewlet): |
---|
124 | """A viewlet that wraps the `AddFacultyForm`. |
---|
125 | """ |
---|
126 | grok.viewletmanager(MainArea) |
---|
127 | grok.context(IFacultyContainer) |
---|
128 | grok.view(Add) |
---|
129 | grok.require('waeup.manageUniversity') |
---|
130 | |
---|
131 | formview_name = 'addfacultyform' # The name of the formview we |
---|
132 | # want to be rendered in this |
---|
133 | # viewlet. |
---|
134 | |
---|
135 | |
---|
136 | class AddFacultyLink(grok.Viewlet): |
---|
137 | """A link in the left sidebar displaying 'Add faculty' |
---|
138 | """ |
---|
139 | grok.viewletmanager(LeftSidebar) |
---|
140 | grok.context(IFacultyContainer) |
---|
141 | grok.view(Index) |
---|
142 | grok.order(5) |
---|
143 | # This is so cool! This link is only displayed, when the user is |
---|
144 | # allowed to use it! |
---|
145 | grok.require('waeup.manageUniversity') |
---|
146 | |
---|
147 | def render(self): |
---|
148 | return u'<div class="portlet"><a href="add">Add faculty</a></div>' |
---|
149 | |
---|
150 | class ManageFacultyLink(grok.Viewlet): |
---|
151 | """A link in the left sidebar displaying 'Manage faculty' |
---|
152 | """ |
---|
153 | grok.viewletmanager(LeftSidebar) |
---|
154 | grok.context(IFacultyContainer) |
---|
155 | grok.view(Index) |
---|
156 | grok.order(5) |
---|
157 | grok.require('waeup.manageUniversity') |
---|
158 | |
---|
159 | def render(self): |
---|
160 | return u'<div class="portlet"><a href="manage">Manage faculties</a></div>' |
---|