href_
href_

Reputation: 1589

Querying portal_catalog using typed uuids instead of string uuids

I'm writing a Plone calendar module for reserving different resources. This module features a calendar whose events are stored in Postgresql. Each calendar is a Dexterity object stored in the ZODB.

To make a link between Plone and Postgresql I naturally turned to the uuid property of the Plone objects. So each uuid in Plone serves as a foreign key in Postgresql, which supports uuid natively.

This worked well for me in the past 6 months until I started using Plone 4.1.4. With it, plone.uuid 1.0.2 was introduced which changes the string representation of uuids from uuids with to uuids without dashes.

The problem with this change is that I can no longer be certain which representation is going to be used on any given object. Objects created before Plone 4.1.4 contain a differently formatted uuid-string than objects created after.

Long story short, to ensure that my code works with any uuid representation I would love to be able to search by using Python's uuid type.

So instead of this:

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

Which returns different results than this:

catalog.searchResults(UID='325dc47e08f94fa1bc183944a725adb4')

I would love to do this:

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

Which would be equivalent to this:

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

Does anyone know how I can achieve that kind of independency from the uuid's representation in Plone?

Upvotes: 3

Views: 698

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123450

You would have to query for both formats; once the UID field has been assigned it should not be changed, really. Your options are thus:

  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.

Upvotes: 4

Related Questions