使用类型化的UUID而不是字符串UUID查询portal_catalog

3
我正在编写一个用于预订不同资源的Plone日历模块。该模块具有存储在Postgresql中的事件日历。每个日历都是存储在ZODB中的可塑性对象。
为了在Plone和Postgresql之间建立链接,我自然地转向了Plone对象的uuid属性。因此,Plone中的每个uuid都可以作为Postgresql中的外键使用,Postgresql原生支持uuid。
这在过去6个月对我很有帮助,直到我开始使用Plone 4.1.4。随着plone.uuid 1.0.2的引入,这种方法发生了变化,它将uuids的字符串表示从带破折号的uuids更改为没有破折号的uuids。
这个变化的问题在于,我无法确定任何给定对象将使用哪种表示形式。在Plone 4.1.4之前创建的对象包含不同格式的uuid字符串,而在之后创建的对象则没有。
长话短说,为了确保我的代码适用于任何uuid表示形式,我希望能够使用Python的uuid类型进行搜索。
所以,代替这个:
catalog.searchResults(UID='325dc47e-08f9-4fa1-bc18-3944a725adb4')

这段话的翻译如下:

这会返回不同的结果:

catalog.searchResults(UID='325dc47e08f94fa1bc183944a725adb4')

我很乐意做这件事:

from uuid import UUID
catalog.searchResults(UID=UUID('325dc47e-08f9-4fa1-bc18-3944a725adb4'))

这将等同于这个:

catalog.searchResults(UID=UUID('325dc47e08f94fa1bc183944a725adb4'))

有人知道我如何在Plone中实现与uuid表示无关的独立性吗?


嗯,你不能在其他地方有一个函数UUID吗?该函数检查字符串,根据需要插入或删除破折号,然后调用uuid.UUID?(令人惊讶的是,每次小更新Plone都会产生如此多的故障...) - Ulrich Schwarz
uuid.UUID实际上允许带或不带短划线的字符串。我在任何地方都会利用这个特性,通过确保我处理的字符串(主要用于JSON)始终具有可预测的格式来实现。问题是我必须使用两种表示法查询目录,以确保我获取对象。 - href_
解决方案是假设 (U)UID 是字符串 ID 并包含任何类型的字符吗?为什么要去掉破折号? - Mikko Ohtamaa
我正在去除破折号,因为plone.uuid从包含破折号的字符串变成了去除破折号的十六进制字符串。这留下了一个数据库,在使用基于字符串的目录UID搜索时必须考虑两种变体。 - href_
1个回答

4

您需要查询两种格式;一旦UID字段被分配,就不应该更改。因此,您的选项如下:

  1. Create a method that given a UID returns a tuple with both dashed and non-dashed versions, then use to query the catalog:

    def queryUID(UID):
        if '-' in UID:
            return (UID.replace('-', ''), UID)
        return (UID, '-'.join([
            UID[:8], UID[8:12], UID[12:16], UID[16:20], UID[20:]]))
    

    With that method in place the query simply becomes:

    catalog.searchResults(UID=queryUID('325dc47e-08f9-4fa1-bc18-3944a725adb4'))
    
  2. Make your database use dashed versions where the Plone UID has dashes, non-dashed versions where older Plone content still has a UID without dashes. In other words, treat the UID as a opaque string.

    The UIDs in Plone are not going to change in any case, so you won't have to search for the non-dashed version when a dashed version has been generated. Once a UID has been assigned to an object, it never changes, and never will gain the dashes.

  3. NOT recommended Iterate through your ZODB and replace all UIDs without dashes with the dashed equivalent. This most likely will break anything else linking by UID to those items.


我将选择方案一。我觉得我可以在索引上再进行一次额外查找。谢谢! - href_
这不是在索引上进行额外的查找;我们要求索引匹配两个值而不是一个。这非常高效。 :-) - Martijn Pieters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接