getItem(): use uid_mapping for legacy UIDs when appropriate

This commit is contained in:
Helmut Merz 2024-02-07 17:53:27 +01:00
parent bea7ed0254
commit fb9d6991cd
2 changed files with 16 additions and 5 deletions

View file

@ -26,7 +26,6 @@ class Storage(common.Storage):
self.session.execute(stmt) self.session.execute(stmt)
mark_changed(self.session) mark_changed(self.session)
def getUidTable(self, schema=None): def getUidTable(self, schema=None):
#table = getExistingTable(self.storage, self.tableName) #table = getExistingTable(self.storage, self.tableName)
#if table is None: #if table is None:

20
util.py
View file

@ -142,14 +142,25 @@ def records(context, name, factory):
# UID stuff # UID stuff
class IUid(Interface): class IUid(Interface):
"""Provides uid property."""
uid = Attribute("Unique Identifier") uid = Attribute("Unique Identifier")
def getItem(uid, intIds=None, storage=None): def getItem(uid, intIds=None, storage=None):
if storage is not None and '-' in uid: if storage is not None and '-' in uid:
return storage.getItem(uid) return storage.getItem(uid)
return getObjectForUid(uid, intIds=intIds) obj = getObjectForUid(uid, intIds=intIds)
if obj is None and storage is not None:
return getMigratedItem(uid, storage)
return obj
def getMigratedItem(uid, storage):
t = self.getUidTable
stmt = t.select().where(t.c.legacy == int(uid))
newId = storage.session.execute(stmt).scalar()
if newId is not None:
return storage.getItem(newUid)
return None
def getObjectForUid(uid, intIds=None): def getObjectForUid(uid, intIds=None):
if uid == '*': # wild card if uid == '*': # wild card
@ -173,9 +184,10 @@ def getUidForObject(obj, intIds=None):
intIds = component.getUtility(IIntIds) intIds = component.getUtility(IIntIds)
return str(intIds.queryId(obj)) return str(intIds.queryId(obj))
def getObjectsForUids(uids, adapt=True): def getObjectsForUids(uids, adapt=True, storage=None):
intIds = component.getUtility(IIntIds) intIds = component.getUtility(IIntIds)
result = [getObjectForUid(uid, intIds) for uid in uids] #result = [getObjectForUid(uid, intIds) for uid in uids]
result = [getItem(uid, intIds, storage=storage) for uid in uids]
if adapt: if adapt:
from loops.common import adapted from loops.common import adapted
return [adapted(obj) for obj in result if obj is not None] return [adapted(obj) for obj in result if obj is not None]