# Generated by Django 2.1.4 on 2019-01-15 21:11

from django.db import migrations, models
import django.db.models.deletion
import document.models

DOC_TEMPLATE = '''{
    "type": "article",
    "content": [{
        "type": "title"
    }, {
        "type": "heading_part",
        "attrs": {
            "id": "subtitle",
            "title": "Subtitle",
            "initial": [{
                "type": "heading1",
                "attrs": {
                    "id": "H6323428"
                }
            }],
            "optional": "hidden",
            "hidden": true,
            "elements": ["heading1"],
            "marks": ["strong", "em", "link", "anchor"],
            "metadata": "subtitle"
        },
        "content": [{
            "type": "heading1",
            "attrs": {
                "id": "H6323428"
            }
        }]
    }, {
        "type": "contributors_part",
        "attrs": {
            "id": "authors",
            "title": "Authors",
            "optional": "hidden",
            "hidden": true,
            "item_title": "Author",
            "metadata": "authors"
        }
    }, {
        "type": "richtext_part",
        "attrs": {
            "id": "abstract",
            "title": "Abstract",
            "optional": "hidden",
            "hidden": true,
            "elements": ["paragraph", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6", "figure", "ordered_list", "bullet_list", "horizontal_rule", "equation", "citation", "blockquote", "footnote", "table"],
            "marks": ["strong", "em", "link", "anchor"],
            "metadata": "abstract"
        },
        "content": [{
            "type": "paragraph"
        }]
    }, {
        "type": "tags_part",
        "attrs": {
            "id": "keywords",
            "title": "Keywords",
            "optional": "hidden",
            "hidden": true,
            "item_title": "Keyword",
            "metadata": "keywords"
        }
    }, {
        "type": "richtext_part",
        "attrs": {
            "id": "body",
            "title": "Body",
            "elements": ["paragraph", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6", "figure", "ordered_list", "bullet_list", "horizontal_rule", "equation", "citation", "blockquote", "footnote", "table"],
            "marks": ["strong", "em", "link", "anchor"]
        },
        "content": [{
            "type": "paragraph"
        }]
    }],
    "attrs": {
        "footnote_elements": ["paragraph", "heading1", "heading2", "heading3", "heading4", "heading5", "heading6", "figure", "ordered_list", "bullet_list", "horizontal_rule", "equation", "citation", "blockquote", "table"],
        "footnote_marks": ["strong", "em", "link", "anchor"],
        "languages": ["af-ZA", "sq-AL", "ar", "ast", "be", "br", "bg", "ca", "ca-ES-Valencia", "zh-CN", "da", "nl", "en-AU", "en-CA", "en-NZ", "en-ZA", "en-GB", "en-US", "eo", "fr", "gl", "de-DE", "de-AU", "de-CH", "el", "he", "is", "it", "ja", "km", "lt", "ml", "nb-NO", "nn-NO", "fa", "pl", "pt-BR", "pt-PT", "ro", "ru", "tr", "sr-SP-Cy", "sr-SP-Lt", "sk", "sl", "es", "sv", "ta", "tl", "uk"],
        "papersizes": ["A4", "US Letter"],
        "template": "Standard Article",
        "papersize": "A4",
        "language": "af-ZA",
        "bibliography_header": {}
    }
}
'''


DOC_TEMPLATE_HASH = '9oh5bHynenFdZxCA5NvXVQ'


DEFAULT_TEMPLATE_ID = 1


def create_default_template(apps, schema_editor):
    global DEFAULT_TEMPLATE_ID
    Document = apps.get_model('document', 'Document')
    document = Document.objects.first()
    if not document:
        # There are no documents in the system yet. CitationStyles/
        # DocumentStyles will not have been created yet either. We therefore do
        # not need to create any DocumentTemplates just yet. A template will
        # be created automatically when creating the first Document.
        return
    DocumentTemplate = apps.get_model('document', 'DocumentTemplate')
    template = DocumentTemplate.objects.first()
    if template:
        DEFAULT_TEMPLATE_ID = template.pk
        return
    template = DocumentTemplate()
    template.definition = DOC_TEMPLATE
    template.definition_hash = DOC_TEMPLATE_HASH
    template.title = 'Standard Article'
    template.save()
    CitationStyle = apps.get_model('style', 'CitationStyle')
    for style in CitationStyle.objects.all():
        template.citation_styles.add(style)
    DocumentStyle = apps.get_model('style', 'DocumentStyle')
    for style in DocumentStyle.objects.all():
        template.document_styles.add(style)
    ExportTemplate = apps.get_model('document', 'ExportTemplate')
    for exporter in ExportTemplate.objects.all():
        template.export_templates.add(exporter)
    DEFAULT_TEMPLATE_ID = template.pk
    return


class Migration(migrations.Migration):

    dependencies = [
        ('document', '0004_documenttemplate'),
    ]

    operations = [
        migrations.RunPython(create_default_template),
        migrations.AddField(
            model_name='document',
            name='template',
            field=models.ForeignKey(default=DEFAULT_TEMPLATE_ID, on_delete=django.db.models.deletion.CASCADE, to='document.DocumentTemplate'),
        ),
    ]
