Ignore:
Timestamp:
8 Jun 2009, 17:07:18 (15 years ago)
Author:
uli
Message:

Add UI stuff for faculties.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/university/faculty.py

    r4156 r4229  
    11import grok
    22from zope.component.interfaces import IFactory
    3 from waeup.interfaces import IFaculty
    4 from waeup.viewlets import MainArea, LeftSidebar, Index, FormWrapMixin, Manage
     3from zope.component import createObject
     4from waeup.interfaces import IFaculty, IDepartment
     5from waeup.viewlets import (MainArea, LeftSidebar, Index, FormWrapMixin,
     6                            Manage, Add)
    57
    68class Faculty(grok.Container):
    79    grok.implements(IFaculty)
    810
     11    # A simple counter for ids.
     12    next_id = 0L
     13   
    914    def __init__(self,
    1015                 title=u'Unnamed Faculty',
     
    1621        self.code = code
    1722
     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       
    1840class FacultyFactory(grok.GlobalUtility):
    1941    """A factory for faculty containers.
     
    3052        return implementedBy(Faculty)
    3153
    32        
    3354class DisplayForm(grok.DisplayForm):
    3455    grok.context(IFaculty)
    3556    form_fields = grok.AutoFields(IFaculty)
    36 
     57 
    3758class Content(FormWrapMixin, grok.Viewlet):
    3859    """A viewlet that wraps the `AddFacultyForm`.
     
    4162    grok.context(IFaculty)
    4263    grok.view(Index)
    43     grok.require('waeup.manageUniversity')
    44 
     64    grok.order(2)
    4565    formview_name = 'displayform' # The name of the formview we want
    4666                                  # to be rendered in this viewlet.
     67   
     68class 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
    4784
    4885
     
    95132    def render(self):
    96133        return u'<div class="portlet"><a href="manage">Settings</a></div>'
     134
     135class 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       
     153class 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
     165class 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.