Back to index

plone3  3.1.7
addtoFavorites.py
Go to the documentation of this file.
00001 ## Script (Python) "addtoFavorites"
00002 ##title=Add item to favourites (Plone Version)
00003 ##bind container=container
00004 ##bind context=context
00005 ##bind namespace=
00006 ##bind script=script
00007 ##bind subpath=traverse_subpath
00008 ##parameters=
00009 
00010 from Products.CMFPlone.utils import base_hasattr
00011 from Products.CMFPlone import PloneMessageFactory as _
00012 
00013 RESPONSE = context.REQUEST.RESPONSE
00014 homeFolder=context.portal_membership.getHomeFolder()
00015 state = context.restrictedTraverse("@@plone_context_state")
00016 view_url = '%s/%s' % (context.absolute_url(), state.view_template_id())
00017 if not homeFolder:
00018     context.plone_utils.addPortalMessage(_(u'Can\'t access home folder. Favorite is not added.'), 'error')
00019     return RESPONSE.redirect(view_url)
00020 
00021 if not base_hasattr(homeFolder, 'Favorites'):
00022     homeFolder.invokeFactory('Folder', id='Favorites', title='Favorites')
00023     addable_types = ['Favorite']
00024     favs = homeFolder.Favorites
00025     if base_hasattr(favs, 'setConstrainTypesMode'):
00026         favs.setConstrainTypesMode(1)
00027         favs.setImmediatelyAddableTypes(addable_types)
00028         favs.setLocallyAllowedTypes(addable_types)
00029 
00030 targetFolder = homeFolder.Favorites
00031 new_id='fav_' + str(int( context.ZopeTime()))
00032 myPath=context.portal_url.getRelativeUrl(context)
00033 fav_id = targetFolder.invokeFactory('Favorite', id=new_id, title=context.TitleOrId(), remote_url=myPath)
00034 if fav_id:
00035     favorite = getattr(targetFolder, fav_id, None)
00036 else:
00037     favorite = getattr(targetFolder, new_id, None)
00038 
00039 if favorite:
00040     favorite.reindexObject()
00041     msg = _(u'${title} has been added to your Favorites.',
00042             mapping={u'title' : context.title_or_id()})
00043     context.plone_utils.addPortalMessage(msg)
00044 else:
00045     msg = _(u'There was a problem adding ${title} to your Favorites.',
00046             mapping={u'title' : context.title_or_id()})
00047 
00048 return RESPONSE.redirect(view_url)