<commit_before># -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-10-07 17:05
from __future__ import unicode_literals
from django.db import migrations


def reorder_actions_sequence_values(apps, schema_editor):
    """
    We're gonna get rid of Behaviors, however, Action's sequence orders
    are based on their parent behavior. So... let's revise those values
    so all actions retain the same order.

    """
    Action = apps.get_model("goals", "Action")
    Behavior = apps.get_model("goals", "Behavior")

    previous = 0
    for behavior in Behavior.objects.all().order_by('sequence_order'):
        actions = Action.objects.filter(behavior__id=behavior.id).order_by('sequence_order')
        for action in actions:
            if action.sequence_order < previous:
                action.sequence_order = previous
                action.save()
            previous = max([action.sequence_order, previous])


class Migration(migrations.Migration):

    dependencies = [
        ('goals', '0177_remove_dailyprogress_behaviors_total'),
    ]<commit_msg>revising how actions are ordered within goals<commit_after># -*- coding: utf-8 -*-
# Generated by Django 1.9.9 on 2016-10-07 17:05
from __future__ import unicode_literals
from django.db import migrations


def reorder_actions_sequence_values(apps, schema_editor):
    """
    We're gonna get rid of Behaviors, however, Action's sequence orders
    are based on their parent behavior. So... let's revise those values
    so all actions retain the same order.

    """
    Action = apps.get_model("goals", "Action")
    Behavior = apps.get_model("goals", "Behavior")
    Category = apps.get_model("goals", "Category")

    for cat in Category.objects.all():
        for goal in cat.goal_set.all().order_by('sequence_order'):

            prev_behavior = 0
            max_action_seq = 0

            behaviors = Behavior.objects.filter(goals__pk=goal.id)
            for behavior in behaviors.order_by('sequence_order'):

                offset = 0
                if behavior.sequence_order > prev_behavior:
                    offset = max_action_seq

                actions = Action.objects.filter(behavior__id=behavior.id)
                for action in actions.order_by('sequence_order'):

                    if behavior.sequence_order > prev_behavior:
                        action.sequence_order += offset
                        action.save()

                    if action.sequence_order > max_action_seq:
                        max_action_seq = action.sequence_order

                prev_behavior = behavior.sequence_order


class Migration(migrations.Migration):

    dependencies = [
        ('goals', '0177_remove_dailyprogress_behaviors_total'),
    ]
{'fraction_non_alphanumeric': 0.09360146252285192, 'fraction_numerical': 0.016087751371115174, 'mean_word_length': 2.581151832460733, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7249437', 'n_tokens_mistral': 802, 'n_tokens_neox': 718, 'n_words': 196}