Back to index

plone3  3.1.7
BaseDiff.py
Go to the documentation of this file.
00001 """CMFDiffTool.py
00002 
00003    Calculate differences between content objects
00004 """
00005 
00006 from zope.interface import implements
00007 
00008 import Acquisition
00009 from Acquisition import aq_base
00010 from Globals import InitializeClass
00011 from Products.CMFDiffTool.interfaces.portal_diff import IDifference as IDifferenceZ2
00012 from Products.CMFDiffTool.interfaces import IDifference
00013 
00014 class BaseDiff:
00015     """Basic diff type"""
00016 
00017     __implements__ = (IDifferenceZ2,)
00018     implements(IDifference)
00019     __allow_access_to_unprotected_subobjects__ = 1
00020     meta_type = "Base Diff"
00021     
00022     def __init__(self, obj1, obj2, field, id1=None, id2=None,
00023                  field_label=None,schemata=None):
00024         self.field = field
00025         self.oldValue = _getValue(obj1, field)
00026         self.newValue = _getValue(obj2, field)
00027         self.same = (self.oldValue == self.newValue)
00028         if not id1 and hasattr(obj1, 'getId'):
00029             id1 = obj1.getId()
00030         if not id2 and hasattr(obj2, 'getId'):
00031             id2 = obj2.getId()
00032         self.id1 = id1
00033         self.id2 = id2
00034         self.label = field_label or field
00035         self.schemata = schemata or 'default'
00036 
00037     def testChanges(self, ob):
00038         """Test the specified object to determine if the change set will apply without errors"""
00039         pass
00040 
00041     def applyChanges(self, ob):
00042         """Update the specified object with the difference"""
00043         pass
00044     
00045 
00046 def _getValue(ob, field):
00047     # Check for the attribute without acquisition.  If it's there,
00048     # grab it *with* acquisition, so things like ComputedAttribute
00049     # will work
00050     if hasattr(aq_base(ob), field):
00051         value = getattr(ob, field)
00052     else:
00053         raise AttributeError, field
00054 
00055     # Handle case where the field is a method
00056     try:
00057         value = value()
00058     except (AttributeError, TypeError):
00059         pass
00060 
00061     # If this is some object, convert it to a string
00062     try:
00063         if isinstance(value, Acquisition.Implicit):
00064             value = str(value)
00065     except TypeError:
00066         pass
00067 
00068     return value
00069 
00070 InitializeClass(BaseDiff)