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

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

Use new attribute to get faculty name.

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