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

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

Use ints as keys.

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