1 | import grok |
---|
2 | from zope.component import getUtility |
---|
3 | from zope.component.interfaces import Invalid |
---|
4 | from zope.exceptions import DuplicationError |
---|
5 | from waeup.interfaces import IFacultyContainer, IFaculty |
---|
6 | from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin |
---|
7 | from waeup.university.faculty import Faculty |
---|
8 | |
---|
9 | class FacultyContainer(grok.Container): |
---|
10 | """See interfaces for description. |
---|
11 | """ |
---|
12 | grok.implements(IFacultyContainer) |
---|
13 | grok.require('waeup.manageUniversity') |
---|
14 | |
---|
15 | def getId(self, faculty): |
---|
16 | """We need a unified id for each faculty, which acts as key. |
---|
17 | """ |
---|
18 | id = faculty.name |
---|
19 | id = id.replace(' ', '') |
---|
20 | return id.lower() |
---|
21 | |
---|
22 | def addFaculty(self, faculty): |
---|
23 | if not IFaculty.providedBy(faculty): |
---|
24 | raise TypeError('FacultyContainers contain only IFaculty instances') |
---|
25 | id = self.getId(faculty) |
---|
26 | self[id] = faculty |
---|
27 | |
---|
28 | # We register FacultyContainer as a utility. This way we can |
---|
29 | # call getUtility(IFacultyContainer) from anywhere in the code and |
---|
30 | # get an instance of FacultyContainer. |
---|
31 | grok.global_utility(FacultyContainer, provides=IFacultyContainer) |
---|
32 | |
---|
33 | # |
---|
34 | # Viewing / Layout stuff... |
---|
35 | # |
---|
36 | class Content(grok.Viewlet): |
---|
37 | grok.viewletmanager(MainArea) |
---|
38 | grok.context(IFacultyContainer) |
---|
39 | grok.view(Index) |
---|
40 | |
---|
41 | def getFaculties(self): |
---|
42 | """Convinience method to create a sorted list of faculties. |
---|
43 | |
---|
44 | It provides a list of dicts with entries for all data needed by |
---|
45 | usual list templates. |
---|
46 | """ |
---|
47 | result = [] |
---|
48 | for key, val in self.context.items(): |
---|
49 | result.append(dict(id=key, name=val.name)) |
---|
50 | return result |
---|
51 | |
---|
52 | class AddFacultyForm(grok.AddForm): |
---|
53 | grok.context(IFacultyContainer) |
---|
54 | form_fields = grok.AutoFields(IFaculty) |
---|
55 | label = 'Add a faculty' |
---|
56 | |
---|
57 | @grok.action('Add faculty') |
---|
58 | def addFaculty(self, **data): |
---|
59 | faculty = getUtility(IFaculty) |
---|
60 | self.applyData(faculty, **data) |
---|
61 | try: |
---|
62 | self.context.addFaculty(faculty) |
---|
63 | except DuplicationError: |
---|
64 | self.status = Invalid('The name chosen already exists ' |
---|
65 | 'in the database') |
---|
66 | return |
---|
67 | self.redirect(self.url(self.context)) |
---|
68 | |
---|
69 | |
---|
70 | class AddFaculty(FormWrapMixin, grok.Viewlet): |
---|
71 | """A viewlet that wraps the `AddFacultyForm`. |
---|
72 | """ |
---|
73 | grok.viewletmanager(MainArea) |
---|
74 | grok.context(IFacultyContainer) |
---|
75 | grok.view(Add) |
---|
76 | grok.require('waeup.manageUniversity') |
---|
77 | |
---|
78 | formview_name = 'addfacultyform' # The name of the formview we |
---|
79 | # want to be rendered in this |
---|
80 | # viewlet. |
---|
81 | |
---|
82 | |
---|
83 | class AddFacultyLink(grok.Viewlet): |
---|
84 | """A link in the left sidebar displaying 'Add faculty' |
---|
85 | """ |
---|
86 | grok.viewletmanager(LeftSidebar) |
---|
87 | grok.context(IFacultyContainer) |
---|
88 | grok.view(Index) |
---|
89 | grok.order(5) |
---|
90 | # This is so cool! This link is only displayed, when the user is |
---|
91 | # allowed to use it! |
---|
92 | grok.require('waeup.manageUniversity') |
---|
93 | |
---|
94 | def render(self): |
---|
95 | return u'<div class="portlet"><a href="add">Add faculty</a></div>' |
---|