comparison roundup/hyperdb.py @ 4877:2ba982dcdf2c

New Link / Multilink option "try_id_parsing"
author Ralf Schlatterbeck <rsc@runtux.com>
date Fri, 28 Mar 2014 16:47:36 +0100
parents 6998ad77841e
children adb8b787e157
comparison
equal deleted inserted replaced
4876:74b24b14071a 4877:2ba982dcdf2c
125 return val.as_seconds() 125 return val.as_seconds()
126 126
127 class _Pointer(_Type): 127 class _Pointer(_Type):
128 """An object designating a Pointer property that links or multilinks 128 """An object designating a Pointer property that links or multilinks
129 to a node in a specified class.""" 129 to a node in a specified class."""
130 def __init__(self, classname, do_journal='yes', required=False, 130 def __init__(self, classname, do_journal='yes', try_id_parsing='yes',
131 default_value = None): 131 required=False, default_value=None):
132 """ Default is to journal link and unlink events 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.
133 """ 138 """
134 super(_Pointer, self).__init__(required, default_value) 139 super(_Pointer, self).__init__(required, default_value)
135 self.classname = classname 140 self.classname = classname
136 self.do_journal = do_journal == 'yes' 141 self.do_journal = do_journal == 'yes'
142 self.try_id_parsing = try_id_parsing == 'yes'
137 def __repr__(self): 143 def __repr__(self):
138 """more useful for dumps. But beware: This is also used in schema 144 """more useful for dumps. But beware: This is also used in schema
139 storage in SQL backends! 145 storage in SQL backends!
140 """ 146 """
141 return '<%s.%s to "%s">'%(self.__class__.__module__, 147 return '<%s.%s to "%s">'%(self.__class__.__module__,
143 149
144 class Link(_Pointer): 150 class Link(_Pointer):
145 """An object designating a Link property that links to a 151 """An object designating a Link property that links to a
146 node in a specified class.""" 152 node in a specified class."""
147 def from_raw(self, value, db, propname, **kw): 153 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:
149 value = None 155 value = None
150 else: 156 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)
152 return value 161 return value
153 def sort_repr (self, cls, val, name): 162 def sort_repr (self, cls, val, name):
154 if not val: 163 if not val:
155 return val 164 return val
156 op = cls.labelprop() 165 op = cls.labelprop()
211 elif item.startswith('+'): 220 elif item.startswith('+'):
212 item = item[1:] 221 item = item[1:]
213 do_set = 0 222 do_set = 0
214 223
215 # look up the value 224 # 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)
217 229
218 # perform the add/remove 230 # perform the add/remove
219 if remove: 231 if remove:
220 try: 232 try:
221 curvalue.remove(itemid) 233 curvalue.remove(itemid)
1354 pass 1366 pass
1355 1367
1356 def convertLinkValue(db, propname, prop, value, idre=re.compile('^\d+$')): 1368 def convertLinkValue(db, propname, prop, value, idre=re.compile('^\d+$')):
1357 """ Convert the link value (may be id or key value) to an id value. """ 1369 """ Convert the link value (may be id or key value) to an id value. """
1358 linkcl = db.classes[prop.classname] 1370 linkcl = db.classes[prop.classname]
1359 if not idre.match(value): 1371 if not idre or not idre.match(value):
1360 if linkcl.getkey(): 1372 if linkcl.getkey():
1361 try: 1373 try:
1362 value = linkcl.lookup(value) 1374 value = linkcl.lookup(value)
1363 except KeyError, message: 1375 except KeyError, message:
1364 raise HyperdbValueError, _('property %s: %r is not a %s.')%( 1376 raise HyperdbValueError, _('property %s: %r is not a %s.')%(

Roundup Issue Tracker: http://roundup-tracker.org/