from __future__ import unicode_literals

from django.db import migrations
from osf.models import RegistrationSchema

OLD_NAME = 'AsPredicted Preregistration'
NEW_NAME = 'Preregistration Template from AsPredicted.org'

def rename_schema(from_name, to_name):
    try:
        schema = RegistrationSchema.objects.get(name=from_name)
    except RegistrationSchema.DoesNotExist:
        return

    schema.name = to_name
    schema.schema['name'] = to_name
    schema.schema['title'] = to_name
    schema.schema['pages'][0]['title'] = to_name
    return schema.save()

def rename_aspredicted_schema(*args, **kwargs):
    return rename_schema(OLD_NAME, NEW_NAME)

def undo_aspredicted_rename(*args, **kwargs):
    return rename_schema(NEW_NAME, OLD_NAME)

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0138_merge_20181012_1944'),
    ]

    operations = [
        migrations.RunPython(rename_aspredicted_schema, undo_aspredicted_rename)
    ]

from __future__ import unicode_literals

from django.db import migrations

OLD_NAME = 'AsPredicted Preregistration'
NEW_NAME = 'Preregistration Template from AsPredicted.org'

def rename_schema(model, from_name, to_name):
    try:
        schema = model.objects.get(name=from_name)
    except model.DoesNotExist:
        return

    schema.name = to_name
    schema.schema['name'] = to_name
    schema.schema['title'] = to_name
    schema.schema['pages'][0]['title'] = to_name
    return schema.save()

def rename_aspredicted_schema(state, schema):
    RegistrationSchema = state.get_model('osf.registrationschema')
    return rename_schema(RegistrationSchema, OLD_NAME, NEW_NAME)

def undo_aspredicted_rename(state, schema):
    RegistrationSchema = state.get_model('osf.registrationschema')
    return rename_schema(RegistrationSchema, NEW_NAME, OLD_NAME)

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0138_merge_20181012_1944'),
    ]

    operations = [
        migrations.RunPython(rename_aspredicted_schema, undo_aspredicted_rename)
    ]

Migrate with model from app state - Not from app code
Migrate with model from app state
- Not from app code

  from __future__ import unicode_literals
  
  from django.db import migrations
- from osf.models import RegistrationSchema
  
  OLD_NAME = 'AsPredicted Preregistration'
  NEW_NAME = 'Preregistration Template from AsPredicted.org'
  
- def rename_schema(from_name, to_name):
+ def rename_schema(model, from_name, to_name):
      try:
-         schema = RegistrationSchema.objects.get(name=from_name)
+         schema = model.objects.get(name=from_name)
-     except RegistrationSchema.DoesNotExist:
+     except model.DoesNotExist:
          return
  
      schema.name = to_name
      schema.schema['name'] = to_name
      schema.schema['title'] = to_name
      schema.schema['pages'][0]['title'] = to_name
      return schema.save()
  
- def rename_aspredicted_schema(*args, **kwargs):
+ def rename_aspredicted_schema(state, schema):
+     RegistrationSchema = state.get_model('osf.registrationschema')
-     return rename_schema(OLD_NAME, NEW_NAME)
+     return rename_schema(RegistrationSchema, OLD_NAME, NEW_NAME)
  
- def undo_aspredicted_rename(*args, **kwargs):
+ def undo_aspredicted_rename(state, schema):
+     RegistrationSchema = state.get_model('osf.registrationschema')
-     return rename_schema(NEW_NAME, OLD_NAME)
+     return rename_schema(RegistrationSchema, NEW_NAME, OLD_NAME)
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0138_merge_20181012_1944'),
      ]
  
      operations = [
          migrations.RunPython(rename_aspredicted_schema, undo_aspredicted_rename)
      ]
  
Migrate with model from app state - Not from app code
## Code Before:
from __future__ import unicode_literals

from django.db import migrations
from osf.models import RegistrationSchema

OLD_NAME = 'AsPredicted Preregistration'
NEW_NAME = 'Preregistration Template from AsPredicted.org'

def rename_schema(from_name, to_name):
    try:
        schema = RegistrationSchema.objects.get(name=from_name)
    except RegistrationSchema.DoesNotExist:
        return

    schema.name = to_name
    schema.schema['name'] = to_name
    schema.schema['title'] = to_name
    schema.schema['pages'][0]['title'] = to_name
    return schema.save()

def rename_aspredicted_schema(*args, **kwargs):
    return rename_schema(OLD_NAME, NEW_NAME)

def undo_aspredicted_rename(*args, **kwargs):
    return rename_schema(NEW_NAME, OLD_NAME)

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0138_merge_20181012_1944'),
    ]

    operations = [
        migrations.RunPython(rename_aspredicted_schema, undo_aspredicted_rename)
    ]

## Instruction:
Migrate with model from app state - Not from app code
## Code After:
from __future__ import unicode_literals

from django.db import migrations

OLD_NAME = 'AsPredicted Preregistration'
NEW_NAME = 'Preregistration Template from AsPredicted.org'

def rename_schema(model, from_name, to_name):
    try:
        schema = model.objects.get(name=from_name)
    except model.DoesNotExist:
        return

    schema.name = to_name
    schema.schema['name'] = to_name
    schema.schema['title'] = to_name
    schema.schema['pages'][0]['title'] = to_name
    return schema.save()

def rename_aspredicted_schema(state, schema):
    RegistrationSchema = state.get_model('osf.registrationschema')
    return rename_schema(RegistrationSchema, OLD_NAME, NEW_NAME)

def undo_aspredicted_rename(state, schema):
    RegistrationSchema = state.get_model('osf.registrationschema')
    return rename_schema(RegistrationSchema, NEW_NAME, OLD_NAME)

class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0138_merge_20181012_1944'),
    ]

    operations = [
        migrations.RunPython(rename_aspredicted_schema, undo_aspredicted_rename)
    ]
