Changeset 3940 for waeup/branches


Ignore:
Timestamp:
9 Feb 2009, 21:39:00 (16 years ago)
Author:
uli
Message:

Add addHostel stuff for hostelcontainers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-rewrite/src/waeup/hostel/hostelcontainer.py

    r3937 r3940  
    11import grok
    22
    3 from waeup.interfaces import IHostelContainer
    4 from waeup.viewlets import MainArea, Index
     3from waeup.interfaces import IHostelContainer, IHostel
     4from waeup.viewlets import MainArea, Index, Add, FormWrapMixin
    55from hostel import Hostel
    66
     
    1313    next_id = 0L
    1414
     15    def addHostel(self, hostel):
     16        if not IHostel.providedBy(hostel):
     17            raise TypeError('HostelContainers contain only IHostel instances')
     18        id = str(self.next_id)
     19        self[id] = hostel
     20        while self.next_id in self.keys():
     21            # Look for next unused int...
     22            self.next_id += 1
     23        return id
     24
     25
    1526# We register HostelContainer as a utility. This way we can
    1627# call getUtility(IHostelContainer) from anywhere in the code and
     
    1829grok.global_utility(HostelContainer, provides=IHostelContainer)
    1930
     31class AddHostelForm(grok.AddForm):
     32    grok.context(IHostelContainer)
     33    form_fields = grok.AutoFields(IHostel)
     34    label = 'Add a hostel'
     35
     36    @grok.action('Add hostel')
     37    def addHostel(self, **data):
     38        hostel = getUtility(IHostel)
     39        self.applyData(hostel, **data)
     40        try:
     41            self.context.addHostel(hostel)
     42        except DuplicationError:
     43            self.status = Invalid('The name chosen already exists '
     44                                  'in the database')
     45            return
     46        self.redirect(self.url(self.context))
     47
     48class AddHostel(FormWrapMixin, grok.Viewlet):
     49    """A viewlet that wraps the `AddHostelForm`.
     50    """
     51    grok.viewletmanager(MainArea)
     52    grok.context(IHostelContainer)
     53    grok.view(Add)
     54    grok.require('waeup.manageUniversity')
     55
     56    formview_name = 'addhostelform' # The name of the formview we want
     57                                    # to be rendered in this viewlet.
     58
Note: See TracChangeset for help on using the changeset viewer.