# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-02 17:45
from __future__ import unicode_literals
from waffle.models import Flag
from django.db import migrations, IntegrityError, transaction

EMBER_WAFFLE_PAGES = [
    'completed_registration_form_detail',
    'dashboard',
    'draft_registration_form',
    'file_detail',
    'home_logged_out',
    'meeting_detail',
    'meetings',
    'my_projects',
    'prereg_onboarder',
    'project_analytics',
    'project_contributors',
    'project_detail',
    'project_files',
    'project_forks',
    'project_registrations',
    'project_settings',
    'project_wiki',
    'registration_detail',
    'search',
    'support',
    'user_profile',
    'user_settings'
]

def reverse_func(state, schema):
    pages = [format_ember_waffle_flag_name(page) for page in EMBER_WAFFLE_PAGES]
    Flag.objects.filter(name__in=pages).delete()
    return

def format_ember_waffle_flag_name(page):
    return '{}{}{}'.format('ember_', page, '_page')

def add_ember_waffle_flags(state, schema):
    """
    This migration adds some waffle flags for pages that are being emberized.
    Waffle flags are used for feature flipping, for example, showing an
    emberized page to one set of users, and the existing osf page for another set.

    By default, flags are given an everyone=False value, which overrides all other settings,
    making the flag False for everyone.  Flag settings can be changed in the Django admin app.
    """
    for page in EMBER_WAFFLE_PAGES:
        try:
            with transaction.atomic():
                Flag.objects.create(name=format_ember_waffle_flag_name(page), everyone=False)
        except IntegrityError as e:
            # just in case someone has already added a flag with this name to the db
            pass
    return

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0082_merge_20180213_1502'),
    ]

    operations = [
        migrations.RunPython(add_ember_waffle_flags, reverse_func)
    ]

Add migration to add some ember waffle flags to the db for waffle pages with a default value of False. Can be changed in django app when ready.
Add migration to add some ember waffle flags to the db for waffle pages with a default value of False. Can be changed in django app when ready.
