def check(result, must_be, no):
     failed = result != must_be
     if failed or display_result:
         txt = "No. %s - result:" % no
         print_data(txt, result)
     if failed:
         txt = "ERROR %s - must be:" % no
         print_data(txt, must_be)
         
         try:
             # Display a diff, if PyLucid's diff tool is available
             from PyLucid.tools.Diff import diff_lines
         except ImportError:
             pass
         else:
             print "-"*79
             print diff_lines(pformat(must_be), pformat(result))
             print "-"*79
         
         raise AssertionError("wrong result.")
    def _update(self, model, content_attr_name, info_text):
        """
        Update all Entries from the given model.
        """
        from PyLucid.tools.Diff import diff_lines

        print "\n"*3
        print "*"*79
        print " **** Update all %s ****" % info_text
        print "*"*79
        print "\n"*2

        model_objects = model.objects.all()
        for model_item in model_objects:
            name = model_item.name
            print "\n================[ %s ]================" % name

            old_content = getattr(model_item, content_attr_name)

            content = self._update_content(old_content)

            if model_item.createtime == None:
                # Should be normaly not None :)
                # Is from a old PyLucid installation
                model_item.createtime = datetime.now()
                print "Update 'createtime' to now. (Time was not set.)"
            else:
                if content == old_content:
                    print "\t - Nothing to change."
                    continue

            print "\t - changed!\n\nthe diff:"
            print diff_lines(old_content, content)

            # assign new content to model
            setattr(model_item, content_attr_name, content)

            # save the new content
            model_item.save()
    def assertEqual2(self, first, second, error_msg=None):
        """
        Same as the original, but display a diff on error.
        Makes only sence if the string contains more lines (\n)
        """
        if first == second:
            # is equal, ok
            return

        if error_msg:
            error_msg += "\n"
        else:
            error_msg = ""

        error_msg += "Strings not equal:\n"
        error_msg += "1: %r\n" % first
        error_msg += "2: %r\n" % second
        error_msg += "Diff:\n"
        error_msg += diff_lines(first, second)

        self.fail(error_msg)
    def do(self, find_string, replace_string, type, simulate):
        """
        Do the find/replace action.
        Returns a result list with diff information.
        """
        def nothing_found():
            # We used this two times
            self.page_msg(
                "No %s contains the string '%s'" % (type, find_string)
            )
            return None, None

        if type == "pages":
            model_object = Page
        elif type == "templates":
            model_object = Template
        elif type == "stylesheets":
            model_object = Style
        else:
            self.page_msg.red("Wrong type!")
            return None, None

        items = model_object.objects.all().filter(
            content__contains=find_string
        )
        if len(items) == 0:
            return nothing_found()

        total_changes = 0
        results = []
        changed_items = []
        for item in items:
            old_content = item.content

            changes = old_content.count(find_string)
            if changes == 0:
                # SQlite work-a-round for the
                continue

            total_changes += changes

            new_content = old_content.replace(find_string, replace_string)
            if not simulate:
                # Save the find/replace result
                item.content = new_content
                item.save()
                changed_items.append(item.name)

            diff = diff_lines(old_content, new_content)
            diff = escape(diff)
            diff = mark_safe(diff)

            results.append({
                "item": item,
                "changes": changes,
                "diff": diff,
            })

        if total_changes == 0:
            # SQLite work-a-round
            return nothing_found()

        if not simulate:
            self.page_msg.green("Changed %s:" % type)
            self.page_msg.green(", ".join(changed_items))

        return results, total_changes