<commit_before>from django.core.management.base import BaseCommand, CommandError

from object_log.models import LogItem, LogAction

class Command(BaseCommand):
    args = '[LOG_KEY LOG_KEY ...]'
    help = 'Rebuilds object log cache for the given log types.'

    def handle(self, *args, **options):
        if len(args) == 0:
            args = LogAction.objects.all().values_list('name', flat=True)
        map(rebuild_cache, args)


def rebuild_cache(key):
    """
    Rebuild the log cache for all entries of the given type.  If the type has no
    cache builder then it is ignored    
    """
    
    action = LogAction.objects.get_from_cache(key)
    if action.build_cache is None:
        return
    
    for entry in action.entries.all().select_related('user').iterator():
        entry.data = action.build_cache(entry.user,
                                        entry.object1,
                                        entry.object2,
                                        entry.object3,
                                        entry.data)
        entry.save(force_update=True)
        print '.'<commit_msg>Refactoring the rebuild_log_cache command so that a full rebuild can be called via the function<commit_after>from django.core.management.base import BaseCommand, CommandError

from object_log.models import LogItem, LogAction

class Command(BaseCommand):
    args = '[LOG_KEY LOG_KEY ...]'
    help = 'Rebuilds object log cache for the given log types.'

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


def rebuild_cache(*args):
    if len(args):
        args = LogAction.objects.all().values_list('name', flat=True)
        map(rebuild_cache, args)


def _rebuild_cache(key):
    """
    Rebuild the log cache for all entries of the given type.  If the type has no
    cache builder then it is ignored    
    """
    
    action = LogAction.objects.get_from_cache(key)
    if action.build_cache is None:
        return
    
    for entry in action.entries.all().select_related('user').iterator():
        entry.data = action.build_cache(entry.user,
                                        entry.object1,
                                        entry.object2,
                                        entry.object3,
                                        entry.data)
        entry.save(force_update=True)
        print '.'
{'fraction_non_alphanumeric': 0.08929327126534067, 'fraction_numerical': 0.0029623360135421074, 'mean_word_length': 1.9113300492610839, 'pattern_counts': {'":': 0, '<': 3, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4327230', 'n_tokens_mistral': 618, 'n_tokens_neox': 605, 'n_words': 176}