source: waeup/branches/ulif-rewrite/src/waeup/university/facultycontainer.py @ 3910

Last change on this file since 3910 was 3910, checked in by uli, 16 years ago

Let AddFaculty? viewlet use the new mixin.

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1import grok
2from zope.component import getUtility, getMultiAdapter
3from zope.component.interfaces import Invalid
4from zope.exceptions import DuplicationError
5from waeup.basecontainer import BaseContainer
6from waeup.interfaces import IFacultyContainer
7from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin
8from interfaces import IFaculty
9from faculty import Faculty
10
11from zope.schema import ValidationError
12
13class 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.
36grok.global_utility(FacultyContainer, provides=IFacultyContainer)
37
38class 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
54class 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       
72class 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
85class 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>'
Note: See TracBrowser for help on using the repository browser.