Package orm2 :: Package ui :: Package xist :: Module views
[hide private]
[frames] | no frames]

Source Code for Module orm2.ui.xist.views

  1  #!/usr/bin/env python 
  2  # -*- coding: iso-8859-1 -*- 
  3   
  4  ##  This file is part of orm, The Object Relational Membrane Version 2. 
  5  ## 
  6  ##  Copyright 2002-2006 by Diedrich Vorberg <diedrich@tux4web.de> 
  7  ## 
  8  ##  All Rights Reserved 
  9  ## 
 10  ##  For more Information on orm see the README file. 
 11  ## 
 12  ##  This program is free software; you can redistribute it and/or modify 
 13  ##  it under the terms of the GNU General Public License as published by 
 14  ##  the Free Software Foundation; either version 2 of the License, or 
 15  ##  (at your option) any later version. 
 16  ## 
 17  ##  This program is distributed in the hope that it will be useful, 
 18  ##  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  ##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  ##  GNU General Public License for more details. 
 21  ## 
 22  ##  You should have received a copy of the GNU General Public License 
 23  ##  along with this program; if not, write to the Free Software 
 24  ##  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 25  ## 
 26  ##  I have added a copy of the GPL in the file gpl.txt. 
 27   
 28  """ 
 29    
 30  """ 
 31   
 32  from orm2.ui import widget, view 
 33  from orm2.exceptions import ValidatorException 
 34   
 35  from i18n import translate, _ 
 36   
 37  from ll.xist.ns import xml, html, chars 
 38   
 39   
40 -class xist_view(view):
41 - def __getattr__(self, name):
42 if not hasattr(self, "context"): 43 if not hasattr(self, "_ds"): 44 raise AttributeError("No context, (yet?), " + name) 45 46 self.context = self.ds().context 47 48 if name == "context": 49 return self.context 50 51 return getattr(self.context, name)
52 53 zope_view = xist_view 54
55 -class xist_form(xist_view):
56 - def rows(self):
57 """ 58 Return a list that contains ll.xist.Node objects and _xist_widget 59 object. The nodes will be passed into the form as-is, the widgets 60 will be initialized according to form usage, either with the values 61 from the request+errors (on failed submits), values from the dbobject 62 (display for a modification) or the defaults (for a new dbobject). 63 64 This default implementation will return all the widget_specs that 65 are xist widgets from the dbclass. 66 """ 67 for widget_spec in self.dbobj.__widget_specs__("xist"): 68 yield widget_spec
69
70 - def widgets(self):
71 """ 72 Return a list of widgets in this form (i.e. extract the 73 instances of widget from the result of (rows). 74 """ 75 rows = self.rows() 76 return filter(lambda row: isinstance(row, widget), rows)
77
78 - def set_from_request(self, ignore_if_unset=True):
79 """ 80 Call the set_from_request() method of every widget. Catch validator 81 exceptions and process them into error messages using the 82 error_message() function. Return a dictionary as 83 84 { 'param_name', [ error1, error2, ..., errorn ] } 85 86 An empty dict means successful submition ;-) 87 """ 88 ret = {} 89 for widget in self.widgets(): 90 actual_widget = widget(self.dbobj) 91 92 errors = ret.get(actual_widget.name, []) 93 94 try: 95 actual_widget.set_from_request(use_default=False, 96 ignore_if_unset=ignore_if_unset) 97 except ValidatorException, e: 98 msg = self.error_message(actual_widget, e) 99 errors.append(msg) 100 101 ret[actual_widget.name] = errors 102 103 return ret
104
105 - def error_message(self, widget, exception):
106 """ 107 Return an error message for when widget raised 108 ValidatorException exception. This error message will be 109 translated into the user's language by xist.widgets.row() 110 if need be. 111 """ 112 return unicode(str(exception))
113
114 - def __call__(self, errors={}, **kw):
115 form = html.form(**kw) 116 117 for row in self.rows(): 118 if isinstance(row, widget): 119 actual_widget = row(self.dbobj) 120 error = errors.get(actual_widget.name, None) 121 form.append(actual_widget.row(error=error)) 122 else: 123 form.append(row) 124 125 return form
126 127 128 129 130 131 # Local variables: 132 # mode: python 133 # ispell-local-dictionary: "english" 134 # End: 135