Exemple #1
0
 def update(self):
     """ Checks if we have Modified errors and renders split view 
         with display widgets for db values and normal for input 
     """
     super(DiffEditForm, self).update()
     for error in self.errors:
         if error.__class__ == Modified:
             # Set flag that form is in diff mode
             if not self.diff:
                 self.diff=True
             
     if self.diff:
         # Set last timestamp which we store in hidden field because
         # document may be modified during diff and from.timestamp field
         # contains old value
         self.last_timestamp = self.context.timestamp
         for widget in self.widgets:
             try:
                 value = widget.getInputValue()
             except:
                 value = ""
             
             # Get widget's field    
             form_field = self.form_fields.get(widget.context.__name__)                    
             field = form_field.field.bind(self.context)
             
             # Form display widget for our field 
             if form_field.custom_widget is not None:
                 display_widget = form_field.custom_widget(
                     field, self.request)                 
             else:
                 display_widget = component.getMultiAdapter(
                     (field, self.request), IDisplayWidget)
             
             # If field is Text or TextLine we display HTML diff
             if IText.providedBy(field) or ITextLine.providedBy(field):
                 if value:
                     diff_val = textDiff(field.get(self.context), value)
                 else:
                     diff_val = ""
                 display_widget = component.getMultiAdapter(
                     (field, self.request), IDiffDisplayWidget)
                 display_widget.setRenderedValue(diff_val)
             else:
                 display_widget.setRenderedValue(field.get(self.context))
             display_widget.name = widget.name + ".diff.display"
             # Add display - input widgets pair to list of diff widgets
             self.diff_widgets.append((widget, display_widget))
Exemple #2
0
    def update(self):
        """ Checks if we have Modified errors and renders split view 
            with display widgets for db values and normal for input 
        """
        super(DiffEditForm, self).update()
        for error in self.errors:
            if error.__class__ == Modified:
                # Set flag that form is in diff mode
                if not self.diff:
                    self.diff = True

        if self.diff:
            # Set last timestamp which we store in hidden field because
            # document may be modified during diff and from.timestamp field
            # contains old value
            self.last_timestamp = self.context.timestamp
            for widget in self.widgets:
                try:
                    value = widget.getInputValue()
                except:
                    value = ""

                # Get widget's field
                form_field = self.form_fields.get(widget.context.__name__)
                field = form_field.field.bind(self.context)

                # Form display widget for our field
                if form_field.custom_widget is not None:
                    display_widget = form_field.custom_widget(
                        field, self.request)
                else:
                    display_widget = component.getMultiAdapter(
                        (field, self.request), IDisplayWidget)

                # If field is Text or TextLine we display HTML diff
                if IText.providedBy(field) or ITextLine.providedBy(field):
                    if value:
                        diff_val = textDiff(field.get(self.context), value)
                    else:
                        diff_val = ""
                    display_widget = component.getMultiAdapter(
                        (field, self.request), IDiffDisplayWidget)
                    display_widget.setRenderedValue(diff_val)
                else:
                    display_widget.setRenderedValue(field.get(self.context))
                display_widget.name = widget.name + ".diff.display"
                # Add display - input widgets pair to list of diff widgets
                self.diff_widgets.append((widget, display_widget))
def diff(source, target, *interfaces):
    """Get a list of (field, changed, result) 3-tuples, for "diff-able" fields.
    """
    if not len(interfaces):
        interfaces = interface.providedBy(source)
    results = []
    for iface in interfaces:
        # the order is locked on the order returned by of interface.names()
        for name in iface.names():
            field = iface[name]
            # only consider for diffing fields of this type
            if not isinstance(field, (schema.Text, schema.TextLine, schema.Set)):
                continue
            bound = field.bind(source)
            source_value = bound.query(source, field.default)
            target_value = bound.query(target, field.default)
            if source_value is None or target_value is None:
                continue
            hresult = textDiff(source_value, target_value)
            results.append((field, bool(hresult!=source_value), hresult))
    return results
Exemple #4
0
def diff(source, target, *interfaces):
    """Get a list of (field, changed, result) 3-tuples, for "diff-able" fields.
    """
    if not len(interfaces):
        interfaces = interface.providedBy(source)
    results = []
    for iface in interfaces:
        # the order is locked on the order returned by of interface.names()
        for name in iface.names():
            field = iface[name]
            # only consider for diffing fields of this type
            if not isinstance(field, (schema.Text, schema.TextLine, schema.Set)):
                continue
            bound = field.bind(source)
            source_value = bound.query(source, field.default)
            target_value = bound.query(target, field.default)
            if source_value is None or target_value is None:
                continue
            hresult = textDiff(source_value, target_value)
            results.append((field, bool(hresult!=source_value), hresult))
    return results