Пример #1
0
    def test_round_trip(self):
        base_toc = library.get_toc()
        base_json = json.dumps(base_toc, sort_keys=True)
        oo_toc = c.toc_serial_to_objects(base_toc)
        rt_toc = oo_toc.serialize()["contents"]

        # Deep test of toc lists
        assert not DeepDiff(base_toc, rt_toc)

        # Check that the json is identical -
        # that the round-trip didn't change anything by reference that would poison the deep test
        new_json = json.dumps(rt_toc, sort_keys=True)
        assert len(base_json) == len(new_json)
Пример #2
0
    def test_round_trip(self):
        base_toc = library.get_toc()
        base_json = json.dumps(base_toc, sort_keys=True)
        oo_toc = c.toc_serial_to_objects(base_toc)
        rt_toc = oo_toc.serialize()["contents"]

        # Deep test of toc lists
        assert not DeepDiff(base_toc, rt_toc)

        # Check that the json is identical -
        # that the round-trip didn't change anything by reference that would poison the deep test
        new_json = json.dumps(rt_toc, sort_keys=True)
        assert len(base_json) == len(new_json)
    Modifies contents in place.
    :param treenode:
    :param callback:
    :param order:
    :param kwargs:
    :return:
    """
    if treenode.children:
        for i, node in enumerate(treenode.children):
            visit_structure(node, callback, i, **kwargs)
        if order is not None:  # skip root
            callback(treenode, **kwargs)


def create_category(treenode):
    c = Category()
    if Term().load({"name": treenode.primary_title("en")}):
        c.add_shared_term(treenode.primary_title("en"))
    else:
        c.add_primary_titles(treenode.primary_title("en"),
                             treenode.primary_title("he"))
    c.path = treenode.full_path
    c.lastPath = treenode.full_path[-1]
    print("Creating - {}".format(" / ".join(c.path)))
    c.save(override_dependencies=True)


db.category.remove({})
serial_toc = update_table_of_contents()
toctree = toc_serial_to_objects(serial_toc)
visit_structure(toctree, create_category)
    Tree visitor for traversing existing structure nodes of content trees and passing them to callback.
    Traverses from bottom up, with intention that this be used to aggregate content from content nodes up.
    Modifies contents in place.
    :param treenode:
    :param callback:
    :param order:
    :param kwargs:
    :return:
    """
    if treenode.children:
        for i, node in enumerate(treenode.children):
            visit_structure(node, callback, i, **kwargs)
        if order is not None:  # skip root
            callback(treenode, **kwargs)


def create_category(treenode):
    c = Category()
    if Term().load({"name": treenode.primary_title("en")}):
        c.add_shared_term(treenode.primary_title("en"))
    else:
        c.add_primary_titles(treenode.primary_title("en"), treenode.primary_title("he"))
    c.path = treenode.full_path
    c.lastPath = treenode.full_path[-1]
    print "Creating - {}".format(" / ".join(c.path))
    c.save(override_dependencies=True)

db.category.remove({})
serial_toc = update_table_of_contents()
toctree = toc_serial_to_objects(serial_toc)
visit_structure(toctree, create_category)