Ignore:
Timestamp:
8 Jan 2010, 01:11:48 (15 years ago)
Author:
uli
Message:
  • Remove view components. They moved to browser subpackage.
  • Clean up code.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-layout/src/waeup/university/department.py

    r4511 r4721  
    11"""University departments.
    22"""
    3 import copy
    43import grok
    54from zope.component import createObject
    6 from zope.component.interfaces import IFactory, Invalid
    7 from zope.exceptions import DuplicationError
     5from zope.component.interfaces import IFactory
    86from zope.interface import implementedBy
    9 from waeup.interfaces import IDepartment, ICourse, ICertificate
    10 from waeup.utils.helpers import getName
    11 from waeup.widgets.interfaces import ITableProvider
    12 from waeup.viewlets import (MainArea, LeftSidebar, Index, FormWrapMixin,
    13                             Manage, AddCourse, AddCertificate)
     7from waeup.interfaces import IDepartment
    148
    159class Department(grok.Container):
     10    """A university department.
     11    """
    1612    grok.implements(IDepartment)
    1713
     
    3531
    3632    def traverse(self, name):
     33        """Deliver appropriate containers, if someone wants to go to courses
     34        or departments.
     35        """
    3736        if name == 'courses':
    3837            return self.courses
     
    5453
    5554    def getInterfaces(self):
     55        """Get interfaces of objects provided by this factory.
     56        """
    5657        return implementedBy(Department)
    57 
    58 
    59 class Content(grok.Viewlet):
    60     """A viewlet that wraps the `AddDepartmentForm`.
    61     """
    62     grok.viewletmanager(MainArea)
    63     grok.context(IDepartment)
    64     grok.view(Index)
    65     grok.require('waeup.manageUniversity')
    66 
    67     def getName(self):
    68         return getName(self.context)
    69 
    70     def getParentName(self):
    71         return getName(self.context.__parent__)
    72 
    73 class CourseList(grok.Viewlet):
    74     grok.viewletmanager(MainArea)
    75     grok.context(IDepartment)
    76     grok.view(Index)
    77     grok.order(3)
    78 
    79     def update(self):
    80         if 'delcourse' in self.request.form:
    81             code = self.request.form['code']
    82             del self.context.courses[code]
    83         if 'delcert' in self.request.form:
    84             code = self.request.form['code']
    85             del self.context.certificates[code]
    86         self.tables = ITableProvider(self.context).getTables(view=self.view)
    87         self.coursetable = self.tables[0]
    88         self.certificatetable = self.tables[1]
    89         self.coursetable.need()
    90 
    91 
    92 class ManageForm(grok.EditForm):
    93     """Manage the basic properties of a `Department` instance.
    94     """
    95 
    96     form_fields = grok.AutoFields(IDepartment)
    97 
    98     @property
    99     def label(self):
    100         # Set department name as form label
    101         return self.form_fields['title'].field.get(self.context)
    102    
    103     @grok.action('Save')
    104     def save(self, **data):
    105         self.applyData(self.context, **data)
    106         return
    107    
    108     @grok.action('Save and return')
    109     def saveAndReturn(self, **data):
    110         self.applyData(self.context, **data)
    111         self.redirect(self.url(self.context))
    112         return
    113 
    114     @grok.action('Cancel')
    115     def cancel(self, **data):
    116         self.redirect(self.url(self.context))
    117         return
    118 
    119 class ManageDepartment(FormWrapMixin, grok.Viewlet):
    120     """A viewlet that renders the `ManageForm`.
    121     """
    122     grok.viewletmanager(MainArea)
    123     grok.context(IDepartment)
    124     grok.view(Manage)
    125     grok.require('waeup.manageUniversity')
    126    
    127     formview_name = 'manageform' # The name of the formview we want to
    128                                  # be rendered in this viewlet.
    129 
    130 class ManageLink(grok.Viewlet):
    131     grok.viewletmanager(LeftSidebar)
    132     grok.context(IDepartment)
    133     grok.view(Index)
    134     grok.order(5)
    135     # This link is only displayed, if the user is allowed to use it!
    136     grok.require('waeup.manageUniversity')
    137    
    138     def render(self):
    139         return u'<div class="portlet"><a href="manage">Settings</a></div>'
    140 
    141 class AddCourseForm(grok.AddForm):
    142     grok.context(IDepartment)
    143     # We need a deepcopy here, because otherwise also all other
    144     # ICourse forms would be affected.
    145     form_fields = copy.deepcopy(grok.AutoFields(ICourse))
    146     form_fields.get('code').field.readonly = False
    147     label = 'Add a course'
    148 
    149     @grok.action('Add course')
    150     def addCourse(self, **data):
    151         course = createObject(u'waeup.Course')
    152         self.applyData(course, **data)
    153         try:
    154             self.context.courses.addCourse(course)
    155         except DuplicationError:
    156             self.status = Invalid('The code chosen already exists '
    157                                   'in the database')
    158             return
    159         self.redirect(self.url(self.context))
    160 
    161     @grok.action('Cancel')
    162     def cancel(self, **data):
    163         self.redirect(self.url(self.context))
    164         return
    165 
    166        
    167 class AddCourse(FormWrapMixin, grok.Viewlet):
    168     """A viewlet that wraps the `AddFacultyForm`.
    169     """
    170     grok.viewletmanager(MainArea)
    171     grok.context(IDepartment)
    172     grok.view(AddCourse)
    173     grok.require('waeup.manageUniversity')
    174 
    175     formview_name = 'addcourseform' # The name of the formview we
    176                                     # want to be rendered in this
    177                                     # viewlet.
    178 
    179 class AddCertificateForm(grok.AddForm):
    180     grok.context(IDepartment)
    181     # We need a deepcopy here, because otherwise also all other
    182     # ICertificate forms would be affected.
    183     form_fields = copy.deepcopy(grok.AutoFields(ICertificate))
    184     form_fields.get('code').field.readonly = False
    185     label = 'Add a certificate'
    186 
    187     @grok.action('Add certificate')
    188     def addCertificate(self, **data):
    189         certificate = createObject(u'waeup.Certificate')
    190         self.applyData(certificate, **data)
    191         try:
    192             self.context.certificates.addCertificate(certificate)
    193         except DuplicationError:
    194             self.status = Invalid('The name chosen already exists '
    195                                   'in the database')
    196             return
    197         self.redirect(self.url(self.context))
    198 
    199     @grok.action('Cancel')
    200     def cancel(self, **data):
    201         self.redirect(self.url(self.context))
    202         return
    203 
    204        
    205 class AddCertificate(FormWrapMixin, grok.Viewlet):
    206     """A viewlet that wraps the `AddFacultyForm`.
    207     """
    208     grok.viewletmanager(MainArea)
    209     grok.context(IDepartment)
    210     grok.view(AddCertificate)
    211     grok.require('waeup.manageUniversity')
    212 
    213     formview_name = 'addcertificateform' # The name of the formview we
    214                                          # want to be rendered in this
    215                                          # viewlet.
Note: See TracChangeset for help on using the changeset viewer.