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

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

Be smart when looking for new ids.

  • Property svn:eol-style set to native
File size: 3.2 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 str(self.next_id)
22   
23    def addFaculty(self, faculty):
24        if not IFaculty.providedBy(faculty):
25            raise TypeError('FacultyContainers contain only IFaculty instances')
26        id = self.getId(faculty)
27        self[id] = faculty
28        while self.next_id in self.keys():
29            # Look for next unused int...
30            self.next_id += 1
31        return id
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
38#
39# Viewing / Layout stuff...
40#
41class Content(grok.Viewlet):
42    grok.viewletmanager(MainArea)
43    grok.context(IFacultyContainer)
44    grok.view(Index)
45
46    def getFaculties(self):
47        """Convinience method to create a sorted list of faculties.
48
49        It provides a list of dicts with entries for all data needed by
50        usual list templates.
51        """
52        result = []
53        for key, val in self.context.items():
54            result.append(dict(id=key, name=val.name))
55        return result
56
57class AddFacultyForm(grok.AddForm):
58    grok.context(IFacultyContainer)
59    form_fields = grok.AutoFields(IFaculty)
60    label = 'Add a faculty'
61
62    @grok.action('Add faculty')
63    def addFaculty(self, **data):
64        faculty = getUtility(IFaculty)
65        self.applyData(faculty, **data)
66        try:
67            self.context.addFaculty(faculty)
68        except DuplicationError:
69            self.status = Invalid('The name chosen already exists '
70                                  'in the database')
71            return
72        self.redirect(self.url(self.context))
73
74       
75class AddFaculty(FormWrapMixin, grok.Viewlet):
76    """A viewlet that wraps the `AddFacultyForm`.
77    """
78    grok.viewletmanager(MainArea)
79    grok.context(IFacultyContainer)
80    grok.view(Add)
81    grok.require('waeup.manageUniversity')
82
83    formview_name = 'addfacultyform' # The name of the formview we
84                                     # want to be rendered in this
85                                     # viewlet.
86
87
88class AddFacultyLink(grok.Viewlet):
89    """A link in the left sidebar displaying 'Add faculty'
90    """
91    grok.viewletmanager(LeftSidebar)
92    grok.context(IFacultyContainer)
93    grok.view(Index)
94    grok.order(5)
95    # This is so cool! This link is only displayed, when the user is
96    # allowed to use it!
97    grok.require('waeup.manageUniversity')
98   
99    def render(self):
100        return u'<div class="portlet"><a href="add">Add faculty</a></div>'
Note: See TracBrowser for help on using the repository browser.