Back to index

plone3  3.1.7
ATCompoundDiff.py
Go to the documentation of this file.
00001 from Globals import InitializeClass
00002 from Products.CMFDiffTool.interfaces.portal_diff import IDifference
00003 from Products.CMFDiffTool.TextDiff import TextDiff
00004 from Products.CMFDiffTool.FieldDiff import FieldDiff
00005 from Products.CMFDiffTool.BinaryDiff import BinaryDiff
00006 from Products.CMFDiffTool.ListDiff import ListDiff
00007 from Products.CMFDiffTool.CMFDTHtmlDiff import CMFDTHtmlDiff
00008 
00009 
00010 AT_FIELD_MAPPING = {'text': 'variable_text',
00011                     'string': FieldDiff,
00012                     'datetime': FieldDiff,
00013                     'file': 'variable_binary',
00014                     'image': BinaryDiff,
00015                     'lines': ListDiff,
00016                     'integer': FieldDiff,
00017                     'float': FieldDiff,
00018                     'fixedpoint': FieldDiff,
00019                     'boolean': FieldDiff,
00020                     'reference': 'raw:ListDiff'}
00021 
00022 AT_EXCLUDED_FIELDS = ('modification_date',)
00023 
00024 
00025 class ATCompoundDiff:
00026     """Text difference"""
00027 
00028     meta_type = "Compound Diff for AT types"
00029 
00030     def __init__(self, obj1, obj2, field, id1=None, id2=None):
00031         if not id1:
00032             id1 = obj1.getId()
00033         if not id2:
00034             id2 = obj2.getId()
00035         self.id1 = id1
00036         self.id2 = id2
00037 
00038         fields = self.getFields(obj1, obj2)
00039         self._diffs = self.generateSubDiffs(fields, obj1, obj2)
00040 
00041     def __getitem__(self, index):
00042         return self._diffs[index]
00043 
00044     def generateSubDiffs(self, fields, obj1, obj2):
00045         diff_list = []
00046         for field in fields:
00047             klass = field['klass']
00048             diff = klass(obj1, obj2, field['accessor'], id1=self.id1,
00049                               id2=self.id2, field_label=field['label'],
00050                               schemata=field['schemata'])
00051             diff_list.append(diff)
00052         return diff_list
00053 
00054     def getFields(self, obj1, obj2):
00055         fields = []
00056         # Make sure we get the fields ordered by schemata, as in the edit view
00057         schematas = obj1.Schemata()
00058         schemata_names = schematas.keys()
00059 
00060         # Put default first and metadata last
00061         if 'default' in schemata_names and schemata_names[0] != 'default':
00062             schemata_names.remove('default')
00063             schemata_names.insert(0,'default')
00064         if 'metadata' in schemata_names and schemata_names[-1] != 'metadata':
00065             schemata_names.remove('metadata')
00066             schemata_names.insert(-1,'metadata')
00067 
00068         for schemata_name in schemata_names:
00069             schema = schematas[schemata_name]
00070             for field in schema.viewableFields(obj1):
00071                 if (AT_FIELD_MAPPING.has_key(field.type) and 
00072                         field.getName() not in AT_EXCLUDED_FIELDS):
00073                     is_primary = getattr(field, 'primary' , False)
00074                     label = field.widget.Label(obj1)
00075                     diff_type = AT_FIELD_MAPPING[field.type]
00076                     if IDifference.isImplementedByInstancesOf(diff_type):
00077                         fields.append({'name':field.getName(),
00078                                     'accessor':field.accessor,
00079                                     'klass':diff_type,
00080                                     'primary':is_primary,
00081                                     'label':label,
00082                                     'schemata':schemata_name})
00083                     elif 'raw' in diff_type:
00084                         #Handle Fields which diff against the edit accessor
00085                         diff_name = diff_type.split(':')[1]
00086                         diff_type = globals()[diff_name]
00087                         fields.append({'name':field.getName(),
00088                                     'accessor':field.edit_accessor,
00089                                     'klass':diff_type,
00090                                     'primary':is_primary,
00091                                     'label':label,
00092                                     'schemata':schemata_name})
00093                     elif diff_type == 'variable_binary':
00094                         diff_type = BinaryDiff
00095                         if 'text/' in field.getContentType(obj1) and \
00096                         'text/' in \
00097                             obj2.getField(field.getName()).getContentType(obj2):
00098                             diff_type = TextDiff
00099                         fields.append({'name':field.getName(),
00100                                     'accessor':field.accessor,
00101                                     'klass':diff_type,
00102                                     'primary':is_primary,
00103                                     'label':label,
00104                                     'schemata':schemata_name})
00105                     elif diff_type == 'variable_text':
00106                         diff_type = TextDiff
00107                         if 'html' in field.getContentType(obj1) and \
00108                         'html' in \
00109                             obj2.getField(field.getName()).getContentType(obj2):
00110                             diff_type = CMFDTHtmlDiff
00111                         fields.append({'name':field.getName(),
00112                                     'accessor':field.accessor,
00113                                     'klass':diff_type,
00114                                     'primary':is_primary,
00115                                     'label':label,
00116                                     'schemata':schemata_name})
00117         return fields
00118 
00119 InitializeClass(ATCompoundDiff)