Back to index

plone3  3.1.7
fields_versioning.py
Go to the documentation of this file.
00001 
00002 from zope.component import adapter
00003 from kss.core.interfaces import IKSSView
00004 from interfaces import IVersionedFieldModifiedEvent
00005 
00006 from Products.CMFPlone.utils import base_hasattr
00007 from Products.CMFCore.utils import getToolByName
00008 from Products.CMFPlone import PloneMessageFactory as _
00009 from Products.CMFEditions.interfaces.IArchivist import ArchivistUnregisteredError
00010 from Products.CMFEditions.interfaces.IModifier import FileTooLargeToVersionError
00011 
00012 # --
00013 # As a temporary solution, we start a new version after a field is 
00014 # instant edited. This is done similarly as on the formcontroller hook. 
00015 # This hook, Products/CMFEditions/skins/update_version_on_edit.py, is
00016 # set up from  Products/CMFEditions/VersionPolicies.py.
00017 # --
00018 
00019 @adapter(None, IKSSView, IVersionedFieldModifiedEvent)
00020 def versionObjectBecauseFieldChanged(obj, view, event):
00021     # XXX I believe we don't need to check if CMFEditions has actually
00022     # set the hook or not?
00023 
00024     pr = getToolByName(view.context, 'portal_repository')
00025     isVersionable = pr.isVersionable(obj)
00026     
00027     changed = False
00028     if not base_hasattr(obj, 'version_id'):
00029         changed = True
00030     else:
00031         try:
00032             changed = not pr.isUpToDate(obj, obj.version_id)
00033         except ArchivistUnregisteredError:
00034             # XXX: The object is not actually registered, but a
00035             # version is set, perhaps it was imported, or versioning
00036             # info was inappropriately destroyed
00037             changed = True
00038 
00039     fieldnames = event.fieldnames
00040     txtfieldnames ='"%s"' % ('", "'.join(fieldnames), )
00041     # XXX I am not sure we actually want to translate _here_, but 
00042     # at the moment this seems to be the best policy.
00043     comment_msg = _('Instant edited field(s) ${fields}' , 
00044                     mapping=dict(fields=txtfieldnames))
00045     # XXX unicode is not possible here?
00046     comment = view.context.translate(comment_msg)
00047     comment = comment.encode('utf')
00048 
00049     if changed and comment is not None and \
00050            pr.supportsPolicy(obj, 'at_edit_autoversion') and isVersionable:
00051         try:
00052             pr.save(obj=obj, comment=comment)
00053         except FileTooLargeToVersionError:
00054             commands = view.getCommandSet('plone')
00055             commands.issuePortalMessage(
00056                 _("Changes Saved. Versioning for this file has been disabled "
00057                   "because it is too large."),
00058                 msgtype="warn")