<commit_before># -*- coding: utf-8 -*-

# Akvo Reporting is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

import tablib

from django.conf import settings
from django.core.management.base import BaseCommand

from optparse import make_option

from akvo.rsr.models import IndicatorPeriodData
from akvo.utils import single_period_dates
from ...models import Project, Indicator, IndicatorPeriod


class Command(BaseCommand):
    args = '<name> <all | ids of projects>'
    help = ('Convert a project hierarchy to use single period reporting')

    option_list = BaseCommand.option_list + (
        make_option('--live',
                    action='store_true',
                    dest='live',
                    default=False,
                    help='Live run, changes made to the models'),
    )

    def handle(self, *args, **options):

        def set_period_dates(period):
            period.period_start = PERIOD_START
            period.period_end = PERIOD_END
            period.save()

        def get_parent_periods(indicator, PERIOD_START, PERIOD_END):
            return indicator.parent_indicator.periods.filter(
                period_start=PERIOD_START, period_end=PERIOD_END
            )

        def find_periods_with_data(periods):
            """ Separate a list of periods into two groups: those tht have been modified from an
            original, "empty" state and those that haven't. To be counted as modified a period has
            data in at least one of the fields target_value, target_comment, actual_value or
            actual_comment or has at least one update.
            """
            modified_periods = []
            unmodified_periods = []
            for period in periods:
                updates = IndicatorPeriodData.objects.filter(period=period)
                if (period.target_value or period.target_comment or
                        period.actual_value or period.actual_comment or updates):<commit_msg>[#3278] Fix small performance problem<commit_after># -*- coding: utf-8 -*-

# Akvo Reporting is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

import tablib

from django.conf import settings
from django.core.management.base import BaseCommand

from optparse import make_option

from akvo.rsr.models import IndicatorPeriodData
from akvo.utils import single_period_dates
from ...models import Project, Indicator, IndicatorPeriod


class Command(BaseCommand):
    args = '<name> <all | ids of projects>'
    help = ('Convert a project hierarchy to use single period reporting')

    option_list = BaseCommand.option_list + (
        make_option('--live',
                    action='store_true',
                    dest='live',
                    default=False,
                    help='Live run, changes made to the models'),
    )

    def handle(self, *args, **options):

        def set_period_dates(period):
            period.period_start = PERIOD_START
            period.period_end = PERIOD_END
            period.save()

        def get_parent_periods(indicator, PERIOD_START, PERIOD_END):
            return indicator.parent_indicator.periods.filter(
                period_start=PERIOD_START, period_end=PERIOD_END
            )

        def find_periods_with_data(periods):
            """ Separate a list of periods into two groups: those tht have been modified from an
            original, "empty" state and those that haven't. To be counted as modified a period has
            data in at least one of the fields target_value, target_comment, actual_value or
            actual_comment or has at least one update.
            """
            modified_periods = []
            unmodified_periods = []
            for period in periods:
                updates_exist = IndicatorPeriodData.objects.filter(period=period).exists()
                if (period.target_value or period.target_comment or
                        period.actual_value or period.actual_comment or updates_exist):
{'fraction_non_alphanumeric': 0.0718728403593642, 'fraction_numerical': 0.00138217000691085, 'mean_word_length': 2.451510333863275, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5523662', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1103, 'n_words': 379}