@@ -0,0 +1,2543 @@
+#!/usr/bin/env python%0A# -*- coding: utf-8 -*-%0Aimport logging%0A%0Afrom tests.base import OsfTestCase%0Afrom tests.factories import NodeFactory%0A%0Afrom website.project.model import Node%0A%0Alogger = logging.getLogger(__name__)%0A%0A# legacy =%3E new category%0AMIGRATE_MAP = %7B%0A    'category': '',%0A    'measure': 'methods and measures',%0A%7D%0A%0A%0Adef migrate_category(node):%0A    %22%22%22Migrate legacy, invalid category to new, valid category. Return whether%0A    the node was changed.%0A    %22%22%22%0A    if node.category not in Node.CATEGORY_MAP.keys():  # invalid category%0A        node.category = MIGRATE_MAP.get(node.category, 'other')%0A        return True%0A    return False%0A%0A%0Adef migrate_nodes():%0A    migrated_count = 0%0A    for node in Node.find():%0A        was_migrated = migrate_category(node)%0A        if was_migrated:%0A            node.save()%0A            logger.info('Migrated %7B0%7D'.format(node._id))%0A            migrated_count += 1%0A    logger.info('Finished migrating %7B0%7D nodes.'.format(migrated_count))%0A%0A%0Aclass TestMigratingCategories(OsfTestCase):%0A%0A    def test_migrate_category(self):%0A        node = NodeFactory(category='category')%0A        was_migrated = migrate_category(node)%0A        assert was_migrated is True%0A        node.save()%0A        assert node.category == ''%0A%0A    def test_migrate_measure(self):%0A        node = NodeFactory(category='measure')%0A        migrate_category(node)%0A        node.save()%0A        assert node.category == 'methods and measures'%0A%0A    def test_everything_else_is_migrated_to_other(self):%0A        node1 = NodeFactory(category='background')%0A        migrate_category(node1)%0A        node1.save()%0A        assert node1.category == 'other'%0A%0A        node2 = NodeFactory(category=u'%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88')%0A        migrate_category(node2)%0A        node2.save()%0A        assert node2.category == 'other'%0A%0A    def test_valid_categories_not_migrated(self):%0A        node1 = NodeFactory(category='project')%0A        node2 = NodeFactory(category='hypothesis')%0A%0A        was_migrated1 = migrate_category(node1)%0A        was_migrated2 = migrate_category(node2)%0A%0A        node1.save()%0A        node2.save()%0A%0A        assert was_migrated1 is False%0A        assert was_migrated2 is False%0A        assert node1.category == 'project'%0A        assert node2.category == 'hypothesis'%0A%0Aclass TestMigrateAll(OsfTestCase):%0A%0A    def test_migrate_categories_all(self):%0A        n1 = NodeFactory(category='hypothesis')%0A        n2 = NodeFactory(category='category')%0A%0A        migrate_nodes()%0A%0A        assert n1.category == 'hypothesis'%0A        assert n2.category == ''%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add script for migrating categories