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

Source Code for Module Products.ZenUtils.Ext

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 or (at your 
 8  # option) any later version as published by the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13   
14  # Functions to simplify form submission return values. 
15   
16  import transaction 
17   
18  from Products.ZenUtils.jsonutils import json, unjson 
19  from Products.ZenUtils.extdirect.zope.router import ZopeDirectRouter as DirectRouter 
20  from Products.ZenUtils.extdirect.router import DirectResponse 
21 22 -class FormResponse(object):
23 """ 24 Builds a response for an Ext form. 25 """ 26 _errors = None 27 _redirect = None 28
29 - def has_errors(self):
30 return bool(self._errors)
31
32 - def redirect(self, url):
33 self._redirect = url
34
35 - def error(self, field_name, error_text):
36 if self._errors is None: 37 self._errors = {} 38 self._errors[field_name] = error_text
39 40 @json
41 - def get_response(self):
42 return { 43 'success': not self.has_errors(), 44 'redirect': self._redirect, 45 'errors': self._errors or {} 46 }
47
48 49 -def form_action(f):
50 """ 51 Decorator for methods that are the targets of Ext form submission. 52 53 Provides transaction rollback, so methods can be used as their own 54 validation without harm. 55 """ 56 def inner(*args, **kwargs): 57 savepoint = transaction.savepoint() 58 result = f(*args, **kwargs) 59 if isinstance(result, FormResponse): 60 if result.has_errors(): 61 savepoint.rollback() 62 return result.get_response() 63 return result
64 return inner 65