Changeset 371

Show
Ignore:
Timestamp:
05/23/08 14:23:23 (6 months ago)
Author:
johnbywater
Message:

Added stuff to postpone and then add-in references to instances of the same class, needed for persisting linked lists.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/dm/migrate.py

    r322 r371  
    111111        classRefs = self.outstandingReferences[className] 
    112112        for attrTypeName in classRefs.values(): 
     113            if attrTypeName == className: 
     114                continue 
    113115            if attrTypeName in self.outstandingReferences: 
    114116                return True 
     
    168170            classDataKeys.sort() 
    169171            #print "Iterating over %s %s" % (className, classDataKeys) 
     172            reflexiveClassData = {} 
     173            freshDomainObjects = {} 
    170174            for objectId in classDataKeys: 
    171175                #if objectId == 'metaData': 
     
    178182                self.logger.info(msg) 
    179183                strObjectData = {} 
     184                reflexiveStrObjectData = {} 
    180185                for attr in domainClass.meta.attributes: 
    181186                    if attr.isAssociateList: 
     
    183188                    elif attr.isImageFile: 
    184189                        continue 
    185                     else
    186                         if objectData.has_key(attr.name): 
    187                             value = objectData[attr.name] 
    188                             if attr.isDomainObjectRef and ( 
    189                                 value.__class__ == int 
    190                             ): 
    191                                 idMapKey = "%s %s" % (attr.typeName, value) 
    192                                 if idMapKey in self.idMap.keys(): 
    193                                     mappedValue = self.idMap[idMapKey] 
    194                                     #msg = "Updating %s %s from %s to %s" % ( 
    195                                     #    className, 
    196                                     #    attr.name, 
    197                                     #    value, 
    198                                     #    mappedValue, 
    199                                     #) 
    200                                     #print msg 
    201                                     value = mappedValue 
    202                             if value.__class__ == unicode: 
    203                                 # SQLObject doesn't handle unicode. 
    204                                 try: 
    205                                     value = str(value) 
    206                                 except: 
    207                                     msg = "Failed to convert unicode to str" 
    208                                     msg += " for '%s' of %s %s: %s" % ( 
    209                                         attr.name, className, objectId, value 
    210                                    
    211                                     self.logger.error(msg) 
    212                                     msg = "Continuing with null string value." 
    213                                     self.logger.info(msg) 
    214                                     value = "" 
    215  
     190                    elif objectData.has_key(attr.name)
     191                        value = objectData[attr.name] 
     192                        if attr.isDomainObjectRef and value.__class__ == int: 
     193                            idMapKey = "%s %s" % (attr.typeName, value) 
     194                            if idMapKey in self.idMap.keys(): 
     195                                mappedValue = self.idMap[idMapKey] 
     196                                #msg = "Updating %s %s from %s to %s" % ( 
     197                                #    className, 
     198                                #    attr.name, 
     199                                #    value, 
     200                                #    mappedValue, 
     201                                #) 
     202                                #print msg 
     203                                value = mappedValue 
     204                        if value.__class__ == unicode: 
     205                            # SQLObject doesn't handle unicode. 
     206                            try: 
     207                                value = str(value) 
     208                            except: 
     209                                msg = "Failed to convert unicode to str" 
     210                                msg += " for '%s' of %s %s: %s" % ( 
     211                                    attr.name, className, objectId, value 
     212                                ) 
     213                                self.logger.error(msg) 
     214                                msg = "Continuing with null string value." 
     215                                self.logger.info(msg
     216                                value = "" 
     217 
     218                        if attr.isDomainObjectRef and (attr.typeName==className): 
     219                            reflexiveStrObjectData[attr.name] = value 
     220                        else: 
    216221                            strObjectData[attr.name] = value 
     222                if reflexiveStrObjectData: 
     223                    reflexiveClassData[objectId] = reflexiveStrObjectData 
    217224                objectDict = MultiValueDict() 
    218225                objectDict.update(strObjectData) 
     
    256263                domainObject = manipulator.domainObject 
    257264                newObjectId = domainObject.id 
     265                freshDomainObjects[newObjectId] = domainObject 
    258266                if newObjectId != objectId: 
    259267                    idMapKey = "%s %s" % (className, objectId) 
     
    263271                ) 
    264272                #print msg 
     273 
     274 
     275            reflexiveIds = reflexiveClassData.keys() 
     276            reflexiveIds.sort() 
     277            for id in reflexiveIds: 
     278                reflexiveStrObjectData = reflexiveClassData[id] 
     279                domainObject = freshDomainObjects.pop(id) 
     280                manipulator=DomainObjectManipulator(classRegister,domainObject) 
     281                objectDict = MultiValueDict() 
     282                objectDict.update(reflexiveStrObjectData) 
     283                for attrName in reflexiveStrObjectData: 
     284                    m = domainObject.meta.attributeNames[attrName] 
     285                    refObject = m.makeValueFromMultiValueDict(objectDict) 
     286                    setattr(domainObject, attrName, refObject) 
     287                    domainObject.save() 
     288 
    265289            #print "Importing %s objects completed OK." % className 
    266290        #print "Imported all domain objects records OK."