# Generated by Django 3.1 on 2021-02-03 21:39

from django.db import migrations, models
from django.utils.text import slugify

import websites.models


COURSE_STARTER_SLUG = "course"
COURSE_STARTER_REPO_URL = "https://github.com/mitodl/ocw-course-hugo-starter"
COURSE_STARTER_REPO_NAME = "OCW Course Hugo Starter"
STARTER_SOURCE_GITHUB = "github"
STARTER_CONFIG = """
collections:
  - label: "Page"
    name: "page"
    fields:
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Content", name: "content", widget: "markdown"}
      
  - label: "Resource"
    name: "resource"
    fields:
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Description", name: "description", widget: "markdown"}
"""


def add_first_starter_repo(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter, created = WebsiteStarter.objects.get_or_create(
        path=COURSE_STARTER_REPO_URL,
        defaults=dict(
            slug=COURSE_STARTER_SLUG,
            name=COURSE_STARTER_REPO_NAME,
            source=STARTER_SOURCE_GITHUB,
            commit=None,
            config=STARTER_CONFIG,
        ),
    )
    if created is False and starter.slug is None:
        starter.slug = COURSE_STARTER_SLUG
        starter.save()


def fill_in_slug_values(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starters = WebsiteStarter.objects.filter(slug=None)
    for starter in starters:
        starter.slug = slugify(starter.name)[0:30]
        starter.save()


class Migration(migrations.Migration):

    dependencies = [
        ("websites", "0003_add_website_starter_model"),
    ]

    operations = [
        migrations.AddField(
            model_name="websitestarter",
            name="slug",
            field=models.CharField(
                help_text="Short string that can be used to identify this starter.",
                max_length=30,
                null=True,
            ),
        ),
        migrations.RunPython(add_first_starter_repo, migrations.RunPython.noop),
        migrations.RunPython(fill_in_slug_values, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="websitestarter",
            name="slug",
            field=models.CharField(
                help_text="Short string that can be used to identify this starter.",
                max_length=30,
                unique=True,
                validators=[websites.models.validate_slug],
            ),
        ),
    ]

# Generated by Django 3.1 on 2021-02-03 21:39

from django.db import migrations, models
from django.utils.text import slugify

import websites.models


COURSE_STARTER_SLUG = "course"
COURSE_STARTER_REPO_URL = "https://github.com/mitodl/ocw-course-hugo-starter"
COURSE_STARTER_REPO_NAME = "OCW Course Hugo Starter"
STARTER_SOURCE_GITHUB = "github"
STARTER_CONFIG = """
collections:
  - label: "Page"
    name: "page"
    fields:
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Content", name: "content", widget: "markdown"}
      
  - label: "Resource"
    name: "resource"
    fields:
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Description", name: "description", widget: "markdown"}
"""


def add_first_starter_repo(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starter, created = WebsiteStarter.objects.get_or_create(
        path=COURSE_STARTER_REPO_URL,
        defaults=dict(
            slug=COURSE_STARTER_SLUG,
            name=COURSE_STARTER_REPO_NAME,
            source=STARTER_SOURCE_GITHUB,
            commit=None,
            config=STARTER_CONFIG,
        ),
    )
    if created is False and starter.slug is None:
        starter.slug = COURSE_STARTER_SLUG
        starter.save()


def fill_in_slug_values(apps, schema_editor):
    WebsiteStarter = apps.get_model("websites", "WebsiteStarter")
    starters = WebsiteStarter.objects.filter(slug=None)
    for starter in starters:
        starter.slug = slugify(starter.name)[0:30]
        starter.save()


class Migration(migrations.Migration):

    dependencies = [
        ("websites", "0003_add_website_starter_model"),
    ]

    operations = [
        migrations.AddField(
            model_name="websitestarter",
            name="slug",
            field=models.CharField(
                help_text="Short string that can be used to identify this starter.",
                max_length=30,
                null=True,
            ),
        ),
        migrations.RunPython(add_first_starter_repo, migrations.RunPython.noop),
        migrations.RunPython(fill_in_slug_values, migrations.RunPython.noop),
        migrations.AlterField(
            model_name="websitestarter",
            name="slug",
            field=models.CharField(
                help_text="Short string that can be used to identify this starter.",
                max_length=30,
                unique=True,
                validators=[websites.models.validate_slug],
            ),
        ),
    ]

# Generated by Django 3.1 on 2021-02-03 21:39 collections:   - label: "Page"     name: "page"     fields:       - {label: "Title", name: "title", widget: "string"}       - {label: "Content", name: "content", widget: "markdown"}          - label: "Resource"     name: "resource"     fields:       - {label: "Title", name: "title", widget: "string"}       - {label: "Description", name: "description", widget: "markdown"}