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
RevLine 
[3526]1import grok
[3912]2from zope.component import getUtility
[4079]3from zope.component.factory import Factory
4from zope.component.interfaces import Invalid, IFactory
[3878]5from zope.exceptions import DuplicationError
[4079]6from zope.interface import implementedBy
[3912]7from waeup.interfaces import IFacultyContainer, IFaculty
[3910]8from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin
[3526]9
[3878]10class FacultyContainer(grok.Container):
[3912]11    """See interfaces for description.
12    """
[3828]13    grok.implements(IFacultyContainer)
[3884]14    grok.require('waeup.manageUniversity')
[3919]15
16    # A simple counter for ids.
17    next_id = 0L
[4079]18
[3878]19    def addFaculty(self, faculty):
20        if not IFaculty.providedBy(faculty):
21            raise TypeError('FacultyContainers contain only IFaculty instances')
[3922]22        id = str(self.next_id)
[3920]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
[3878]28
[4079]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."
[3878]36
[4079]37    def __call__(self):
38        return FacultyContainer()
39
40    def getInterfaces(self):
41        return implementedBy(FacultyContainer)
42
[3912]43#
44# Viewing / Layout stuff...
45#
[3878]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():
[4132]59            result.append(dict(id=key, name=val.title))
[3878]60        return result
61
62class AddFacultyForm(grok.AddForm):
63    grok.context(IFacultyContainer)
64    form_fields = grok.AutoFields(IFaculty)
[3884]65    label = 'Add a faculty'
[3878]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
[3910]79       
80class AddFaculty(FormWrapMixin, grok.Viewlet):
[3878]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
[3910]88    formview_name = 'addfacultyform' # The name of the formview we
89                                     # want to be rendered in this
90                                     # viewlet.
[3884]91
[3910]92
[3884]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.