[Zope-CMF] Content Types
Jeffrey P Shell
jeffrey@cuemedia.com
Fri, 09 Aug 2002 12:45:06 -0600
On 8/9/02 11:30 AM, "Jim Pharis" <binarybrain@phreaker.net> wrote:
> I am looking for some way to uniquely identify each instance of a
> content object (document). I had a few ideas for how to do this but
> haven't managed to get any working.
>
> Is there a class variable for the document content object that I can add
> an ID property to? Each time I create a new document instance I want to
> increment that class variable.
>
> I am also opened to other suggestions for uniquely identifying each
> document in a portal.
>
> Thanks, Jim Pharis
Are you asking how to track unique identifiers across a Zope/CMF site?
Every object has a unique id per folder - the one given to it when you add a
new object (Document, news item, whatever). Since there can be only one
object with a given id per folder, the entire path to the object becomes its
unique identifier - there can only be one /foo/bar/baz.
If you're asking about how to automatically generate unique ID's, there are
some options:
One is to create some sort of unique identifier tool that spits out ugly but
pretty-much guaranteed unique identifiers. You'd then have to hook into the
instance creation machinery. In a recent project, I had a subclass of
PortalFolder (actually, subclassed from Skinned Folder) which I set up to be
the primary Folder type for my site. One of the things I added was a hook
to auto-gen an ID if one wasn't made.
security.declareProtected(AddPortalContent, 'invokeFactory')
def invokeFactory(self, type_name, id=None, RESPONSE=None, *args, **kw):
'''
Invokes the portal_types tool -- overridden so that we can use
the UIDGen tool
'''
if id is None:
uidgen = getToolByName(self, 'cm_uid_tool')
id = uidgen.get()
pt = getToolByName(self, 'portal_types')
apply(pt.constructContent, (type_name, self, id, RESPONSE) + args, kw)
('cm_uid_tool' is the code of our own creation).
Another option is to hook some stuff in to Scriptable Type objects. I also
did this on a recent project, not to stamp out unique id's, but to create a
'FrontPage' document that was pre-populated with content and always had the
name 'FrontPage' (the folders 'View' action would try to find one of these
FrontPage objects first before looking for either 'index.html' or
'index_html'). In the script used to generate the document, the first lines
were like this:
# Get the FactoryDispatcher
product = context.manage_addProduct['Other']
id = 'FrontPage'
product.addOtherDocument(id)
The line that says 'id=...' could be replaced with a call to a script to
automatically generate an id - either uniquely per folder or for the whole
site.
--
Jeffrey P Shell
www.cuemedia.com