Skip to content

Commit

Permalink
Merge pull request #34 from sgeulette/master
Browse files Browse the repository at this point in the history
When displaying differences on a zope.schema.List field, vocabulary terms titles are displayed (if a vocabulary is linked to the field)
  • Loading branch information
jensens authored Oct 2, 2018
2 parents 581b699 + 7221805 commit b683b12
Show file tree
Hide file tree
Showing 24 changed files with 236 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*.py[co]
build
dist
include/
lib/
*.mo
.installed.cfg
bin/
Expand Down
14 changes: 4 additions & 10 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ Changelog
3.2.2 (unreleased)
------------------

Breaking changes:

- *add item here*

New features:

- *add item here*

Bug fixes:

- *add item here*

- Display titles in diff of zope.schema.List using vocabulary
[sgeulette]
- Flake8 corrections
[sgeulette]

3.2.1 (2018-09-23)
------------------
Expand Down
1 change: 1 addition & 0 deletions Products/CMFDiffTool/ATCompoundDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ def getFields(self, obj1, obj2):
'schemata': schemata_name})
return fields


InitializeClass(ATCompoundDiff)
13 changes: 7 additions & 6 deletions Products/CMFDiffTool/BaseDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Calculate differences between content objects
"""

from Acquisition import aq_base
from AccessControl.class_init import InitializeClass
from Acquisition import aq_base
from plone.dexterity.interfaces import IDexterityContent
from Products.CMFDiffTool import CMFDiffToolMessageFactory as _
from Products.CMFDiffTool.interfaces import IDifference
Expand Down Expand Up @@ -63,7 +63,7 @@ def applyChanges(self, ob):
def filenameTitle(self, filename):
"""Translate the filename leading text
"""
msg = _(u"Filename: ${filename}",
msg = _(u'Filename: ${filename}',
mapping={'filename': filename})
return translate(msg)

Expand All @@ -73,8 +73,8 @@ def _getValue(ob, field, field_name, convert_to_str=True):
# grab it *with* acquisition, so things like ComputedAttribute
# will work
if IDexterityContent.providedBy(ob) and field:
# we need a special handling for subjects because the field is stored as
# `subject` attribute but the schema name is `subjects`
# we need a special handling for subjects because the field is stored
# as `subject` attribute but the schema name is `subjects`
# see plone.app.dexterity.behaviors.metadata.ICategorization and
# plone.dexterity.content.DexterityContent
if field == 'subjects':
Expand Down Expand Up @@ -105,7 +105,7 @@ def _getValue(ob, field, field_name, convert_to_str=True):
if isinstance(val, RelationValue):
try:
obj = val.to_object
except:
except Exception:
obj = None
new_value.append(obj)
else:
Expand All @@ -117,7 +117,7 @@ def _getValue(ob, field, field_name, convert_to_str=True):
if isinstance(value, RelationValue):
try:
obj = value.to_object
except:
except Exception:
obj = None
value = obj

Expand All @@ -131,4 +131,5 @@ def _getValue(ob, field, field_name, convert_to_str=True):

return value


InitializeClass(BaseDiff)
3 changes: 2 additions & 1 deletion Products/CMFDiffTool/BinaryDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def inline_diff(self):
html.append(
self.inlinediff_fmt % (css_class,
self.filenameTitle(self.oldFilename),
self.filenameTitle(self.newFilename))
self.filenameTitle(self.newFilename)),
)

if html:
return linesep.join(html)


InitializeClass(BinaryDiff)
1 change: 1 addition & 0 deletions Products/CMFDiffTool/CMFDTHtmlDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ def _parseField(self, value, filename=None):
value = getattr(value, 'raw', value)
return TextDiff._parseField(self, value, filename)


InitializeClass(CMFDTHtmlDiff)
3 changes: 2 additions & 1 deletion Products/CMFDiffTool/CMFDiffTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Calculate differences between content objects
"""
from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from AccessControl.class_init import InitializeClass
from Acquisition import aq_base
from OFS.SimpleItem import SimpleItem
from Products.CMFCore.permissions import ManagePortal
from Products.CMFCore.utils import registerToolInterface
Expand Down Expand Up @@ -177,5 +177,6 @@ def unregisterDiffType(klass):

del CMFDiffTool._difftypes[klass.meta_type]


InitializeClass(CMFDiffTool)
registerToolInterface('portal_diff', IDiffTool)
2 changes: 1 addition & 1 deletion Products/CMFDiffTool/FieldDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _parseField(self, value, filename=None):
else:
return [
self.filenameTitle(filename),
value
value,
]

def getLineDiffs(self):
Expand Down
70 changes: 48 additions & 22 deletions Products/CMFDiffTool/ListDiff.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
# -*- coding: utf-8 -*-
from AccessControl.class_init import InitializeClass
from plone.dexterity.interfaces import IDexterityContent
from Products.CMFDiffTool.choicediff import get_field_object
from Products.CMFDiffTool.choicediff import title_or_value
from Products.CMFDiffTool.FieldDiff import FieldDiff
from six.moves import range


def chk_hashable(value):
try:
hash(value)
except TypeError as e:
value = repr(e) + ": " + repr(value)
return value


class ListDiff(FieldDiff):
"""Text difference"""

meta_type = 'List Diff'

def __init__(self, obj1, obj2, field, id1=None, id2=None, field_name=None,
field_label=None, schemata=None):
FieldDiff.__init__(self, obj1, obj2, field, id1, id2, field_name,
field_label, schemata)
self._vocabulary = None

# Tries to find a vocabulary. First we need to find an object and
# the field instance.
obj = obj1 if (obj1 is not None) else obj2
field_name = field_name or field
if obj and field_name and IDexterityContent.providedBy(obj):
field_instance = get_field_object(obj, field_name)
if field_instance is not None:
# Binding the field to an object will construct the vocabulary
# using a factory if necessary.
try:
self._vocabulary = field_instance.value_type.bind(obj).\
vocabulary
except Exception:
pass

def chk_hashable(self, value):
if self._vocabulary is not None:
value = title_or_value(self._vocabulary, value)
try:
hash(value)
except TypeError as e:
value = repr(e) + ': ' + repr(value)
return value

def _parseField(self, value, filename=None):
"""Parse a field value in preparation for diffing"""
if type(value) is list or type(value) is tuple:
values = []
for v in value:
values.append(chk_hashable(v))
values.append(self.chk_hashable(v))
return values
else:
if type(value) is set:
return list(value)
else:
return [chk_hashable(value)]
return [self.chk_hashable(value)]


class RelationListDiff(FieldDiff):
Expand All @@ -41,7 +66,8 @@ class RelationListDiff(FieldDiff):
</div>"""

def _parseField(self, value, filename=None):
"""Take RelationValues and just return the target UID so we can compare"""
""" Take RelationValues and just return the target UID
so we can compare """

if filename is None:
# Since we only want to compare a single field, make a
Expand All @@ -50,7 +76,7 @@ def _parseField(self, value, filename=None):
else:
return [
self.filenameTitle(filename),
['/'.join(val.getPhysicalPath()) for val in value]
['/'.join(val.getPhysicalPath()) for val in value],
]

def inline_diff(self):
Expand All @@ -65,27 +91,27 @@ def inline_diff(self):
obj_title = obj.Title()
obj_url = obj.absolute_url()
r.append(inlinediff_fmt %
(css_class, "diff_sub", obj_url, obj_title))
(css_class, 'diff_sub', obj_url, obj_title))
for i in range(blo, bhi):
obj = self.newValue[i]
obj_title = obj.Title()
obj_url = obj.absolute_url()
r.append(inlinediff_fmt %
(css_class, "diff_add", obj_url, obj_title))
(css_class, 'diff_add', obj_url, obj_title))
elif tag == 'delete':
for i in range(alo, ahi):
obj = self.oldValue[i]
obj_title = obj.Title()
obj_url = obj.absolute_url()
r.append(inlinediff_fmt %
(css_class, "diff_sub", obj_url, obj_title))
(css_class, 'diff_sub', obj_url, obj_title))
elif tag == 'insert':
for i in range(blo, bhi):
obj = self.newValue[i]
obj_title = obj.Title()
obj_url = obj.absolute_url()
r.append(inlinediff_fmt %
(css_class, "diff_add", obj_url, obj_title))
(css_class, 'diff_add', obj_url, obj_title))
elif tag == 'equal':
for i in range(alo, ahi):
obj = self.oldValue[i]
Expand All @@ -97,33 +123,33 @@ def inline_diff(self):
return '\n'.join(r)

def ndiff(self):
"""Return a textual diff"""
""" Return a textual diff """
r = []
for tag, alo, ahi, blo, bhi in self.getLineDiffs():
if tag == 'replace':
for i in range(alo, ahi):
obj = self.oldValue[i]
obj_url = obj.absolute_url()
r.append("- %s" % obj_url)
r.append('- %s' % obj_url)
for i in range(blo, bhi):
obj = self.newValue[i]
obj_url = obj.absolute_url()
r.append("+ %s" % obj_url)
r.append('+ %s' % obj_url)
elif tag == 'delete':
for i in range(alo, ahi):
obj = self.oldValue[i]
obj_url = obj.absolute_url()
r.append("- %s" % obj_url)
r.append('- %s' % obj_url)
elif tag == 'insert':
for i in range(blo, bhi):
obj = self.newValue[i]
obj_url = obj.absolute_url()
r.append("+ %s" % obj_url)
r.append('+ %s' % obj_url)
elif tag == 'equal':
for i in range(alo, ahi):
obj = self.oldValue[i]
obj_url = obj.absolute_url()
r.append(" %s" % obj_url)
r.append(' %s' % obj_url)
else:
raise ValueError('unknown tag %r', tag)
return '\n'.join(r)
Expand Down
6 changes: 4 additions & 2 deletions Products/CMFDiffTool/TextDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ def inline_diff(self):
if old_fname != new_fname:
html.append(
self.inlinediff_fmt % ('%s FilenameDiff' % css_class,
old_fname, new_fname)
old_fname, new_fname),
)
if a != b:
html.append(
self.inlinediff_fmt % (css_class, a, b)
self.inlinediff_fmt % (css_class, a, b),
)
if html:
return linesep.join(html)


InitializeClass(TextDiff)


Expand All @@ -118,4 +119,5 @@ def _parseField(self, value, filename=None):

return TextDiff._parseField(self, safe_unicode(value), filename)


InitializeClass(AsTextDiff)
2 changes: 1 addition & 1 deletion Products/CMFDiffTool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
def initialize(context):
ToolInit('CMF Diff Tool',
tools=tools,
icon='tool.gif'
icon='tool.gif',
).initialize(context)
1 change: 1 addition & 0 deletions Products/CMFDiffTool/choicediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ def _parseField(self, value, filename=None):

return AsTextDiff._parseField(self, value, filename)


InitializeClass(ChoiceDiff)
5 changes: 3 additions & 2 deletions Products/CMFDiffTool/dexteritydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _diff_field(self, obj1, obj2, field, schema_name):
id2=self.id2,
field_name=field.getName(),
field_label=field.title,
schemata=schema_name
schemata=schema_name,
)

def _get_diff_type(self, field):
Expand Down Expand Up @@ -176,6 +176,7 @@ def _compute_fields_order(self, obj):
all_fields += [(form.fields[name].field, name) for name in form.fields]
if form.groups:
for group in form.groups:
all_fields += [(group.fields[name].field, name) for name in group.fields]
all_fields += [(group.fields[name].field, name)
for name in group.fields]

return all_fields
6 changes: 4 additions & 2 deletions Products/CMFDiffTool/namedfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, obj1, obj2, field, id1=None, id2=None, field_name=None,
def _parseField(self, value, filename=None):
return [
'' if (value is None)
else named_file_as_str(NamedFile(data=value, filename=filename))
else named_file_as_str(NamedFile(data=value, filename=filename)),
]

def inline_diff(self):
Expand All @@ -72,6 +72,7 @@ def inline_diff(self):

return '' if self.same else self.inlinediff_fmt % (css_class, old, new)


InitializeClass(NamedFileBinaryDiff)


Expand Down Expand Up @@ -131,7 +132,7 @@ def inline_diff(self):

def is_same_dict(d1, d2):
return is_same(
d1['data'], d1['filename'], d2['data'], d2['filename']
d1['data'], d1['filename'], d2['data'], d2['filename'],
)

return '\n'.join([
Expand All @@ -140,4 +141,5 @@ def is_same_dict(d1, d2):
% (css_class, d_old['repr'], d_new['repr'])
) for (d_old, d_new) in zip(old_data, new_data)])


InitializeClass(NamedFileListDiff)
Loading

0 comments on commit b683b12

Please sign in to comment.