<gh_stars>10-100
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2017-06-11 18:50
from __future__ import unicode_literals

import json

from django.db import migrations


def remove_preferences_permanent_purchases(apps, schema_editor):
    for account in apps.get_model("accounts", "Account").objects.all():

        permanent_purchases = json.loads(account.permanent_purchases)

        purchases_count = len(set(permanent_purchases) - {12})

        permanent_purchases = list(set(permanent_purchases) & {12})

        account.permanent_purchases = json.dumps(permanent_purchases, ensure_ascii=False)

        account.save()

        for i in range(purchases_count):
            apps.get_model("accounts", "Award").objects.create(account=account, type=7, description='Компенсация вечных покупок')


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0007_account_gender'),
    ]

    operations = [
        migrations.RunPython(remove_preferences_permanent_purchases)
    ]
