Package orm2 :: Package ui
[hide private]
[frames] | no frames]

Source Code for Package orm2.ui

  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  # $Log: __init__.py,v $ 
 30  # Revision 1.9  2007/04/14 20:36:45  diedrich 
 31  # Make view raise an exceptin if its didn't run. 
 32  # 
 33  # Revision 1.8  2006/09/19 14:26:25  diedrich 
 34  # View_spec.import_() uses the sys.modules cache to speed things 
 35  # up. Also in debugging mode, it reload() to make sure changes matter. 
 36  # 
 37  # Revision 1.7  2006/09/07 13:56:16  diedrich 
 38  # Added widget_spec mechanism (again). 
 39  # 
 40  # Revision 1.6  2006/09/04 15:56:26  diedrich 
 41  # The view class now has a method called ds() as opposed to an 
 42  # attribute. (This reverts to a previous state due to practical 
 43  # experience). 
 44  # 
 45  # Revision 1.5  2006/07/08 17:10:53  diedrich 
 46  # Changed view.ds() to view.ds like in procedure 
 47  # 
 48  # Revision 1.4  2006/07/04 22:48:41  diedrich 
 49  # Added belongs_to() method to widget class. 
 50  # 
 51  # Revision 1.3  2006/06/10 18:06:12  diedrich 
 52  # Rewrote widget handling 
 53  # 
 54  # Revision 1.2  2006/05/15 21:49:10  diedrich 
 55  # Working on ui basics 
 56  # 
 57  # Revision 1.1  2006/04/28 09:56:41  diedrich 
 58  # Initial commit 
 59  # 
 60  # 
 61   
 62  """ 
 63  Someday there will be user interface 'glue' in this module. Now there 
 64  is just a lot of undocumented code that makes no sense. 
 65  """ 
 66   
 67  import sys, thread, imp 
 68  from string import * 
 69  from orm2 import debug 
 70   
 71  ###################################################################### 
 72  ## Widgets 
 73  ###################################################################### 
 74   
 75  _widget_index = 1 
 76   
77 -class widget:
78 - def __init__(self, **kw):
79 global _widget_index 80 81 self.params = kw 82 83 lock = thread.allocate_lock() 84 lock.acquire() 85 self.__index__ = _widget_index 86 _widget_index += 1 87 lock.release()
88
89 - def __init_dbproperty__(self, dbproperty):
90 self.dbproperty = dbproperty
91
92 - def __cmp__(self, other):
93 """ 94 This makes sort() on sequences of widget objects use the _widget_index 95 for sorting 96 """ 97 return cmp(self.__index__, other.__index__)
98
99 - def belongs_to(self, module_name):
100 raise NotImplementedError()
101
102 - def __call__(self, dbobj, **kw):
103 params = self.params.copy() 104 params.update(kw) 105 106 return self.actual_widget(dbobj, self.dbproperty, **params)
107
108 - class actual_widget:
109 - def __init__(self, dbobj, dbproperty, **kw):
110 self.dbobj = dbobj 111 self.dbproperty = dbproperty 112 113 for name, value in kw.items(): 114 setattr(self, name, value)
115 116 117 118 ###################################################################### 119 ## Views 120 ###################################################################### 121
122 -class view:
123 - def __init__(self, dbobj=None, result=None):
124 self.dbobj = dbobj 125 if dbobj is not None: self.dbclass = dbobj.__class__ 126 127 self.result = result 128 if result is not None: self.dbclass = result.dbclass 129 130 if (dbobj is None and result is None) or \ 131 (dbobj is not None and result is not None): 132 raise ValueError("A view must be initialized either with " + \ 133 "dbobj XOR result set to an appropriate value") 134 135 if self.dbobj is not None: 136 self._ds = dbobj.__ds__() 137 else: 138 self._ds = result.ds
139
140 - def ds(self):
141 if not hasattr(self, "_ds"): 142 raise Exception("View without a constructor! You forgot to call" 143 " view.__init__() in one of your views, so the" 144 " view knows of no ds. It simply won't work" 145 " that way ;-") 146 147 if self._ds is None: 148 if self.dbobj is not None: 149 self._ds = self.dbobj.__ds__() 150 else: 151 self._ds = self.result.ds 152 153 154 if self._ds is None: 155 raise ValueError("Plase make sure any dbobj you use with a "+\ 156 "view knows a datasource (use the __ds "+ \ 157 "param to its constructor!)") 158 else: 159 return self._ds
160 161
162 -class view_spec:
163 """ 164 The view spec class provides a mechanism to lazily load views at runtime. 165 This avoids infinite loops in complex module hierarchies that have 166 seperate modules for dbclasses and views. Defining you dbclass like this:: 167 168 class font(dbobject): 169 id = serial() 170 ps_name = text() 171 user_name = Unicode() 172 173 preview = view_spec('myproject.views.font') 174 175 will cause the myproject.views.font module to be loaded. It must contain 176 a class names preview which inherits from orm2.ui.view. This class will be 177 instantiated and put into the font dbobject on initialization. 178 """
179 - def __init__(self, module_name):
181
182 - def import_(self, class_name):
183 if sys.modules.has_key(self.module_name): 184 module = sys.modules[self.module_name] 185 if debug.debug.verbose: 186 reload(module) 187 else: 188 complete_module_name = self.module_name 189 module_path = split(complete_module_name, ".") 190 module_name = module_path[-1] 191 module_path = join(module_path, ".") 192 193 module = __import__(module_path, 194 globals(), locals(), 195 module_name) 196 197 sys.modules[self.module_name] = module 198 199 return getattr(module, class_name)
200 201 202 203 # Local variables: 204 # mode: python 205 # ispell-local-dictionary: "english" 206 # End: 207