# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-08-17 00:49
from __future__ import unicode_literals

from django.db import migrations, models

from django.db import transaction


def extract_notification_response_count_data_migration(apps, schema_editor):
    ActionLog = apps.get_model('nurture', 'ActionLog')

    with transaction.atomic():
        for act in ActionLog.objects.all():
            reward_text = act.reward_state_message.split(';')[0][1:-1]
            num_accepted = 0
            num_dismissed = 0
            if len(reward_text) > 0:
                try:
                    terms = reward_text.split(',')
                    for t in terms:
                        v = float(t.split(':')[0])
                        if v > 0.:
                            num_accepted += 1
                        elif v < 0.:
                            num_dismissed += 1
                except:
                    pass
            act.num_accepted = num_accepted
            act.num_dismissed = num_dismissed
            act.save()


class Migration(migrations.Migration):

    dependencies = [
        ('nurture', '0016_auto_20180809_1806'),
    ]

    operations = [
        migrations.AddField(
            model_name='actionlog',
            name='num_accepted',
            field=models.IntegerField(default=0),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='actionlog',
            name='num_dismissed',
            field=models.IntegerField(default=0),
            preserve_default=False,
        ),
        migrations.RunPython(extract_notification_response_count_data_migration),
    ]
