Beispiel #1
0
    def get_diff(fieldlist1, fieldlist2):
        reordered = False
        fields1 = []
        fields2 = []
        for index in fieldlist1.form.raw_data.getall(fieldlist1.indices_input_name):
            fields1.append(fieldlist1.field(name=index))
        for index in fieldlist2.form.raw_data.getall(fieldlist2.indices_input_name):
            fields2.append(fieldlist2.field(name=index))

        diffs = []
        while fields1:
            field2 = None
            field1 = fields1.pop(0)
            if fields2 and fieldlist1._is_pair(field1, fields2[0]):
                field2 = fields2.pop(0)

            if field1.widget.render_type == "hidden":
                continue
            if field2 is not None:
                if hasattr(field1, 'get_diff'):
                    diff = field1.get_diff(field2)
                    if diff is not None:
                        diffs.append(diff)
                elif hasattr(field1.widget, 'get_diff'):
                    diff = field1.widget.get_diff(field1, field2)
                    if diff is not None:
                        diffs.append(diff)
                else:
                    data1 = _get_field_data(fieldlist1, field1)
                    data2 = _get_field_data(fieldlist2, field2)
                    if data1 != data2:
                        diff = make_diff(field1, field2,
                                        changed=True)
                        diffs.append(diff)
            else:
                diff = make_diff(field1, None,
                                 changed=False)
                diffs.append(diff)
                reordered = True

        while fields2:
            field2 = fields2.pop(0)
            diff = make_diff(None, field2,
                            changed=True)
            diffs.append(diff)

        if not reordered:
            diffs = [x for x in diffs if x['changed'] or x]
        if diffs:
            return dict(label=fieldlist1.label or '',
                        name=fieldlist1.input_name,
                        before=lambda: '',
                        after=lambda: '',
                        children=diffs,
                        changed=True)
Beispiel #2
0
 def get_diff(form1, form2):
     # diff for edit log
     fields = sum([x.field_names for x in form1.fields], [])
     diffs = []
     for field_name in fields:
         if not field_name:
             continue
         field1 = form1.get_field(field_name)
         field2 = form2.get_field(field_name)
         if field2.widget.render_type == "hidden":
             continue
         if hasattr(field1, 'get_diff'):
             child_diff = field1.get_diff(field2)
             if child_diff is not None:
                 diffs.append(child_diff)
         elif hasattr(field1.widget, 'get_diff'):
             child_diff = field1.widget.get_diff(field1, field2)
             if child_diff is not None:
                 diffs.append(child_diff)
         else:
             data1 = _get_field_data(form1, field1)
             data2 = _get_field_data(form2, field2)
             if data1 != data2:
                 diff = make_diff(field1, field2,
                                  changed=True)
                 diffs.append(diff)
     if diffs:
         if isinstance(form1, Form):
             label = name = ''
             before = lambda: optional_unicode(form1.item)
             after = lambda: optional_unicode(form2.item)
         else:
             label = form1.label or form1.name
             name = form1.input_name
             before = lambda: optional_unicode(form1.clean_value)
             after = lambda: optional_unicode(form2.clean_value)
         return dict(label=label or '',
                     name=name or '',
                     before=before,
                     after=after,
                     children=diffs,
                     changed=True)