Wednesday, April 14, 2010

Change creator programmatically

In order to change an object's Creator from a script or another piece of code, use setCreators(new_creator,). Mark the comma: apparently, the method takes a list as input.

I used this in a view class which mass-changes ownership:
    def changeOwnership(self, obj, userid):
""" Change ownership of obj to userid """
membership = getToolByName(self.context, 'portal_membership')
user = membership.getMemberById(userid)
obj.setCreators(userid,)
obj.changeOwnership(user)
roles = list(obj.get_local_roles_for_userid(userid))
if 'Owner' not in roles:
roles.append('Owner')
obj.manage_setLocalRoles(userid, roles)
obj.reindexObject()
return


I had a look at the internals of plone.app.changeownership to find out what i had to do in order to make this work.

Friday, April 9, 2010

LaTeX: Including .sty files from the command line


$ export $TEXINPUTS='.:/home/kees/custom_latex_templates/:'
$ latex /path/to/file_that_uses_custom_latex_template.tex

Friday, April 2, 2010

Adding a ZCTextIndex from setuphandlers.py

Alternative title: Indexing an object's creator's full name on Plone 4 with a dexterity content type.

Adding a catalog index in profiles/default/catalog.xml is deprecated; see http://maurits.vanrees.org/weblog/archive/2009/12/catalog.

I had some trouble getting this to work with a ZCTextIndex. I have a content type for which i want to be able to make the creator's full username searchable. In order to do this, i had to create a ZCTextIndex, as a simple FieldIndex didn't seem to work.

I got it to work by replacing the loop inside for name, meta_type in wanted: with this:
   if name not in indexes:
if meta_type == 'ZCTextIndex':
class Empty: pass
title_extras = Empty()
#title_extras.doc_attr = 'index attribute?'
title_extras.index_type = 'Okapi BM25 Rank'
title_extras.lexicon_id = 'plone_lexicon'
catalog.addIndex(name, meta_type, title_extras)
else:
catalog.addIndex(name, meta_type)
indexables.append(name)
logger.info("Added %s for field %s.", meta_type, name)


I'm using a dexterity-based content-type, the code contains:
@indexer(IVideo)
def makerFullnameIndexer(obj):
creator = obj.Creator()
fullname = creator
membership = getToolByName(obj, 'portal_membership')
member = membership.getMemberById(creator)
if member:
fullname = '%s %s' % (member.getProperty('firstname'),
member.getProperty('lastname'))
return fullname
grok.global_adapter(makerFullnameIndexer, name="MakerFullname")