<commit_before>from django.utils import timezone
from django.core.management.base import BaseCommand

from ct.models import Course, Role


def copy_model_instance(inst, **kwargs):
    from copy import copy
    n_inst = copy(inst)
    n_inst.id = None
    if kwargs:
        for k, v in kwargs.items():
            setattr(n_inst, k, v)
    n_inst.save()
    return n_inst


class Command(BaseCommand):
    help = 'Get report'

    def add_arguments(self, parser):
        # Positional arguments
        parser.add_argument('course_id', type=int)

    def handle(self, *args, **options):
        course_id=options['course_id']
        course = Course.objects.filter(id=course_id).first()

        if course:
            new_course = copy_model_instance(course, atime=timezone.now())
            for cu in course.courseunit_set.all():
                # deal w ith unit
                n_unit = copy_model_instance(cu.unit, atime=timezone.now())
                # deal with CourseUnit
                n_cu = copy_model_instance(cu, course=new_course, unit=n_unit, atime=timezone.now())

                uls = list(cu.unit.unitlesson_set.filter(parent__isnull=True))  # uls without parent

                # deal with unit lessons which has parent
                nuls_ids = {}  # old_id : (new_id, instance)
                for ul in uls:
                    n_ul = copy_model_instance(ul, unit=n_unit, atime=timezone.now())
                    n_ul.treeID = n_ul.id
                    n_ul.save()
                    nuls_ids[ul.id] = (n_ul.id, n_ul)

                def deep_cp_ul(left_to_copy):
                    for ul in left_to_copy:
                        n_parent = nuls_ids.get(ul.parent.id, [None, None])[1]
                        if n_parent:
                            if ul in left_to_copy:
                                left_to_copy.remove(ul)
                            n_ul = copy_model_instance(ul, unit=n_unit, atime=timezone.now())
                            n_ul.treeID = n_ul.id
                            n_ul.parent = n_parent
                            n_ul.save()
                            # add new ul to parents dict
                            nuls_ids[ul.id] = (n_ul.id, n_ul)
                    if left_to_copy:
                        deep_cp_ul(left_to_copy)

                DEEP_LVLS = 3

                for lvl in range(1, DEEP_LVLS + 1):
                    parent_q = '__parent' * lvl
                    uls = list(cu.unit.unitlesson_set.filter(
                        parent__isnull=False,
                        **{'parent' + parent_q + '__isnull': True}
                    ).order_by(
                        '-parent' + parent_q
                    ))
                    deep_cp_ul(uls)

            for role in course.role_set.filter(role=Role.INSTRUCTOR):
                n_role = copy_model_instance(role, course=new_course, atime=timezone.now())

            print('Done')
            print('New Course id is {0}'.format(new_course.id))
        else:
            print('There is no Course w/ such id')<commit_msg>fix(copy_course): Fixes to remove tripling ( instead of 1 we got 3) error models.<commit_after>from copy import copy

from django.utils import timezone
from django.core.management.base import BaseCommand

from ct.models import Course, Role


def copy_model_instance(inst, **kwargs):
    n_inst = copy(inst)
    n_inst.id = None
    if kwargs:
        for k, v in kwargs.items():
            setattr(n_inst, k, v)
    n_inst.save()
    return n_inst


class Command(BaseCommand):
    help = 'Get report'

    def add_arguments(self, parser):
        # Positional arguments
        parser.add_argument('course_id', type=int)

    def handle(self, *args, **options):
        course_id=options['course_id']
        course = Course.objects.filter(id=course_id).first()

        if course:
            new_course = copy_model_instance(course, atime=timezone.now())
            for cu in course.courseunit_set.all():
                # deal w ith unit
                n_unit = copy_model_instance(cu.unit, atime=timezone.now())
                # deal with CourseUnit
                n_cu = copy_model_instance(cu, course=new_course, unit=n_unit, atime=timezone.now())

                uls = list(cu.unit.unitlesson_set.filter(parent__isnull=True))  # uls without parent

                # deal with unit lessons which has parent
                nuls_ids = {}  # old_id : (new_id, instance)
                for ul in uls:
                    n_ul = copy_model_instance(ul, unit=n_unit, atime=timezone.now())
                    n_ul.treeID = n_ul.id
                    n_ul.save()
                    nuls_ids[ul.id] = (n_ul.id, n_ul)

                def deep_cp_ul(left_to_copy):
                    for ul in left_to_copy:
                        n_parent = nuls_ids.get(ul.parent.id, [None, None])[1]
                        if n_parent:
                            if ul in left_to_copy:
                                left_to_copy.remove(ul)
                            n_ul = copy_model_instance(ul, unit=n_unit, atime=timezone.now())
                            n_ul.treeID = n_ul.id
                            n_ul.parent = n_parent
                            n_ul.save()
                            # add new ul to parents dict
                            nuls_ids[ul.id] = (n_ul.id, n_ul)
                    if left_to_copy:
                        deep_cp_ul(left_to_copy)

                uls = list(cu.unit.unitlesson_set.filter(
                    parent__isnull=False,
                ).order_by(
                    '-parent'
                ))
                deep_cp_ul(uls)

            for role in course.role_set.filter(role=Role.INSTRUCTOR):
                n_role = copy_model_instance(role, course=new_course, atime=timezone.now())

            print('Done')
            print('New Course id is {0}'.format(new_course.id))
        else:
            print('There is no Course w/ such id')
{'fraction_non_alphanumeric': 0.09154694286668894, 'fraction_numerical': 0.001503508185766789, 'mean_word_length': 1.4987479131886476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968791', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1725, 'n_words': 379}