Re: adding metadata

Andy Altepeter <[email protected]>
Newsgroups gmane.comp.web.zope.silva.devel
Message-ID <[email protected]>
Hi jw and the rest,

On Monday 13 June 2005 10:39 am, Jan-Wijbrand Kolman wrote:
> Andy Altepeter wrote:
> > Is there a way to conditionally display a metadata set for a content type
> > (silva document)?
> >
> > What I want is to only display the set if the document is located in a
> > specific place.  Can I use the new 'category' property  in 1.3 to achieve
> > this?
>
> I don't think so. The 'category' property is really only useful to
> distinguish metadata sets for a particular use ('content' metadata, vs. for
> example 'layout settings' metadata).
>
> Conditionally showing a metadata set sounds rather difficult to me, in the
> current framework...
>
> Maybe, just maybe, you might be able to do something with the metadata
> 'access handlers'. There're registered in SilvaMetadata/Access.py. If you
> register another access handler that returns only the sets according to the
> conditions you'd like... maybe that's an idea?
>
This works perfectly!  I created an md access handler wich checks the 
content's physical path (via getPhysicalPath) to see if it is contained in a 
certain publication.  If it is not, then I remove my custom set from the 
list.

Access Handlers need to registered during startup; they aren't something that 
is persistent.  I registered the access handler in  my custom products 
__init__.py, in the initialize function.  Here's the code:

from Products.SilvaMetadata.Binding import MetadataBindAdapter
from Products.SilvaMetadata.Compatibility import getContentType
from Products.SilvaMetadata.Exceptions import BindingError
from Products.SilvaMetadata.Access import registerAccessHandler
def kb_metadata_access_handler(tool, content_type, content):
    type_mapping = tool.getTypeMapping()
    metadata_sets = type_mapping.getMetadataSetsFor(content_type)
    if 'kb' not in content.getPhysicalPath():
        metadata_sets = [ m for m in metadata_sets if m.id != 'its-kb' ]
    if not metadata_sets:
        raise BindingError("no metadata sets defined for %s" % content_type)
    return MetadataBindAdapter(content, metadata_sets).__of__(content)

def initialize(context):
  #...
  #add a metadata register access handler.  This function (defined above) is 
called
  #whenever the metadata sets for SDV are retrieved.  This one filters would 
the its-kb
  #set if the version isn't within the knowledgebase container.
  registerAccessHandler('Silva Document Version', kb_metadata_access_handler)

Thanks jw for the suggestion!
Andy
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.