Commit c1e78721 authored by Jim Fulton's avatar Jim Fulton

Added the ability to use a ZClass as a brain.

parent ceda2236
...@@ -85,8 +85,8 @@ ...@@ -85,8 +85,8 @@
__doc__='''Generic Database adapter __doc__='''Generic Database adapter
$Id: DA.py,v 1.75 1999/11/16 20:19:52 jeffrey Exp $''' $Id: DA.py,v 1.76 1999/12/06 15:31:36 jim Exp $'''
__version__='$Revision: 1.75 $'[11:-2] __version__='$Revision: 1.76 $'[11:-2]
import OFS.SimpleItem, Aqueduct, RDB import OFS.SimpleItem, Aqueduct, RDB
import DocumentTemplate, marshal, md5, base64, Acquisition, os import DocumentTemplate, marshal, md5, base64, Acquisition, os
...@@ -133,6 +133,7 @@ class DA( ...@@ -133,6 +133,7 @@ class DA(
cache_time_=0 cache_time_=0
max_cache_=100 max_cache_=100
class_name_=class_file_='' class_name_=class_file_=''
_zclass=None
allow_simple_one_argument_traversal=None allow_simple_one_argument_traversal=None
manage_options=( manage_options=(
...@@ -150,7 +151,8 @@ class DA( ...@@ -150,7 +151,8 @@ class DA(
'manage_advancedForm', 'PrincipiaSearchSource' 'manage_advancedForm', 'PrincipiaSearchSource'
)), )),
('Change Database Methods', ('Change Database Methods',
('manage_edit','manage_advanced', 'manage_testForm','manage_test')), ('manage_edit','manage_advanced', 'manage_testForm','manage_test',
'manage_product_zclass_info')),
('Use Database Methods', ('__call__',''), ('Anonymous','Manager')), ('Use Database Methods', ('__call__',''), ('Anonymous','Manager')),
) )
...@@ -231,7 +233,7 @@ class DA( ...@@ -231,7 +233,7 @@ class DA(
def manage_advanced(self, max_rows, max_cache, cache_time, def manage_advanced(self, max_rows, max_cache, cache_time,
class_name, class_file, direct=None, class_name, class_file, direct=None,
REQUEST=None): REQUEST=None, zclass=''):
"""Change advanced properties """Change advanced properties
The arguments are: The arguments are:
...@@ -267,6 +269,14 @@ class DA( ...@@ -267,6 +269,14 @@ class DA(
self.class_name_, self.class_file_ = class_name, class_file self.class_name_, self.class_file_ = class_name, class_file
self._v_brain=getBrain(self.class_file_, self.class_name_, 1) self._v_brain=getBrain(self.class_file_, self.class_name_, 1)
self.allow_simple_one_argument_traversal=direct self.allow_simple_one_argument_traversal=direct
if zclass:
for d in self.aq_acquire('_getProductRegistryData')('zclasses'):
if ("%s/%s" % (d.get('product'),d.get('id'))) == zclass:
self._zclass=d['meta_class']
break
if REQUEST is not None: if REQUEST is not None:
m="ZSQL Method advanced settings have been set" m="ZSQL Method advanced settings have been set"
return self.manage_advancedForm(self,REQUEST,manage_tabs_message=m) return self.manage_advancedForm(self,REQUEST,manage_tabs_message=m)
...@@ -296,7 +306,8 @@ class DA( ...@@ -296,7 +306,8 @@ class DA(
try: try:
try: try:
src=self(REQUEST, src__=1) src=self(REQUEST, src__=1)
if find(src,'\0'): src=join(split(src,'\0'),'\n'+'-'*60+'\n') if find(src,'\0'):
src=join(split(src,'\0'),'\n'+'-'*60+'\n')
result=self(REQUEST, test__=1) result=self(REQUEST, test__=1)
if result._searchable_result_columns(): if result._searchable_result_columns():
r=custom_default_report(self.id, result) r=custom_default_report(self.id, result)
...@@ -307,7 +318,8 @@ class DA( ...@@ -307,7 +318,8 @@ class DA(
r='<strong>Error, <em>%s</em>:</strong> %s' % (t, v) r='<strong>Error, <em>%s</em>:</strong> %s' % (t, v)
report=DocumentTemplate.HTML( report=DocumentTemplate.HTML(
'<html><BODY BGCOLOR="#FFFFFF" LINK="#000099" VLINK="#555555">\n' '<html>\n'
'<BODY BGCOLOR="#FFFFFF" LINK="#000099" VLINK="#555555">\n'
'<dtml-var manage_tabs>\n<hr>\n%s\n\n' '<dtml-var manage_tabs>\n<hr>\n%s\n\n'
'<hr><strong>SQL used:</strong><br>\n<pre>\n%s\n</pre>\n<hr>\n' '<hr><strong>SQL used:</strong><br>\n<pre>\n%s\n</pre>\n<hr>\n'
'</body></html>' '</body></html>'
...@@ -408,13 +420,16 @@ class DA( ...@@ -408,13 +420,16 @@ class DA(
else: else:
brain=self._v_brain=getBrain(self.class_file_, self.class_name_) brain=self._v_brain=getBrain(self.class_file_, self.class_name_)
zc=self._zclass
if zc is not None: zc=zc._zclass_
if type(result) is type(''): if type(result) is type(''):
f=StringIO() f=StringIO()
f.write(result) f.write(result)
f.seek(0) f.seek(0)
result=RDB.File(f,brain,p) result=RDB.File(f,brain,p, zc)
else: else:
result=Results(result, brain, p) result=Results(result, brain, p, zc)
columns=result._searchable_result_columns() columns=result._searchable_result_columns()
if test__ and columns != self._col: self._col=columns if test__ and columns != self._col: self._col=columns
return result return result
...@@ -442,6 +457,24 @@ class DA( ...@@ -442,6 +457,24 @@ class DA(
def connected(self): def connected(self):
return getattr(getattr(self, self.connection_id), 'connected')() return getattr(getattr(self, self.connection_id), 'connected')()
def manage_product_zclass_info(self):
r=[]
Z=self._zclass
Z=getattr(Z, 'aq_self', Z)
for d in self.aq_acquire('_getProductRegistryData')('zclasses'):
z=d['meta_class']
if hasattr(z._zclass_,'_p_deactivate'):
# Eek, persistent
continue
x={}
x.update(d)
x['selected'] = (z is Z) and 'selected' or ''
del x['meta_class']
r.append(x)
return r
Globals.default__class_init__(DA) Globals.default__class_init__(DA)
......
...@@ -85,8 +85,8 @@ ...@@ -85,8 +85,8 @@
__doc__='''Class for reading RDB files __doc__='''Class for reading RDB files
$Id: RDB.py,v 1.23 1999/03/10 00:15:44 klm Exp $''' $Id: RDB.py,v 1.24 1999/12/06 15:31:36 jim Exp $'''
__version__='$Revision: 1.23 $'[11:-2] __version__='$Revision: 1.24 $'[11:-2]
import regex, regsub import regex, regsub
from string import split, strip, lower, upper, atof, atoi, atol, find, join from string import split, strip, lower, upper, atof, atoi, atol, find, join
...@@ -113,9 +113,6 @@ Parsers={'n': atof, ...@@ -113,9 +113,6 @@ Parsers={'n': atof,
't': parse_text, 't': parse_text,
} }
record_classes={}
class SQLAlias(ExtensionClass.Base): class SQLAlias(ExtensionClass.Base):
def __init__(self, name): self._n=name def __init__(self, name): self._n=name
def __of__(self, parent): return getattr(parent, self._n) def __of__(self, parent): return getattr(parent, self._n)
...@@ -127,12 +124,13 @@ class DatabaseResults: ...@@ -127,12 +124,13 @@ class DatabaseResults:
""" """
_index=None _index=None
def __init__(self,file,brains=NoBrains, parent=None): def __init__(self,file,brains=NoBrains, parent=None, zbrains=None):
self._file=file self._file=file
readline=file.readline readline=file.readline
line=readline() line=readline()
self._parent=parent self._parent=parent
if zbrains is None: zbrains=NoBrains
comment_pattern=regex.compile('#') comment_pattern=regex.compile('#')
while line and comment_pattern.match(line) >= 0: line=readline() while line and comment_pattern.match(line) >= 0: line=readline()
...@@ -201,30 +199,27 @@ class DatabaseResults: ...@@ -201,30 +199,27 @@ class DatabaseResults:
# Create a record class to hold the records. # Create a record class to hold the records.
names=tuple(names) names=tuple(names)
if record_classes.has_key((names,brains)):
r=record_classes[names,brains]
else:
class r(Record, Implicit, brains):
'Result record class'
r.__record_schema__=schema class r(Record, Implicit, brains, zbrains):
for k in filter(lambda k: k[:2]=='__', Record.__dict__.keys()): 'Result record class'
setattr(r,k,getattr(Record,k))
record_classes[names,brains]=r r.__record_schema__=schema
for k in filter(lambda k: k[:2]=='__', Record.__dict__.keys()):
setattr(r,k,getattr(Record,k))
# Add SQL Aliases # Add SQL Aliases
d=r.__dict__ d=r.__dict__
for k, v in aliases: for k, v in aliases:
if not hasattr(r,k): d[k]=v if not hasattr(r,k): d[k]=v
if hasattr(brains, '__init__'): if hasattr(brains, '__init__'):
binit=brains.__init__ binit=brains.__init__
if hasattr(binit,'im_func'): binit=binit.im_func if hasattr(binit,'im_func'): binit=binit.im_func
def __init__(self, data, parent, binit=binit): def __init__(self, data, parent, binit=binit):
Record.__init__(self,data) Record.__init__(self,data)
binit(self.__of__(parent)) binit(self.__of__(parent))
r.__dict__['__init__']=__init__ r.__dict__['__init__']=__init__
self._class=r self._class=r
......
...@@ -88,8 +88,6 @@ from string import strip, lower, upper, join ...@@ -88,8 +88,6 @@ from string import strip, lower, upper, join
from Acquisition import Implicit from Acquisition import Implicit
from Record import Record from Record import Record
record_classes={}
class SQLAlias(ExtensionClass.Base): class SQLAlias(ExtensionClass.Base):
def __init__(self, name): self._n=name def __init__(self, name): self._n=name
def __of__(self, parent): return getattr(parent, self._n) def __of__(self, parent): return getattr(parent, self._n)
...@@ -101,7 +99,8 @@ class Results: ...@@ -101,7 +99,8 @@ class Results:
""" """
_index=None _index=None
def __init__(self,(items,data),brains=NoBrains, parent=None): def __init__(self,(items,data),brains=NoBrains, parent=None,
zbrains=None):
self._data=data self._data=data
self.__items__=items self.__items__=items
...@@ -110,6 +109,7 @@ class Results: ...@@ -110,6 +109,7 @@ class Results:
self._schema=schema={} self._schema=schema={}
self._data_dictionary=dd={} self._data_dictionary=dd={}
aliases=[] aliases=[]
if zbrains is None: zbrains=NoBrains
i=0 i=0
for item in items: for item in items:
name=item['name'] name=item['name']
...@@ -131,31 +131,28 @@ class Results: ...@@ -131,31 +131,28 @@ class Results:
# Create a record class to hold the records. # Create a record class to hold the records.
names=tuple(names) names=tuple(names)
if record_classes.has_key((names,brains)):
r=record_classes[names,brains] class r(Record, Implicit, brains, zbrains):
else: 'Result record class'
class r(Record, Implicit, brains):
'Result record class' r.__record_schema__=schema
for k in filter(lambda k: k[:2]=='__', Record.__dict__.keys()):
r.__record_schema__=schema setattr(r,k,getattr(Record,k))
for k in filter(lambda k: k[:2]=='__', Record.__dict__.keys()):
setattr(r,k,getattr(Record,k)) # Add SQL Aliases
record_classes[names,brains]=r d=r.__dict__
for k, v in aliases:
# Add SQL Aliases if not hasattr(r,k): d[k]=v
d=r.__dict__
for k, v in aliases: if hasattr(brains, '__init__'):
if not hasattr(r,k): d[k]=v binit=brains.__init__
if hasattr(binit,'im_func'): binit=binit.im_func
if hasattr(brains, '__init__'): def __init__(self, data, parent, binit=binit):
binit=brains.__init__ Record.__init__(self,data)
if hasattr(binit,'im_func'): binit=binit.im_func if parent is not None: self=self.__of__(parent)
def __init__(self, data, parent, binit=binit): binit(self)
Record.__init__(self,data)
if parent is not None: self=self.__of__(parent) r.__dict__['__init__']=__init__
binit(self)
r.__dict__['__init__']=__init__
self._class=r self._class=r
......
...@@ -47,7 +47,28 @@ directory of this Zope installation. ...@@ -47,7 +47,28 @@ directory of this Zope installation.
value="<dtml-var class_name_ html_quote>"></td></tr> value="<dtml-var class_name_ html_quote>"></td></tr>
<tr> <th align='LEFT'><em>Class file</em></th> <tr> <th align='LEFT'><em>Class file</em></th>
<td align='LEFT'><input name="class_file" size="30" <td align='LEFT'><input name="class_file" size="30"
value="<dtml-var class_file_ html_quote>"></td></tr> value="&dtml-class_file_;"></td></tr>
<dtml-if manage_product_zclass_info>
<tr><td> </td></tr>
<tr><td colspan=2><br><br>
You may specify a <strong>ZClass</strong> for the data records.
</td>
<tr> <th align='LEFT'><em>ZClass</em></th>
<td align='LEFT'>
<select name="zclass">
<option value="">No ZClass</option>
<dtml-in manage_product_zclass_info mapping>
<dtml-with "_(v='%s/%s' % (product, id))">
<option value="&dtml-v;" &dtml-selected;>
<dtml-var product> <dtml-var id>
(<dtml-var meta_type>)</option>
</dtml-with>
</dtml-in>
</select>
</td>
</tr>
</dtml-if manage_product_zclass_info>
<tr><td> </td></tr> <tr><td> </td></tr>
<tr><td></td><td><input type="SUBMIT" name="SUBMIT" value="Change"></td></tr> <tr><td></td><td><input type="SUBMIT" name="SUBMIT" value="Change"></td></tr>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment