<commit_before># from pprint import pprint
import difflib

from utils.classes import LoggedInUser
from geral.models import RecordTracking


def post_init_tracking(sender, instance, **kwargs):
    '''
        Signal post_init to track record changes, if record has id field.
        Work in conjunction with post_save_tracking.
    '''
    if hasattr(instance, 'id'):
        original = {}
        for field in sender._meta.get_fields():
            if hasattr(instance, field.name):
                original[field.name] = getattr(instance, field.name)
        setattr(instance, '__original_record_values', original)


def post_save_tracking(sender, instance, **kwargs):
    '''
        Signal post_save to track record changes, if record has id field.
        Work in conjunction with post_init_tracking.
    '''
    if hasattr(instance, '__original_record_values'):
        logged_in = LoggedInUser()
        user = logged_in.user
        # print('user = {}'.format(user))
        original = getattr(instance, '__original_record_values')
        if original['id'] == getattr(instance, 'id'):
            altered = {}
            for k in original:
                old = original[k]
                new = getattr(instance, k)
                if k == 'id' or new != old:
                    altered[k] = new
                    if new is not None and old is not None and \
                            (isinstance(old, str) or isinstance(new, str)):
                        old_s = old.splitlines(keepends=True)
                        new_s = new.splitlines(keepends=True)

                        diff = difflib.unified_diff(
                            old_s, new_s, n=0, lineterm='\r\n')
                        lindiff = ''.join([l for l in diff])

                        # the "+10" is to compensate the ".__delta__"
                        if (len(lindiff)+10) < len(new):
                            altered[k+'.__delta__'] = lindiff
            # print('{} altered = {}'.format(sender.__name__, altered))
            rt = RecordTracking()
            rt.user = user
            rt.table = sender.__name__
            rt.record_id = altered['id']
            rt.iud = 'u'
            rt.log = altered
            rt.save()
            # print('rt.user = {}'.format(rt.user))
        else:
            record = {}
            for field in sender._meta.get_fields():
                if hasattr(instance, field.name):
                    record[field.name] = getattr(instance, field.name)<commit_msg>bug gravava campo alterado string 2 vezes: inteiro e delta<commit_after># from pprint import pprint
import difflib

from utils.classes import LoggedInUser
from geral.models import RecordTracking


def post_init_tracking(sender, instance, **kwargs):
    '''
        Signal post_init to track record changes, if record has id field.
        Work in conjunction with post_save_tracking.
    '''
    if hasattr(instance, 'id'):
        original = {}
        for field in sender._meta.get_fields():
            if hasattr(instance, field.name):
                original[field.name] = getattr(instance, field.name)
        setattr(instance, '__original_record_values', original)


def post_save_tracking(sender, instance, **kwargs):
    '''
        Signal post_save to track record changes, if record has id field.
        Work in conjunction with post_init_tracking.
    '''
    if hasattr(instance, '__original_record_values'):
        logged_in = LoggedInUser()
        user = logged_in.user
        # print('user = {}'.format(user))
        original = getattr(instance, '__original_record_values')
        if original['id'] == getattr(instance, 'id'):
            altered = {}
            for k in original:
                old = original[k]
                new = getattr(instance, k)
                if k == 'id' or new != old:
                    if new is not None and old is not None and \
                            (isinstance(old, str) or isinstance(new, str)):
                        old_s = old.splitlines(keepends=True)
                        new_s = new.splitlines(keepends=True)

                        diff = difflib.unified_diff(
                            old_s, new_s, n=0, lineterm='\r\n')
                        lindiff = ''.join([l for l in diff])

                        # the "+10" is to compensate the ".__delta__"
                        if (len(lindiff)+10) < len(new):
                            altered[k+'.__delta__'] = lindiff
                        else:
                            altered[k] = new
                    else:
                        altered[k] = new
            # print('{} altered = {}'.format(sender.__name__, altered))
            rt = RecordTracking()
            rt.user = user
            rt.table = sender.__name__
            rt.record_id = altered['id']
            rt.iud = 'u'
            rt.log = altered
            rt.save()
            # print('rt.user = {}'.format(rt.user))
        else:
            record = {}
            for field in sender._meta.get_fields():
                if hasattr(instance, field.name):
                    record[field.name] = getattr(instance, field.name)
{'fraction_non_alphanumeric': 0.10546723536254363, 'fraction_numerical': 0.0021326095385808454, 'mean_word_length': 1.6415770609318996, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4471828', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1374, 'n_words': 374}