Package Products :: Package ZenUtils :: Module Ext |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.Ext

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  # Functions to simplify form submission return values.
    12
    13  import transaction
    14
    15  from Products.ZenUtils.jsonutils import json, unjson
    16  from Products.ZenUtils.extdirect.zope.router import ZopeDirectRouter as DirectRouter
    17  from Products.ZenUtils.extdirect.router import DirectResponse
    18
    19 -class FormResponse(object):
    20      """
    21      Builds a response for an Ext form.
    22      """
    23      _errors = None
    24      _redirect = None
    25
    26 -    def has_errors(self):
    27          return bool(self._errors)
    28
    29 -    def redirect(self, url):
    30          self._redirect = url
    31
    32 -    def error(self, field_name, error_text):
    33          if self._errors is None:
    34              self._errors = {}
    35          self._errors[field_name] = error_text
    36
    37      @json
    38 -    def get_response(self):
    39          return {
    40              'success':  not self.has_errors(),
    41              'redirect': self._redirect,
    42              'errors':   self._errors or {}
    43          }
    44
    45
    46 -def form_action(f):
    47      """
    48      Decorator for methods that are the targets of Ext form submission.
    49
    50      Provides transaction rollback, so methods can be used as their own
    51      validation without harm.
    52      """
    53      def inner(*args, **kwargs):
    54          savepoint = transaction.savepoint()
    55          result = f(*args, **kwargs)
    56          if isinstance(result, FormResponse):
    57              if result.has_errors():
    58                  savepoint.rollback()
    59              return result.get_response()
    60          return result
    61      return inner
    62