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

Source Code for Module 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 as published by 
 8  # 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.json import json 
19   
20 -class FormResponse(object):
21 """ 22 Builds a response for an Ext form. 23 """ 24 _errors = None 25 _redirect = None 26
27 - def has_errors(self):
28 return bool(self._errors)
29
30 - def redirect(self, url):
31 self._redirect = url
32
33 - def error(self, field_name, error_text):
34 if self._errors is None: 35 self._errors = {} 36 self._errors[field_name] = error_text
37 38 @json
39 - def get_response(self):
40 return { 41 'success': not self.has_errors(), 42 'redirect': self._redirect, 43 'errors': self._errors or {} 44 }
45 46
47 -def form_action(f):
48 """ 49 Decorator for methods that are the targets of Ext form submission. 50 51 Provides transaction rollback, so methods can be used as their own 52 validation without harm. 53 """ 54 def inner(*args, **kwargs): 55 savepoint = transaction.savepoint() 56 result = f(*args, **kwargs) 57 if isinstance(result, FormResponse): 58 if result.has_errors(): 59 savepoint.rollback() 60 return result.get_response() 61 return result
62 return inner 63