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