@@ -127,13 +127,19 @@ def sort_repr (self, cls, val, name):
127127class _Pointer (_Type ):
128128 """An object designating a Pointer property that links or multilinks
129129 to a node in a specified class."""
130- def __init__ (self , classname , do_journal = 'yes' , required = False ,
131- default_value = None ):
132- """ Default is to journal link and unlink events
130+ def __init__ (self , classname , do_journal = 'yes' , try_id_parsing = 'yes' ,
131+ required = False , default_value = None ):
132+ """ Default is to journal link and unlink events.
133+ When try_id_parsing is false, we don't allow IDs in input
134+ fields (the key of the Link or Multilink property must be
135+ given instead). This is useful when the name of a property
136+ can be numeric. It will only work if the linked item has a
137+ key property and is a questionable feature for multilinks.
133138 """
134139 super (_Pointer , self ).__init__ (required , default_value )
135140 self .classname = classname
136141 self .do_journal = do_journal == 'yes'
142+ self .try_id_parsing = try_id_parsing == 'yes'
137143 def __repr__ (self ):
138144 """more useful for dumps. But beware: This is also used in schema
139145 storage in SQL backends!
@@ -145,10 +151,13 @@ class Link(_Pointer):
145151 """An object designating a Link property that links to a
146152 node in a specified class."""
147153 def from_raw (self , value , db , propname , ** kw ):
148- if value == '-1' or not value :
154+ if ( self . try_id_parsing and value == '-1' ) or not value :
149155 value = None
150156 else :
151- value = convertLinkValue (db , propname , self , value )
157+ if self .try_id_parsing :
158+ value = convertLinkValue (db , propname , self , value )
159+ else :
160+ value = convertLinkValue (db , propname , self , value , None )
152161 return value
153162 def sort_repr (self , cls , val , name ):
154163 if not val :
@@ -213,7 +222,10 @@ def from_raw(self, value, db, klass, propname, itemid, **kw):
213222 do_set = 0
214223
215224 # look up the value
216- itemid = convertLinkValue (db , propname , self , item )
225+ if self .try_id_parsing :
226+ itemid = convertLinkValue (db , propname , self , item )
227+ else :
228+ itemid = convertLinkValue (db , propname , self , item , None )
217229
218230 # perform the add/remove
219231 if remove :
@@ -1356,7 +1368,7 @@ class HyperdbValueError(ValueError):
13561368def convertLinkValue (db , propname , prop , value , idre = re .compile ('^\d+$' )):
13571369 """ Convert the link value (may be id or key value) to an id value. """
13581370 linkcl = db .classes [prop .classname ]
1359- if not idre .match (value ):
1371+ if not idre or not idre .match (value ):
13601372 if linkcl .getkey ():
13611373 try :
13621374 value = linkcl .lookup (value )
0 commit comments