Ignore:
Timestamp:
26 Jan 2009, 14:56:26 (16 years ago)
Author:
uli
Message:

Make faculty container a generic grok.Container.

File:
1 edited

Legend:

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

    r3828 r3878  
    11import grok
     2from zope.component import getUtility, getMultiAdapter
     3from zope.component.interfaces import Invalid
     4from zope.exceptions import DuplicationError
    25from waeup.basecontainer import BaseContainer
    36from waeup.interfaces import IFacultyContainer
     7from waeup.viewlets import MainArea, Index, Add
    48from interfaces import IFaculty
    59from faculty import Faculty
    610
    7 class FacultyContainer(BaseContainer):
     11from zope.schema import ValidationError
     12
     13class FacultyContainer(grok.Container):
    814
    915    grok.implements(IFacultyContainer)
    1016   
    1117    name = u'Faculties'
    12     childClass = Faculty
    1318
     19    def getId(self, faculty):
     20        """We need a unified id for each faculty, which acts as key.
     21        """
     22        id = faculty.name
     23        id = id.replace(' ', '')
     24        return id.lower()
     25   
     26    def addFaculty(self, faculty):
     27        if not IFaculty.providedBy(faculty):
     28            raise TypeError('FacultyContainers contain only IFaculty instances')
     29        id = self.getId(faculty)
     30        self[id] = faculty
     31
     32# We register FacultyContainer as a utility. This way we can
     33# call getUtility(IFacultyContainer) from anywhere in the code and
     34# get an instance of FacultyContainer.
    1435grok.global_utility(FacultyContainer, provides=IFacultyContainer)
     36
     37class Content(grok.Viewlet):
     38    grok.viewletmanager(MainArea)
     39    grok.context(IFacultyContainer)
     40    grok.view(Index)
     41
     42    def getFaculties(self):
     43        """Convinience method to create a sorted list of faculties.
     44
     45        It provides a list of dicts with entries for all data needed by
     46        usual list templates.
     47        """
     48        result = []
     49        for key, val in self.context.items():
     50            result.append(dict(id=key, name=val.name))
     51        return result
     52
     53class AddFacultyForm(grok.AddForm):
     54    grok.context(IFacultyContainer)
     55    form_fields = grok.AutoFields(IFaculty)
     56
     57    @grok.action('Add faculty')
     58    def addFaculty(self, **data):
     59        faculty = getUtility(IFaculty)
     60        self.applyData(faculty, **data)
     61        try:
     62            self.context.addFaculty(faculty)
     63        except DuplicationError:
     64            self.status = Invalid('The name chosen already exists '
     65                                  'in the database')
     66            return
     67        self.redirect(self.url(self.context))
     68
     69class AddFaculty(grok.Viewlet):
     70    """A viewlet that wraps the `AddFacultyForm`.
     71    """
     72    grok.viewletmanager(MainArea)
     73    grok.context(IFacultyContainer)
     74    grok.view(Add)
     75    grok.require('waeup.manageUniversity')
     76   
     77    def update(self, **data):
     78        self.form = getMultiAdapter((self.context, self.request),
     79                                    name='addfacultyform')
     80        result = self.form.update_form()
     81        if self.request.method == 'POST':
     82            #app = get_application(self.context)
     83            #self.__parent__.redirect(self.__parent__.url(obj=app))
     84            pass
     85        return
     86
     87    def render(self):
     88        result = self.form.render()
     89        try:
     90            # strip all except the form part...
     91            result = re.match('^.+\(<form[^\>]+>.*</form>).+$', result,
     92                              re.DOTALL).groups()[0]
     93        except:
     94            # except there is no such part...
     95            pass
     96        return result
Note: See TracChangeset for help on using the changeset viewer.