<gh_stars>1-10
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-10-25 18:21
from __future__ import unicode_literals

from django.db import migrations


REDIRECTS = {
    "/candidates/martin-quezeda": "martin-quezada",
    "/candidates/elizabeth-thompson": "elizabeth-thomson",
    "/stop-the-tpp": "stop-tpp-now",
    "/volunteer-signup": "action",
    "/signup": "/",
    "/candidate-questionnaire": "candidates",
    "/august-30-primary-candidates": "candidates",
    "/tpp": "https://go.berniesanders.com/page/s/stop-the-TPP?source=webslashtpp",
    "/act": "https://go.berniesanders.com/page/s/volunteer-our-revolution?source=webslashact"
}


def populate_redirects(apps, schema_editor):
    from wagtail.wagtailcore.models import Page, Site
    from wagtail.wagtailredirects.models import Redirect


    for source, target in REDIRECTS.iteritems():
        print "Trying %s / %s ..." % (source, target)
        data = {
            'old_path': source,
            'site': Site.objects.get(is_default_site=True),
            'is_permanent': True,
        }

        if '/' in target:
            data['redirect_link'] = target
        else: 
            data['redirect_page'] = Page.objects.get(slug=target)


        Redirect.objects.create(**data)


def remove_redirects(apps, schema_editor):
    from wagtail.wagtailredirects.models import Redirect
    Redirect.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('pages', '0014_create_stop_tpp_page'),
    ]

    operations = [
        migrations.RunPython(populate_redirects, reverse_code=remove_redirects)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-10-25 18:21
from __future__ import unicode_literals

from django.db import migrations


REDIRECTS = {
    "/candidates/martin-quezeda": "martin-quezada",
    "/candidates/elizabeth-thompson": "elizabeth-thomson",
    "/stop-the-tpp": "stop-tpp-now",
    "/volunteer-signup": "action",
    "/signup": "/",
    "/candidate-questionnaire": "candidates",
    "/august-30-primary-candidates": "candidates",
    "/tpp": "https://go.berniesanders.com/page/s/stop-the-TPP?source=webslashtpp",
    "/act": "https://go.berniesanders.com/page/s/volunteer-our-revolution?source=webslashact"
}


def populate_redirects(apps, schema_editor):
    from wagtail.wagtailcore.models import Page, Site
    from wagtail.wagtailredirects.models import Redirect


    for source, target in REDIRECTS.iteritems():
        print "Trying %s / %s ..." % (source, target)
        data = {
            'old_path': source,
            'site': Site.objects.get(is_default_site=True),
            'is_permanent': True,
        }

        if '/' in target:
            data['redirect_link'] = target
        else: 
            data['redirect_page'] = Page.objects.get(slug=target)


        Redirect.objects.create(**data)


def remove_redirects(apps, schema_editor):
    from wagtail.wagtailredirects.models import Redirect
    Redirect.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('pages', '0014_create_stop_tpp_page'),
    ]

    operations = [
        migrations.RunPython(populate_redirects, reverse_code=remove_redirects)
    ]
# -*- coding: utf-8 -*- # Generated by Django 1.10.2 on 2016-10-25 18:21