Index: /waeup/branches/ulif-rewrite/src/waeup/university/faculty.py
===================================================================
--- /waeup/branches/ulif-rewrite/src/waeup/university/faculty.py	(revision 4228)
+++ /waeup/branches/ulif-rewrite/src/waeup/university/faculty.py	(revision 4229)
@@ -1,10 +1,15 @@
 import grok
 from zope.component.interfaces import IFactory
-from waeup.interfaces import IFaculty
-from waeup.viewlets import MainArea, LeftSidebar, Index, FormWrapMixin, Manage
+from zope.component import createObject
+from waeup.interfaces import IFaculty, IDepartment
+from waeup.viewlets import (MainArea, LeftSidebar, Index, FormWrapMixin,
+                            Manage, Add)
 
 class Faculty(grok.Container):
     grok.implements(IFaculty)
 
+    # A simple counter for ids.
+    next_id = 0L
+    
     def __init__(self,
                  title=u'Unnamed Faculty',
@@ -16,4 +21,21 @@
         self.code = code
 
+    def addDepartment(self, department):
+        if not IDepartment.providedBy(department):
+            raise TypeError('Facultues contain only IDepartment instances')
+        id = str(self.next_id)
+        self[id] = department
+        while str(self.next_id) in self.keys():
+            # Look for next unused int...
+            self.next_id += 1
+        return id
+
+    def clear(self):
+        keys = self.keys()
+        for key in keys:
+            del self[key]
+        next_id = 0L
+
+        
 class FacultyFactory(grok.GlobalUtility):
     """A factory for faculty containers.
@@ -30,9 +52,8 @@
         return implementedBy(Faculty)
 
-        
 class DisplayForm(grok.DisplayForm):
     grok.context(IFaculty)
     form_fields = grok.AutoFields(IFaculty)
-
+ 
 class Content(FormWrapMixin, grok.Viewlet):
     """A viewlet that wraps the `AddFacultyForm`.
@@ -41,8 +62,24 @@
     grok.context(IFaculty)
     grok.view(Index)
-    grok.require('waeup.manageUniversity')
-
+    grok.order(2)
     formview_name = 'displayform' # The name of the formview we want
                                   # to be rendered in this viewlet.
+    
+class DepartmentList(grok.Viewlet):
+    grok.viewletmanager(MainArea)
+    grok.context(IFaculty)
+    grok.view(Index)
+    grok.order(3)
+
+    def getDepartments(self):
+        """Convinience method to create a sorted list of departments.
+
+        It provides a list of dicts with entries for all data needed by
+        usual list templates.
+        """
+        result = []
+        for key, val in self.context.items():
+            result.append(dict(id=key, name=val.title))
+        return result
 
 
@@ -95,2 +132,48 @@
     def render(self):
         return u'<div class="portlet"><a href="manage">Settings</a></div>'
+
+class AddDepartmentForm(grok.AddForm):
+    grok.context(IFaculty)
+    form_fields = grok.AutoFields(IDepartment)
+    label = 'Add a department'
+
+    @grok.action('Add department')
+    def addDepartment(self, **data):
+        department = createObject(u'waeup.Department')
+        self.applyData(department, **data)
+        try:
+            self.context.addDepartment(department)
+        except DuplicationError:
+            self.status = Invalid('The name chosen already exists '
+                                  'in the database')
+            return
+        self.redirect(self.url(self.context))
+
+        
+class AddDepartment(FormWrapMixin, grok.Viewlet):
+    """A viewlet that wraps the `AddDepartmentForm`.
+    """
+    grok.viewletmanager(MainArea)
+    grok.context(IFaculty)
+    grok.view(Add)
+    grok.require('waeup.manageUniversity')
+
+    formview_name = 'adddepartmentform' # The name of the formview we
+                                     # want to be rendered in this
+                                     # viewlet.
+
+class AddDepartmentLink(grok.Viewlet):
+    """A link in the left sidebar displaying 'Add department'
+    """
+    grok.viewletmanager(LeftSidebar)
+    grok.context(IFaculty)
+    grok.view(Index)
+    grok.order(5)
+    # This is so cool! This link is only displayed, when the user is
+    # allowed to use it!
+    grok.require('waeup.manageUniversity')
+    
+    def render(self):
+        return u'<div class="portlet"><a href="add">Add department</a></div>'
+
+
