# Path: cpm_generic/migration_utils.py
# 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 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 remove_subpage(parent, model, **kwargs):
#     parent.numchild -= 1
# 
#     model.objects.filter(**kwargs).delete()
#     parent.save()


from django.db import migrations
from cpm_generic.migration_utils import (get_content_type, add_subpage,
                                         remove_subpage)
def add_partners_page(apps, schema_editor):
    HomePage = apps.get_model('home.HomePage')
    PartnersPage = apps.get_model('partners.PartnersPage')
    Season = apps.get_model('cpm_data.Season')

    partners_page_ct = get_content_type(apps, 'partners', 'partnerspage')

    homepage = HomePage.objects.get(slug='home')
    season = Season.objects.get(name_en='2017')

    add_subpage(
        homepage,
        PartnersPage,
        content_type=partners_page_ct,
        title=u'CPM Partners',
        slug='partners',
        name_en=u'Partners',
        name_be=u'Партнёры',
        name_ru=u'Партнеры',
        entry_en=u'<i>Cinema Perpetuum Mobile</i> is a non-profit, independent project which keeps on expenses and aspirations of the organizers. At the same time international nature of the event, cultural resonance provides fertile ground for collaboration with business and public organizations.',  # noqa:E501
        entry_be=u'<i>Cinema Perpetuum Mobile</i> - некамерцыйны, незалежны праект, які трымаецца на імкненнях і сродках арганізатараў. У той жа час чакаемы культурны рэзананс падзеі, яе міжнародны характар, актыўныя зацікаўленнасці, ствараюць выдатную глебу для супрацоўніцтва з камерцыйнымі і грамадскімі ўстановамі.',  # noqa:E501
        entry_ru=u'<i>Cinema Perpetuum Mobile</i> - некоммерческий, независимый проект, который держится на устремлениях и расходах организаторов. В то же время международный характер события, его культурный резонанс, заинтересованности, создают благоприятную почву для сотрудничества с коммерческими и общественными организациями.',  # noqa:E501
        season=season
    )


def remove_partners_page(apps, schema_editor):
    HomePage = apps.get_model('home.HomePage')
    PartnersPage = apps.get_model('partners.PartnersPage')


# Path: cpm_generic/migration_utils.py
# 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 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 remove_subpage(parent, model, **kwargs):
#     parent.numchild -= 1
# 
#     model.objects.filter(**kwargs).delete()
#     parent.save()


# Path: partners/migrations/0002_partners_page_data.py
from django.db import migrations
from cpm_generic.migration_utils import (get_content_type, add_subpage,
                                         remove_subpage)

def add_partners_page(apps, schema_editor):
    HomePage = apps.get_model('home.HomePage')
    PartnersPage = apps.get_model('partners.PartnersPage')
    Season = apps.get_model('cpm_data.Season')

    partners_page_ct = get_content_type(apps, 'partners', 'partnerspage')

    homepage = HomePage.objects.get(slug='home')
    season = Season.objects.get(name_en='2017')

    add_subpage(
        homepage,
        PartnersPage,
        content_type=partners_page_ct,
        title=u'CPM Partners',
        slug='partners',
        name_en=u'Partners',
        name_be=u'Партнёры',
        name_ru=u'Партнеры',
        entry_en=u'<i>Cinema Perpetuum Mobile</i> is a non-profit, independent project which keeps on expenses and aspirations of the organizers. At the same time international nature of the event, cultural resonance provides fertile ground for collaboration with business and public organizations.',  # noqa:E501
        entry_be=u'<i>Cinema Perpetuum Mobile</i> - некамерцыйны, незалежны праект, які трымаецца на імкненнях і сродках арганізатараў. У той жа час чакаемы культурны рэзананс падзеі, яе міжнародны характар, актыўныя зацікаўленнасці, ствараюць выдатную глебу для супрацоўніцтва з камерцыйнымі і грамадскімі ўстановамі.',  # noqa:E501
        entry_ru=u'<i>Cinema Perpetuum Mobile</i> - некоммерческий, независимый проект, который держится на устремлениях и расходах организаторов. В то же время международный характер события, его культурный резонанс, заинтересованности, создают благоприятную почву для сотрудничества с коммерческими и общественными организациями.',  # noqa:E501
        season=season
    )


def remove_partners_page(apps, schema_editor):
    HomePage = apps.get_model('home.HomePage')
    PartnersPage = apps.get_model('partners.PartnersPage')


    partners_page_ct = get_content_type(apps, 'partners', 'partnerspage')