products/CPSonFive.old

changeset 20:bdaa4cdc1995

- Now monkey patches the update method of EditViews to return a translated status message.
author lregebro
date Wed, 05 Oct 2005 14:08:29 +0000
parents 3374f826ead5
children de0a323b2128
files CHANGES __init__.py
diffstat 2 files changed, 61 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/CHANGES
     1.2 +++ b/CHANGES
     1.3 @@ -3,7 +3,8 @@
     1.4  -
     1.5  New features:
     1.6  ~~~~~~~~~~~~~
     1.7 --
     1.8 +- Now monkey patches the update method of EditViews to return a translated
     1.9 +  status message.
    1.10  Bug fixes:
    1.11  ~~~~~~~~~~
    1.12  -
     2.1 --- a/__init__.py
     2.2 +++ b/__init__.py
     2.3 @@ -66,9 +66,68 @@
     2.4          '''See interface IUserPreferredCharsets'''
     2.5          return ['iso-8859-15']
     2.6  
     2.7 +
     2.8 +try: # Five 1.1
     2.9 +    from Products.Five.form import EditView, Update, applyWidgetsChanges
    2.10 +except ImportError: # Five 1.0
    2.11 +    from Products.Five.browser import EditView, Update
    2.12 +from zope.app.form.interfaces import WidgetsError
    2.13 +from zope.app.form.utility import setUpEditWidgets, applyWidgetsChanges
    2.14 +from zope.app.event.objectevent import ObjectModifiedEvent
    2.15 +from zope.app.i18n import ZopeMessageIDFactory as _
    2.16 +import transaction, datetime
    2.17 +
    2.18 +def EditViewUpdate(self):
    2.19 +    if self.update_status is not None:
    2.20 +        # We've been called before. Just return the status we previously
    2.21 +        # computed.
    2.22 +        return self.update_status
    2.23 +
    2.24 +    status = ''
    2.25 +
    2.26 +    content = self.adapted
    2.27 +
    2.28 +    if Update in self.request.form.keys():
    2.29 +        changed = False
    2.30 +        try:
    2.31 +            changed = applyWidgetsChanges(self, self.schema,
    2.32 +                target=content, names=self.fieldNames)
    2.33 +            # We should not generate events when an adapter is used.
    2.34 +            # That's the adapter's job.
    2.35 +            if changed and self.context is self.adapted:
    2.36 +                notify(ObjectModifiedEvent(content))
    2.37 +        except WidgetsError, errors:
    2.38 +            self.errors = errors
    2.39 +            status = _("An error occured.")
    2.40 +            transaction.abort()
    2.41 +        else:
    2.42 +            setUpEditWidgets(self, self.schema, source=self.adapted,
    2.43 +                             ignoreStickyValues=True,
    2.44 +                             names=self.fieldNames)
    2.45 +            if changed:
    2.46 +                self.changed()
    2.47 +                # It's going to be very nice to drop Five 1.0.x support:
    2.48 +                localizer = getattr(self.context, 'Localizer', None)
    2.49 +                if localizer is not None:
    2.50 +                    
    2.51 +                    status = localizer.default("Updated on %(date_time)s")
    2.52 +                    format = str(localizer.default('date_medium'))
    2.53 +                    date = datetime.datetime.now().strftime(format)
    2.54 +                    status = status % {'date_time': date}
    2.55 +                else:
    2.56 +                    status = _("Updated on ${date_time}")
    2.57 +                    status.mapping = {'date_time': str(datetime.utcnow())}
    2.58 +
    2.59 +    self.update_status = status
    2.60 +    return status
    2.61 +    
    2.62 +    
    2.63  def initialize(context):
    2.64  
    2.65      # Zope3monkey
    2.66      from zope.app.form.browser.itemswidgets import ItemsWidgetBase
    2.67      ItemsWidgetBase.textForValue = textForValue
    2.68 +    
    2.69 +    # Five monkey
    2.70 +    EditView.update = EditViewUpdate
    2.71      
    2.72 \ No newline at end of file