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

Last change on this file since 4464 was 4464, checked in by uli, 15 years ago

Apply to new ITableProvider behviour.

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1import sys
2import grok
3from zope.component import getUtility
4from zope.component.factory import Factory
5from zope.component.interfaces import Invalid, IFactory
6from zope.component import createObject
7from zope.exceptions import DuplicationError
8from zope.interface import implementedBy
9from waeup.interfaces import IFacultyContainer, IFaculty, IWAeUPCSVImporter
10from waeup.viewlets import (MainArea, LeftSidebar, Index, Add, Manage,
11                            FormWrapMixin)
12from waeup.widgets.interfaces import ITableProvider
13
14class FacultyContainer(grok.Container):
15    """See interfaces for description.
16    """
17    grok.implements(IFacultyContainer)
18    grok.require('waeup.manageUniversity')
19
20    def addFaculty(self, faculty):
21        if not IFaculty.providedBy(faculty):
22            raise TypeError('FacultyContainers contain only IFaculty instances')
23        self[faculty.code] = faculty
24        return
25
26    def clear(self):
27        keys = self.keys()
28        for key in keys:
29            del self[key]
30
31    def getName(self, key):
32        if key in self.keys():
33            fac = self[key]
34            prefix = fac.title_prefix
35            prefix = prefix[0].upper() + prefix[1:]
36            return '%s of %s' % (prefix, fac.title)
37           
38class FacultyContainerFactory(grok.GlobalUtility):
39    """A factory for faculty containers.
40    """
41    grok.implements(IFactory)
42    grok.name(u'waeup.FacultyContainer')
43    title = u"Create a new faculty container.",
44    description = u"This factory instantiates new faculty containers."
45
46    def __call__(self):
47        return FacultyContainer()
48
49    def getInterfaces(self):
50        return implementedBy(FacultyContainer)
51
52
53#
54# Viewing / Layout stuff...
55#
56class Content(grok.Viewlet):
57    grok.viewletmanager(MainArea)
58    grok.context(IFacultyContainer)
59    grok.view(Index)
60
61    def update(self):
62        self.table = ITableProvider(self.context).getTables(self.view)[0]
63        self.table.need()
64   
65    def getFaculties(self):
66        """Convinience method to create a sorted list of faculties.
67
68        It provides a list of dicts with entries for all data needed by
69        usual list templates.
70        """
71        result = []
72        for key, val in self.context.items():
73            result.append(dict(id=key, name=val.title))
74        return result
75
76class ManageFacultyContainer(grok.Viewlet):
77    grok.viewletmanager(MainArea)
78    grok.context(IFacultyContainer)
79    grok.view(Manage)
80    grok.template('manage')   
81
82    def update(self):
83        form = self.request.form
84        if 'CANCEL' in form.keys():
85            self.view.redirect(self.view.url(self.context))
86        if not 'DELETE' in form.keys():
87            return
88        fac_id = form['fac_id']
89        if not isinstance(fac_id, list):
90            fac_id = [fac_id]
91        deleted = []
92        for id in fac_id:
93            try:
94                del self.context[id]
95                deleted.append(id)
96            except:
97                self.view.flash('Could not delete %s: %s: %s' % (
98                        id, sys.exc_info()[0], sys.exc_info()[1]))
99        if len(deleted):
100            self.view.flash('Successfully deleted: %s' % ', '.join(deleted))
101        # We have to redirect to let flash messages appear immediately...
102        self.view.redirect(self.view.url())
103        return
104       
105class AddFacultyForm(grok.AddForm):
106    grok.context(IFacultyContainer)
107    form_fields = grok.AutoFields(IFaculty)
108    label = 'Add a faculty'
109
110    @grok.action('Add faculty')
111    def addFaculty(self, **data):
112        faculty = createObject(u'waeup.Faculty')
113        self.applyData(faculty, **data)
114        try:
115            self.context.addFaculty(faculty)
116        except DuplicationError:
117            self.status = Invalid('The name chosen already exists '
118                                  'in the database')
119            return
120        self.redirect(self.url(self.context))
121
122       
123class AddFaculty(FormWrapMixin, grok.Viewlet):
124    """A viewlet that wraps the `AddFacultyForm`.
125    """
126    grok.viewletmanager(MainArea)
127    grok.context(IFacultyContainer)
128    grok.view(Add)
129    grok.require('waeup.manageUniversity')
130
131    formview_name = 'addfacultyform' # The name of the formview we
132                                     # want to be rendered in this
133                                     # viewlet.
134
135
136class AddFacultyLink(grok.Viewlet):
137    """A link in the left sidebar displaying 'Add faculty'
138    """
139    grok.viewletmanager(LeftSidebar)
140    grok.context(IFacultyContainer)
141    grok.view(Index)
142    grok.order(5)
143    # This is so cool! This link is only displayed, when the user is
144    # allowed to use it!
145    grok.require('waeup.manageUniversity')
146   
147    def render(self):
148        return u'<div class="portlet"><a href="add">Add faculty</a></div>'
149
150class ManageFacultyLink(grok.Viewlet):
151    """A link in the left sidebar displaying 'Manage faculty'
152    """
153    grok.viewletmanager(LeftSidebar)
154    grok.context(IFacultyContainer)
155    grok.view(Index)
156    grok.order(5)
157    grok.require('waeup.manageUniversity')
158   
159    def render(self):
160        return u'<div class="portlet"><a href="manage">Manage faculties</a></div>'
Note: See TracBrowser for help on using the repository browser.