# Path: cpm_generic/migration_utils.py
# def add_subpage(parent, model, *args, **kwargs):
#     step = 4
#     alphabet = MP_Node.alphabet
#     numconv = NumConv(len(alphabet), alphabet)
# 
#     children_qs = Page.objects.filter(depth=parent.depth + 1,
#                                       path__startswith=parent.path)
#     sibling = children_qs.order_by('-path').first()
#     sibling_num = numconv.str2int(sibling.path[-step:]) if sibling else 0
#     next_key = numconv.int2str(sibling_num + 1)
#     path = '%s%s%s' % (parent.path,
#                        alphabet[0] * (step - len(next_key)),
#                        next_key)
# 
#     parent.numchild += 1
#     kwargs.setdefault('depth', parent.depth + 1)
#     kwargs.setdefault('path', path)
#     kwargs.setdefault('numchild', 0)
#     kwargs.setdefault('url_path', '%s%s/' % (parent.url_path, kwargs['slug']))
# 
#     child = model(*args, **kwargs)
# 
#     child.save()
#     parent.save()
# 
#     return child
# 
# def get_content_type(apps, app_label, model):
#     ContentType = apps.get_model('contenttypes.ContentType')
#     content_type, _ = ContentType.objects.get_or_create(
#         model=model,
#         app_label=app_label
#     )
#     return content_type
# 
# def remove_subpage(parent, model, **kwargs):
#     parent.numchild -= 1
# 
#     model.objects.filter(**kwargs).delete()
#     parent.save()


import json
import os
from dateutil.parser import parse as parse_datetime
from django.conf import settings
from django.db import migrations
from cpm_generic.migration_utils import (add_subpage, get_content_type,
                                         remove_subpage)
                title=item['title'],
            )


def add_venues_2017(apps, schema_editor):
    Venue = apps.get_model('events.Venue')

    for item in _get_data('venues.json'):
        if not Venue.objects.filter(name_en=item['name_en']).exists():
            Venue(**item).save()


def remove_venues_2017(apps, schema_editor):
    Venue = apps.get_model('events.Venue')

    for item in _get_data('venues.json'):
        try:
            program = Venue.objects.get(name_en=item['name_en'])
        except Venue.DoesNotExist:
            pass
        else:
            program.delete()


def add_timetable_2017(apps, schema_editor):
    FilmProgram = apps.get_model('events.FilmProgram')
    TimeTable = apps.get_model('events.TimeTable')
    TimeTableEvent = apps.get_model('events.TimeTableEvent')
    Venue = apps.get_model('events.Venue')


# Path: cpm_generic/migration_utils.py
# def add_subpage(parent, model, *args, **kwargs):
#     step = 4
#     alphabet = MP_Node.alphabet
#     numconv = NumConv(len(alphabet), alphabet)
# 
#     children_qs = Page.objects.filter(depth=parent.depth + 1,
#                                       path__startswith=parent.path)
#     sibling = children_qs.order_by('-path').first()
#     sibling_num = numconv.str2int(sibling.path[-step:]) if sibling else 0
#     next_key = numconv.int2str(sibling_num + 1)
#     path = '%s%s%s' % (parent.path,
#                        alphabet[0] * (step - len(next_key)),
#                        next_key)
# 
#     parent.numchild += 1
#     kwargs.setdefault('depth', parent.depth + 1)
#     kwargs.setdefault('path', path)
#     kwargs.setdefault('numchild', 0)
#     kwargs.setdefault('url_path', '%s%s/' % (parent.url_path, kwargs['slug']))
# 
#     child = model(*args, **kwargs)
# 
#     child.save()
#     parent.save()
# 
#     return child
# 
# def get_content_type(apps, app_label, model):
#     ContentType = apps.get_model('contenttypes.ContentType')
#     content_type, _ = ContentType.objects.get_or_create(
#         model=model,
#         app_label=app_label
#     )
#     return content_type
# 
# def remove_subpage(parent, model, **kwargs):
#     parent.numchild -= 1
# 
#     model.objects.filter(**kwargs).delete()
#     parent.save()


# Path: events/migrations/0004_timetable_2017.py
import json
import os
from dateutil.parser import parse as parse_datetime
from django.conf import settings
from django.db import migrations
from cpm_generic.migration_utils import (add_subpage, get_content_type,
                                         remove_subpage)

                title=item['title'],
            )


def add_venues_2017(apps, schema_editor):
    Venue = apps.get_model('events.Venue')

    for item in _get_data('venues.json'):
        if not Venue.objects.filter(name_en=item['name_en']).exists():
            Venue(**item).save()


def remove_venues_2017(apps, schema_editor):
    Venue = apps.get_model('events.Venue')

    for item in _get_data('venues.json'):
        try:
            program = Venue.objects.get(name_en=item['name_en'])
        except Venue.DoesNotExist:
            pass
        else:
            program.delete()


def add_timetable_2017(apps, schema_editor):
    FilmProgram = apps.get_model('events.FilmProgram')
    TimeTable = apps.get_model('events.TimeTable')
    TimeTableEvent = apps.get_model('events.TimeTableEvent')
    Venue = apps.get_model('events.Venue')


    timetable_ct = get_content_type(apps, 'events', 'timetable')