text: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.16 on 2018-12-06 22:04
  from __future__ import unicode_literals
  
  from decimal import Decimal
  from django.db import migrations
  from django.conf import settings
  
  
  def handle_premium_users(apps, schema_editor):
      """
      All LinkUsers who are currently paying belong to the "unlimited" tier:
      self.is_unlimited should be True
      """
      LinkUser = apps.get_model('perma', 'LinkUser')
      premiun_users = LinkUser.objects.filter(nonpaying=False)
      premiun_users.update(unlimited=True)
  
  
  def ensure_all_users_have_nonzero_price(apps, schema_editor):
      """
      All customers who currently have a price should keep it.
      All others should get the default price.
      """
      LinkUser = apps.get_model('perma', 'LinkUser')
      users_without_prices = LinkUser.objects.filter(base_rate=Decimal('0.00'))
      users_without_prices.update(base_rate=Decimal(settings.DEFAULT_BASE_RATE))
  
  
  def ensure_all_registrars_have_nonzero_price(apps, schema_editor):
      """
      All customers who currently have a price should keep it.
      All others should get the default price.
      """
      Registrar = apps.get_model('perma', 'Registrar')
      registrars_without_prices = Registrar.objects.filter(base_rate=Decimal('0.00'))
      registrars_without_prices.update(base_rate=Decimal(settings.DEFAULT_BASE_RATE_REGISTRAR))
  
  
  def ensure_all_users_can_subscribe(apps, schema_editor):
      """
      All users should have the option to purchase a personal subscription.
      We can manually adjust any exceptions.
      """
      LinkUser = apps.get_model('perma', 'LinkUser')
      nonpaying_users = LinkUser.objects.filter(nonpaying=True)
      nonpaying_users.update(nonpaying = False)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('perma', '0038_auto_20181206_2141'),
      ]
  
      operations = [
          migrations.RunPython(handle_premium_users, migrations.RunPython.noop),
          migrations.RunPython(ensure_all_users_have_nonzero_price, migrations.RunPython.noop),
          migrations.RunPython(ensure_all_registrars_have_nonzero_price, migrations.RunPython.noop),
          migrations.RunPython(ensure_all_users_can_subscribe, migrations.RunPython.noop)
      ]
  