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

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

Move imports for CSV imports to new module.

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