1 | import grok |
---|
2 | from zope.component import getUtility |
---|
3 | from zope.component.factory import Factory |
---|
4 | from zope.component.interfaces import Invalid, IFactory |
---|
5 | from zope.exceptions import DuplicationError |
---|
6 | from zope.interface import implementedBy |
---|
7 | from waeup.interfaces import IFacultyContainer, IFaculty |
---|
8 | from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin |
---|
9 | |
---|
10 | class FacultyContainer(grok.Container): |
---|
11 | """See interfaces for description. |
---|
12 | """ |
---|
13 | grok.implements(IFacultyContainer) |
---|
14 | grok.require('waeup.manageUniversity') |
---|
15 | |
---|
16 | # A simple counter for ids. |
---|
17 | next_id = 0L |
---|
18 | |
---|
19 | def addFaculty(self, faculty): |
---|
20 | if not IFaculty.providedBy(faculty): |
---|
21 | raise TypeError('FacultyContainers contain only IFaculty instances') |
---|
22 | id = str(self.next_id) |
---|
23 | self[id] = faculty |
---|
24 | while self.next_id in self.keys(): |
---|
25 | # Look for next unused int... |
---|
26 | self.next_id += 1 |
---|
27 | return id |
---|
28 | |
---|
29 | class FacultyContainerFactory(grok.GlobalUtility): |
---|
30 | """A factory for faculty containers. |
---|
31 | """ |
---|
32 | grok.implements(IFactory) |
---|
33 | grok.name(u'waeup.FacultyContainer') |
---|
34 | title = u"Create a new faculty container.", |
---|
35 | description = u"This factory instantiates new faculty containers." |
---|
36 | |
---|
37 | def __call__(self): |
---|
38 | return FacultyContainer() |
---|
39 | |
---|
40 | def getInterfaces(self): |
---|
41 | return implementedBy(FacultyContainer) |
---|
42 | |
---|
43 | # |
---|
44 | # Viewing / Layout stuff... |
---|
45 | # |
---|
46 | class Content(grok.Viewlet): |
---|
47 | grok.viewletmanager(MainArea) |
---|
48 | grok.context(IFacultyContainer) |
---|
49 | grok.view(Index) |
---|
50 | |
---|
51 | def getFaculties(self): |
---|
52 | """Convinience method to create a sorted list of faculties. |
---|
53 | |
---|
54 | It provides a list of dicts with entries for all data needed by |
---|
55 | usual list templates. |
---|
56 | """ |
---|
57 | result = [] |
---|
58 | for key, val in self.context.items(): |
---|
59 | result.append(dict(id=key, name=val.title)) |
---|
60 | return result |
---|
61 | |
---|
62 | class AddFacultyForm(grok.AddForm): |
---|
63 | grok.context(IFacultyContainer) |
---|
64 | form_fields = grok.AutoFields(IFaculty) |
---|
65 | label = 'Add a faculty' |
---|
66 | |
---|
67 | @grok.action('Add faculty') |
---|
68 | def addFaculty(self, **data): |
---|
69 | faculty = getUtility(IFaculty) |
---|
70 | self.applyData(faculty, **data) |
---|
71 | try: |
---|
72 | self.context.addFaculty(faculty) |
---|
73 | except DuplicationError: |
---|
74 | self.status = Invalid('The name chosen already exists ' |
---|
75 | 'in the database') |
---|
76 | return |
---|
77 | self.redirect(self.url(self.context)) |
---|
78 | |
---|
79 | |
---|
80 | class AddFaculty(FormWrapMixin, grok.Viewlet): |
---|
81 | """A viewlet that wraps the `AddFacultyForm`. |
---|
82 | """ |
---|
83 | grok.viewletmanager(MainArea) |
---|
84 | grok.context(IFacultyContainer) |
---|
85 | grok.view(Add) |
---|
86 | grok.require('waeup.manageUniversity') |
---|
87 | |
---|
88 | formview_name = 'addfacultyform' # The name of the formview we |
---|
89 | # want to be rendered in this |
---|
90 | # viewlet. |
---|
91 | |
---|
92 | |
---|
93 | class AddFacultyLink(grok.Viewlet): |
---|
94 | """A link in the left sidebar displaying 'Add faculty' |
---|
95 | """ |
---|
96 | grok.viewletmanager(LeftSidebar) |
---|
97 | grok.context(IFacultyContainer) |
---|
98 | grok.view(Index) |
---|
99 | grok.order(5) |
---|
100 | # This is so cool! This link is only displayed, when the user is |
---|
101 | # allowed to use it! |
---|
102 | grok.require('waeup.manageUniversity') |
---|
103 | |
---|
104 | def render(self): |
---|
105 | return u'<div class="portlet"><a href="add">Add faculty</a></div>' |
---|