Changeset 4229 for waeup/branches/ulif-rewrite/src
- Timestamp:
- 8 Jun 2009, 17:07:18 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/university/faculty.py
r4156 r4229 1 1 import grok 2 2 from zope.component.interfaces import IFactory 3 from waeup.interfaces import IFaculty 4 from waeup.viewlets import MainArea, LeftSidebar, Index, FormWrapMixin, Manage 3 from zope.component import createObject 4 from waeup.interfaces import IFaculty, IDepartment 5 from waeup.viewlets import (MainArea, LeftSidebar, Index, FormWrapMixin, 6 Manage, Add) 5 7 6 8 class Faculty(grok.Container): 7 9 grok.implements(IFaculty) 8 10 11 # A simple counter for ids. 12 next_id = 0L 13 9 14 def __init__(self, 10 15 title=u'Unnamed Faculty', … … 16 21 self.code = code 17 22 23 def addDepartment(self, department): 24 if not IDepartment.providedBy(department): 25 raise TypeError('Facultues contain only IDepartment instances') 26 id = str(self.next_id) 27 self[id] = department 28 while str(self.next_id) in self.keys(): 29 # Look for next unused int... 30 self.next_id += 1 31 return id 32 33 def clear(self): 34 keys = self.keys() 35 for key in keys: 36 del self[key] 37 next_id = 0L 38 39 18 40 class FacultyFactory(grok.GlobalUtility): 19 41 """A factory for faculty containers. … … 30 52 return implementedBy(Faculty) 31 53 32 33 54 class DisplayForm(grok.DisplayForm): 34 55 grok.context(IFaculty) 35 56 form_fields = grok.AutoFields(IFaculty) 36 57 37 58 class Content(FormWrapMixin, grok.Viewlet): 38 59 """A viewlet that wraps the `AddFacultyForm`. … … 41 62 grok.context(IFaculty) 42 63 grok.view(Index) 43 grok.require('waeup.manageUniversity') 44 64 grok.order(2) 45 65 formview_name = 'displayform' # The name of the formview we want 46 66 # to be rendered in this viewlet. 67 68 class DepartmentList(grok.Viewlet): 69 grok.viewletmanager(MainArea) 70 grok.context(IFaculty) 71 grok.view(Index) 72 grok.order(3) 73 74 def getDepartments(self): 75 """Convinience method to create a sorted list of departments. 76 77 It provides a list of dicts with entries for all data needed by 78 usual list templates. 79 """ 80 result = [] 81 for key, val in self.context.items(): 82 result.append(dict(id=key, name=val.title)) 83 return result 47 84 48 85 … … 95 132 def render(self): 96 133 return u'<div class="portlet"><a href="manage">Settings</a></div>' 134 135 class AddDepartmentForm(grok.AddForm): 136 grok.context(IFaculty) 137 form_fields = grok.AutoFields(IDepartment) 138 label = 'Add a department' 139 140 @grok.action('Add department') 141 def addDepartment(self, **data): 142 department = createObject(u'waeup.Department') 143 self.applyData(department, **data) 144 try: 145 self.context.addDepartment(department) 146 except DuplicationError: 147 self.status = Invalid('The name chosen already exists ' 148 'in the database') 149 return 150 self.redirect(self.url(self.context)) 151 152 153 class AddDepartment(FormWrapMixin, grok.Viewlet): 154 """A viewlet that wraps the `AddDepartmentForm`. 155 """ 156 grok.viewletmanager(MainArea) 157 grok.context(IFaculty) 158 grok.view(Add) 159 grok.require('waeup.manageUniversity') 160 161 formview_name = 'adddepartmentform' # The name of the formview we 162 # want to be rendered in this 163 # viewlet. 164 165 class AddDepartmentLink(grok.Viewlet): 166 """A link in the left sidebar displaying 'Add department' 167 """ 168 grok.viewletmanager(LeftSidebar) 169 grok.context(IFaculty) 170 grok.view(Index) 171 grok.order(5) 172 # This is so cool! This link is only displayed, when the user is 173 # allowed to use it! 174 grok.require('waeup.manageUniversity') 175 176 def render(self): 177 return u'<div class="portlet"><a href="add">Add department</a></div>' 178 179
Note: See TracChangeset for help on using the changeset viewer.