[3526] | 1 | import grok |
---|
[3912] | 2 | from zope.component import getUtility |
---|
[4079] | 3 | from zope.component.factory import Factory |
---|
| 4 | from zope.component.interfaces import Invalid, IFactory |
---|
[3878] | 5 | from zope.exceptions import DuplicationError |
---|
[4079] | 6 | from zope.interface import implementedBy |
---|
[3912] | 7 | from waeup.interfaces import IFacultyContainer, IFaculty |
---|
[3910] | 8 | from waeup.viewlets import MainArea, LeftSidebar, Index, Add, FormWrapMixin |
---|
[3526] | 9 | |
---|
[3878] | 10 | class 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] | 29 | class 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] | 46 | class 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 | |
---|
| 62 | class 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 | |
---|
| 80 | class 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] | 93 | class 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>' |
---|