# Path: content/models.py
# class ContentPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
# 
#     content_panels = Page.content_panels + [
#         StreamFieldPanel('body')
#     ]
#     search_fields = Page.search_fields + [
#         index.SearchField('body')
#     ]
# 
# Path: kehmet/models.py
# class KehmetContentPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
#     tags = ClusterTaggableManager(through=KehmetContentPageTag, blank=True)
#     show_in_submenus = models.BooleanField(verbose_name=_('Show in sub menus'), help_text=_('Page is visible on the submenu'), default=True)
# 
#     content_panels = [
#         FieldPanel('title', classname='full title'),
#         FieldPanel('tags'),
#         StreamFieldPanel('body'),
#     ]
# 
#     promote_panels = Page.promote_panels + [
#         MultiFieldPanel([FieldPanel('show_in_submenus'),], _('Custom page configuration')),
#     ]
# 
#     search_fields = Page.search_fields + [
#         index.SearchField('body'),
#         tag_search_field,
#     ]
# 
#     parent_page_types = ['KehmetContentPage', 'KehmetFrontPage']
# 
# class KehmetFrontPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
# 
#     content_panels = Page.content_panels + [
#         StreamFieldPanel('body')
#     ]
#     search_fields = Page.search_fields + [
#         index.SearchField('body')
#     ]
# 
#     subpage_types = ['KehmetContentPage']


from django.contrib.contenttypes.models import ContentType
from django.db import models, transaction
from wagtail.core.models import Page
from content.models import ContentPage
from kehmet.models import KehmetContentPage, KehmetFrontPage


def convert_page(page, target_model):
    try:
        page.kehmetcontentpage
        return
    except:
        pass

    kcp_type = ContentType.objects.get_for_model(target_model)
    cp_page = page.specific
    kcp_page = target_model(body=cp_page.body, page_ptr=page)
    for f in kcp_page._meta.fields:
        setattr(kcp_page, f.name, getattr(cp_page, f.name))
    super(Page, kcp_page).save()

    for f in dummy_page._meta.fields:
        setattr(cp_page, f.name, getattr(dummy_page, f.name))
    cp_page.page_ptr_id = dummy_page.id
    cp_page.save()
    print(page)
    models.Model.delete(cp_page, keep_parents=True)

    page.content_type = kcp_type
    page.save(update_fields=['content_type'])


with transaction.atomic():
    dummy_page.save()
    if not isinstance(k_root, KehmetFrontPage):

# Path: content/models.py
# class ContentPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
# 
#     content_panels = Page.content_panels + [
#         StreamFieldPanel('body')
#     ]
#     search_fields = Page.search_fields + [
#         index.SearchField('body')
#     ]
# 
# Path: kehmet/models.py
# class KehmetContentPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
#     tags = ClusterTaggableManager(through=KehmetContentPageTag, blank=True)
#     show_in_submenus = models.BooleanField(verbose_name=_('Show in sub menus'), help_text=_('Page is visible on the submenu'), default=True)
# 
#     content_panels = [
#         FieldPanel('title', classname='full title'),
#         FieldPanel('tags'),
#         StreamFieldPanel('body'),
#     ]
# 
#     promote_panels = Page.promote_panels + [
#         MultiFieldPanel([FieldPanel('show_in_submenus'),], _('Custom page configuration')),
#     ]
# 
#     search_fields = Page.search_fields + [
#         index.SearchField('body'),
#         tag_search_field,
#     ]
# 
#     parent_page_types = ['KehmetContentPage', 'KehmetFrontPage']
# 
# class KehmetFrontPage(RelativeURLMixin, Page):
#     body = StreamField(content_blocks)
# 
#     content_panels = Page.content_panels + [
#         StreamFieldPanel('body')
#     ]
#     search_fields = Page.search_fields + [
#         index.SearchField('body')
#     ]
# 
#     subpage_types = ['KehmetContentPage']


# Path: scripts/migrate_kehmet_pages.py
from django.contrib.contenttypes.models import ContentType
from django.db import models, transaction
from wagtail.core.models import Page
from content.models import ContentPage
from kehmet.models import KehmetContentPage, KehmetFrontPage



def convert_page(page, target_model):
    try:
        page.kehmetcontentpage
        return
    except:
        pass

    kcp_type = ContentType.objects.get_for_model(target_model)
    cp_page = page.specific
    kcp_page = target_model(body=cp_page.body, page_ptr=page)
    for f in kcp_page._meta.fields:
        setattr(kcp_page, f.name, getattr(cp_page, f.name))
    super(Page, kcp_page).save()

    for f in dummy_page._meta.fields:
        setattr(cp_page, f.name, getattr(dummy_page, f.name))
    cp_page.page_ptr_id = dummy_page.id
    cp_page.save()
    print(page)
    models.Model.delete(cp_page, keep_parents=True)

    page.content_type = kcp_type
    page.save(update_fields=['content_type'])


with transaction.atomic():
    dummy_page.save()
    if not isinstance(k_root, KehmetFrontPage):

        convert_page(k_root, KehmetFrontPage)