[4236] | 1 | import sys |
---|
[3526] | 2 | import grok |
---|
[3912] | 3 | from zope.component import getUtility |
---|
[4079] | 4 | from zope.component.factory import Factory |
---|
| 5 | from zope.component.interfaces import Invalid, IFactory |
---|
[4161] | 6 | from zope.component import createObject |
---|
[3878] | 7 | from zope.exceptions import DuplicationError |
---|
[4079] | 8 | from zope.interface import implementedBy |
---|
[4161] | 9 | from waeup.interfaces import IFacultyContainer, IFaculty, IWAeUPCSVImporter |
---|
[4236] | 10 | from waeup.viewlets import (MainArea, LeftSidebar, Index, Add, Manage, |
---|
| 11 | FormWrapMixin) |
---|
[4412] | 12 | from waeup.widgets.interfaces import ITableProvider |
---|
[3526] | 13 | |
---|
[3878] | 14 | class FacultyContainer(grok.Container): |
---|
[3912] | 15 | """See interfaces for description. |
---|
| 16 | """ |
---|
[3828] | 17 | grok.implements(IFacultyContainer) |
---|
[3884] | 18 | grok.require('waeup.manageUniversity') |
---|
[3919] | 19 | |
---|
[3878] | 20 | def addFaculty(self, faculty): |
---|
| 21 | if not IFaculty.providedBy(faculty): |
---|
| 22 | raise TypeError('FacultyContainers contain only IFaculty instances') |
---|
[4230] | 23 | self[faculty.code] = faculty |
---|
| 24 | return |
---|
[3878] | 25 | |
---|
[4161] | 26 | def clear(self): |
---|
| 27 | keys = self.keys() |
---|
| 28 | for key in keys: |
---|
| 29 | del self[key] |
---|
| 30 | |
---|
[4412] | 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 | |
---|
[4079] | 38 | class 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." |
---|
[3878] | 45 | |
---|
[4079] | 46 | def __call__(self): |
---|
| 47 | return FacultyContainer() |
---|
| 48 | |
---|
| 49 | def getInterfaces(self): |
---|
| 50 | return implementedBy(FacultyContainer) |
---|
| 51 | |
---|
[4222] | 52 | |
---|
| 53 | # |
---|
[3912] | 54 | # Viewing / Layout stuff... |
---|
| 55 | # |
---|
[3878] | 56 | class Content(grok.Viewlet): |
---|
| 57 | grok.viewletmanager(MainArea) |
---|
| 58 | grok.context(IFacultyContainer) |
---|
| 59 | grok.view(Index) |
---|
| 60 | |
---|
[4412] | 61 | def update(self): |
---|
[4435] | 62 | self.table = ITableProvider(self.context).getTable(self.view) |
---|
[4412] | 63 | self.table.need() |
---|
| 64 | |
---|
[3878] | 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(): |
---|
[4132] | 73 | result.append(dict(id=key, name=val.title)) |
---|
[3878] | 74 | return result |
---|
| 75 | |
---|
[4236] | 76 | class 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 | |
---|
[3878] | 105 | class AddFacultyForm(grok.AddForm): |
---|
| 106 | grok.context(IFacultyContainer) |
---|
| 107 | form_fields = grok.AutoFields(IFaculty) |
---|
[3884] | 108 | label = 'Add a faculty' |
---|
[3878] | 109 | |
---|
| 110 | @grok.action('Add faculty') |
---|
| 111 | def addFaculty(self, **data): |
---|
[4161] | 112 | faculty = createObject(u'waeup.Faculty') |
---|
[3878] | 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 | |
---|
[3910] | 122 | |
---|
| 123 | class AddFaculty(FormWrapMixin, grok.Viewlet): |
---|
[3878] | 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 | |
---|
[3910] | 131 | formview_name = 'addfacultyform' # The name of the formview we |
---|
| 132 | # want to be rendered in this |
---|
| 133 | # viewlet. |
---|
[3884] | 134 | |
---|
[3910] | 135 | |
---|
[3884] | 136 | class 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>' |
---|
[4161] | 149 | |
---|
[4236] | 150 | class 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>' |
---|