Diff of /projects/FastData2/trunk/fastdata2/ormdatahandler.py [r2586] .. [r2587] Maximize Restore

  Switch to side-by-side view

--- a/projects/FastData2/trunk/fastdata2/ormdatahandler.py
+++ b/projects/FastData2/trunk/fastdata2/ormdatahandler.py
@@ -1,105 +1,62 @@
-import re
-
-from toscawidgets.api import Widget, default_rule
-#XXX: Better use twGrids instead as forms.datagrid is broken when displaying
-#      widgets inside cells when using non-genshi page templates
-from toscawidgets.widgets.forms import FormField, FormFieldRepeater, \
-                                        datagrid
-from toscawidgets.genericfunctions import generic, PriorityDisambiguated
-from view import widget_for, column_for
-
 try:
     from fastdata2.sodatahandler import SODataHandler
 except ImportError:
-    SODataHandler = object
+    SODataHandler = None
+else:
+    import sqlobject as so
     
 try:
     from fastdata2.sadatahandler import SADataHandler
 except ImportError:
-    SADataHandler = object
+    SADataHandler = None
+else:
+    import sqlalchemy as sa
 
 
 # For this to work, we need to import at least one of SODataHandler or
 # SADataHandler. If we can't import either, then we raise an exception    
-if SODataHandler == SADataHandler:
+if not SODataHandler and not SADataHandler:
     raise ImportError, 'Unable to successfully import SADataHandler or ' \
                            'SODataHandler'
     
-#XXX: If sqlobject is not installed the metaclass raises an exception as
-#     as the MRO cannot be computed for (object,SADataHandler)
-class ORMDataHandler(SODataHandler, SADataHandler):
-    "An IDataHandler for both SQLObject and SQLAlchemy."
+if SODataHandler and SADataHandler:
+
+    class ORMDataHandler(SADataHandler, SODataHandler):
+        "An IDataHandler for both SQLObject and SQLAlchemy."
+
+        def filtered_form_vars(self, class_, environ):
+            if hasattr(class_, '_SO_getValue'):
+                fvars = SODataHandler.filtered_form_vars(self, class_, environ)
+            else:
+                fvars = SADataHandler.filtered_form_vars(self, class_, environ)   
+            return fvars
+
+elif SADataHandler:
+    ORMDataHandler = SADataHandler
+elif SODataHandler:
+    ORMDataHandler = SODataHandler
     
-    def filtered_form_vars(self, class_, environ):
-        if hasattr(class_, '_SO_getValue'):
-            fvars = SODataHandler.filtered_form_vars(self, class_, environ)
-        else:
-            fvars = SADataHandler.filtered_form_vars(self, class_, environ)   
-        return fvars
         
 def is_sqlobject_obj(obj):
-    if SODataHandler is object:
+    if not SODataHandler:
         return False
     return SODataHandler.is_so_obj(obj)
     
 def is_sqlalchemy_obj(obj):
-    if SADataHandler is object:
+    if not SADataHandler:
         return False
     return SADataHandler.is_sa_obj(obj)
     
 def is_orm_obj(obj):
     return is_sqlalchemy_obj(obj) or is_sqlobject_obj(obj)
     
-
-is_list_with_data = "isinstance(obj, list) and len(obj)>0"
-[widget_for.when(is_list_with_data)]
-def __widget_for_list(obj, cls, **kw):
-    """
-        >>> w = widget_for(obj=[None,None])
-        >>> o = w.render()
-        >>> 'unknown-0' in o
-        True
-        >>> 'unknown-1' in o
-        True
-        >>> 'unknown-2' in o
-        False
-
-    """
-    widget = widget_for(obj=obj[0], **kw)
-    repetitions = len(obj) + kw.get('extra_repetitions', 0)
-    if isinstance(widget, FormField):
-        return FormFieldRepeater(
-            id=widget.id, widget=widget, repetitions=repetitions
-            )
-    return WidgetRepeater(id=widget.id, widget=widget, repetitions=repetitions)
-
-so_list_index = is_list_with_data + \
-            ' and is_sqlobject_obj(obj[0]) and kw.get("action")=="index"'
-[widget_for.when(so_list_index)]
-def _widget_for_dumbsoindex(obj, cls, **kw):
-    w = Widget(id=obj[0].__class__.__name__ + '_FD2index',
-                template='<div id="$id">I am a sqlobject list.</div>')
-    return w
+def is_sa_column(obj):
+    if not SADataHandler:
+        return False
+    return isinstance(obj, sa.schema.Column)
     
-sa_list_index = is_list_with_data + \
-                'and is_sqlalchemy_obj(obj[0]) and kw.get("action")=="index"'
-[widget_for.when(sa_list_index)]
-def _widget_for_dumbsaindex(obj, cls, **kw):
-    w = Widget(id=obj[0].__class__.__name__ +'_FD2index',
-                template='<div>I am an sqlalchemy list.</div>'  )
-    return w
-
-    
-words_re = re.compile(r'([A-Z][a-z0-9]+|[a-z0-9]+|[A-Z0-9]+)')
-
-def get_title(col_name):
-    "Get a reasonable title based on a column name."
-    words = words_re.findall(col_name)
-    return ' '.join([word.capitalize() for word in words])
-    
-[column_for.when(default_rule)]
-def default_so_col(obj, col_name, **kw):
-    col = datagrid.Column(col_name, getter=col_name, title=get_title(col_name),
-            options=kw.get('options'))
-    return col
-    
+def is_so_column(obj):
+    if not SODataHandler:
+        return False
+    return isinstance(obj, so.col.SOCol)
+