@@ -0,0 +1,2904 @@
+# -*- coding: utf-8 -*-%0A# Generated by Django 1.11 on 2017-05-24 19:33%0Afrom __future__ import unicode_literals%0A%0Aimport os%0Aimport json%0Aimport logging%0A%0Afrom django.db import migrations%0A%0Afrom website import settings%0Afrom osf.models import NodeLicense%0A%0Alogger = logging.getLogger(__file__)%0A%0A%0Adef add_licenses(*args):%0A    %22%22%22Upsert the licenses in our database based on a JSON file.%0A%0A    :return tuple: (number inserted, number updated)%0A%0A    Moved from website/project/licenses/__init__.py%0A    %22%22%22%0A    ninserted = 0%0A    nupdated = 0%0A    with open(%0A            os.path.join(%0A                settings.APP_PATH,%0A                'node_modules', 'list-of-licenses', 'dist', 'list-of-licenses.json'%0A            )%0A    ) as fp:%0A        licenses = json.loads(fp.read())%0A        for id, info in licenses.items():%0A            name = info%5B'name'%5D%0A            text = info%5B'text'%5D%0A            properties = info.get('properties', %5B%5D)%0A%0A            model_kwargs = dict(%0A                license_id=id,%0A                name=name,%0A                text=text,%0A                properties=properties%0A            )%0A%0A            node_license, created = NodeLicense.objects.get_or_create(**model_kwargs)%0A%0A            if not created:%0A                node_license.name = name%0A                node_license.text = text%0A                node_license.properties = properties%0A                node_license.save()%0A                nupdated += 1%0A            else:%0A                ninserted += 1%0A%0A            logger.info('License %7Bname%7D (%7Bid%7D) added to the database.'.format(name=name, id=id))%0A%0A    logger.info('%7B%7D licenses inserted into the database, %7B%7D licenses updated in the database.'.format(%0A        ninserted, nupdated%0A    ))%0A%0A%0Adef remove_licenses(*args):%0A    ndeleted = 0%0A    with open(%0A            os.path.join(%0A                settings.APP_PATH,%0A                'node_modules', 'list-of-licenses', 'dist', 'list-of-licenses.json'%0A            )%0A    ) as fp:%0A        licenses = json.loads(fp.read())%0A        for id, info in licenses.items():%0A            name = info%5B'name'%5D%0A            text = info%5B'text'%5D%0A            properties = info.get('properties', %5B%5D)%0A%0A            model_kwargs = dict(%0A                license_id=id,%0A                name=name,%0A                text=text,%0A                properties=properties%0A            )%0A%0A            try:%0A                node_license = NodeLicense.objects.get(**model_kwargs)%0A                node_license.delete()%0A                ndeleted += 1%0A                logger.info('License %7Bname%7D (%7Bid%7D) removed from the database.'.format(name=name, id=id))%0A            except NodeLicense.DoesNotExist:%0A                pass%0A%0A    logger.info('%7B%7D licenses removed from the database.'.format(ndeleted))%0A%0A%0Aclass Migration(migrations.Migration):%0A%0A    dependencies = %5B%0A        ('osf', '0034_rename_email_user_relation'),%0A    %5D%0A%0A    operations = %5B%0A        migrations.RunPython(add_licenses, remove_licenses),%0A    %5D%0A

Add migration for ensure_licenses