<commit_before>import datetime

# Django specific
from django.core.management.base import BaseCommand
from django.db import connection
from iati.models import Activity, Budget
import logging

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    option_list = BaseCommand.option_list
    counter = 0

    def handle(self, *args, **options):
        parser = TotalBudgetUpdater()
        parser.updateTotal()


class TotalBudgetUpdater():

    def get_fields(self, cursor):
        desc = cursor.description
        results = [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
        ]
        return results

    def update(self):

        cursor = connection.cursor()
        cursor.execute('SELECT activity_id, sum(value) as total_value FROM IATI_budget b GROUP BY activity_id')

        results = self.get_fields(cursor=cursor)
        for r in results:
            cur_act = Activity.objects.get(id=r['activity_id'])
            cur_act.total_budget = r['total_value']
            cur_act.save()

        return True


    def update_single_activity(self, id):

        try:
            cursor = connection.cursor()

            cursor.execute("SELECT activity_id, sum(value) as total_value FROM IATI_budget b WHERE activity_id ='" + id + "' GROUP BY activity_id")

            results = self.get_fields(cursor=cursor)
            for r in results:
                cur_act = Activity.objects.get(id=r['activity_id'])
                cur_act.total_budget = r['total_value']
                cur_act.save()
        except Exception as e:
            logger.info("error in " + id + ", def: update_single_activity")
            if e.args:
                logger.info(e.args[0])
            if e.args.__len__() > 1:
                logger.info(e.args[1])
            if e.message:<commit_msg>Budget updater update_single_activity bug fix<commit_after>import datetime

# Django specific
from django.core.management.base import BaseCommand
from django.db import connection
from iati.models import Activity, Budget
import logging

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    option_list = BaseCommand.option_list
    counter = 0

    def handle(self, *args, **options):
        parser = TotalBudgetUpdater()
        parser.updateTotal()


class TotalBudgetUpdater():

    def get_fields(self, cursor):
        desc = cursor.description
        results = [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
        ]
        return results

    def update(self):

        cursor = connection.cursor()
        cursor.execute('SELECT activity_id, sum(value) as total_value FROM IATI_budget b GROUP BY activity_id')

        results = self.get_fields(cursor=cursor)
        for r in results:
            cur_act = Activity.objects.get(id=r['activity_id'])
            cur_act.total_budget = r['total_value']
            cur_act.save()

        return True


    def update_single_activity(self, id):

        try:
            cursor = connection.cursor()

            cursor.execute("SELECT activity_id, sum(value) as total_value FROM iati_budget b WHERE activity_id ='" + id + "' GROUP BY activity_id")

            results = self.get_fields(cursor=cursor)
            for r in results:
                cur_act = Activity.objects.get(id=r['activity_id'])
                cur_act.total_budget = r['total_value']
                cur_act.save()
        except Exception as e:
            logger.info("error in " + id + ", def: update_single_activity")
            if e.args:
                logger.info(e.args[0])
            if e.args.__len__() > 1:
                logger.info(e.args[1])
            if e.message:
{'fraction_non_alphanumeric': 0.09731182795698924, 'fraction_numerical': 0.002688172043010753, 'mean_word_length': 2.3016858917480034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5558626', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1033, 'n_words': 265}