<commit_before>import collections

from django.core.management import call_command
from django.core.management.base import BaseCommand

from contentcuration.constants import content_kinds

from contentcuration import models as ccmodels
from kolibri.content import models as kolibrimodels

import logging as logmodule
logging = logmodule.getLogger(__name__)


class EarlyExit(BaseException):
    def __init__(self, message, db_path):
        self.message = message
        self.db_path = db_path


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('channel_id', type=str)

    def handle(self, *args, **options):
        # license_id = options['license_id']
        channel_id = options['channel_id']

        # license = ccmodels.License.objects.get(pk=license_id)
        try:
            channel = ccmodels.Channel.objects.get(pk=channel_id)
            # increment the channel version
            # raise_if_nodes_are_all_unchanged(channel)
            mark_all_nodes_as_changed(channel)
            # assign_license_to_contentcuration_nodes(channel, license)
            # create_kolibri_license_object(license)
            increment_channel_version(channel)
            prepare_export_database()
            # TODO: increment channel version numbers when we mark nodes as changed as well
            map_content_tags(channel)

            map_channel_to_kolibri_channel(channel)
            map_content_nodes(channel.main_tree,)
            # use SQLite backup API to put DB into archives folder.
            # Then we can use the empty db name to have SQLite use a temporary DB (https://www.sqlite.org/inmemorydb.html)

        except EarlyExit as e:
            logging.warning("Exited early due to {message}.".format(
                message=e.message))
            self.stdout.write("You can find your database in {path}".format(
                path=e.db_path))


def create_kolibri_license_object(license):
    return kolibrimodels.License.objects.get_or_create(
        license_name=license.license_name
    )


def increment_channel_version(channel):
    channel.version += 1
    channel.save()


def assign_license_to_contentcuration_nodes(channel, license):
    channel.main_tree.get_family().update(license_id=license.pk)


def map_content_tags(channel):
    logging.debug("Creating the Kolibri content tags.")

    cctags = ccmodels.ContentTag.objects.filter(
        channel=channel).values("tag_name")
    kolibrimodels.ContentTag.objects.bulk_create(
        [kolibrimodels.ContentTag(**vals) for vals in cctags])

    logging.info("Finished creating the Kolibri content tags.")


def map_content_nodes(root_node):
<commit_msg>Add the UUIDField as well when mapping content tags.<commit_after>import collections

from django.core.management import call_command
from django.core.management.base import BaseCommand

from contentcuration.constants import content_kinds

from contentcuration import models as ccmodels
from kolibri.content import models as kolibrimodels

import logging as logmodule
logging = logmodule.getLogger(__name__)


class EarlyExit(BaseException):
    def __init__(self, message, db_path):
        self.message = message
        self.db_path = db_path


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('channel_id', type=str)

    def handle(self, *args, **options):
        # license_id = options['license_id']
        channel_id = options['channel_id']

        # license = ccmodels.License.objects.get(pk=license_id)
        try:
            channel = ccmodels.Channel.objects.get(pk=channel_id)
            # increment the channel version
            # raise_if_nodes_are_all_unchanged(channel)
            mark_all_nodes_as_changed(channel)
            # assign_license_to_contentcuration_nodes(channel, license)
            # create_kolibri_license_object(license)
            increment_channel_version(channel)
            prepare_export_database()
            # TODO: increment channel version numbers when we mark nodes as changed as well
            map_content_tags(channel)

            map_channel_to_kolibri_channel(channel)
            map_content_nodes(channel.main_tree,)
            # use SQLite backup API to put DB into archives folder.
            # Then we can use the empty db name to have SQLite use a temporary DB (https://www.sqlite.org/inmemorydb.html)

        except EarlyExit as e:
            logging.warning("Exited early due to {message}.".format(
                message=e.message))
            self.stdout.write("You can find your database in {path}".format(
                path=e.db_path))


def create_kolibri_license_object(license):
    return kolibrimodels.License.objects.get_or_create(
        license_name=license.license_name
    )


def increment_channel_version(channel):
    channel.version += 1
    channel.save()


def assign_license_to_contentcuration_nodes(channel, license):
    channel.main_tree.get_family().update(license_id=license.pk)


def map_content_tags(channel):
    logging.debug("Creating the Kolibri content tags.")

    cctags = ccmodels.ContentTag.objects.filter(
        channel=channel).values("tag_name", "id")
    kolibrimodels.ContentTag.objects.bulk_create(
        [kolibrimodels.ContentTag(**vals) for vals in cctags])

    logging.info("Finished creating the Kolibri content tags.")


def map_content_nodes(root_node):

{'fraction_non_alphanumeric': 0.08, 'fraction_numerical': 0.0003686635944700461, 'mean_word_length': 3.2758077226162334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373569', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1511, 'n_words': 351}