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

from django.core.management.base import BaseCommand

from storage import S3OverlayStorage, migrate_to_s3
from users.models import UserProfile


class Command(BaseCommand):
    help = """Upload user avatars to S3 cloud.
    Won't delete files from local storage.
    """

    def add_arguments(self, parser):
        parser.add_argument(
            '--execute',
            dest='execute',
            action='store_true',
            default=False,
            help='Actually execute migration')
        '''
        parser.add_argument(
            '--reverse',
            dest='reverse',
            action='store_true',
            default=False,
            help='DEV ONLY')
        '''

    def handle(self, **options):
        dry_run = not options['execute']

        if dry_run:
            print("NOTE: Dry run")
        dest_storage = S3OverlayStorage()

        for user_model in UserProfile.objects.all():
            if not user_model.avatar:
                continue
            try:
                new_path = migrate_to_s3(user_model.avatar, dest_storage, dry_run)
                print("Note: uploaded: {}".format(new_path))
            except KeyError as e:
                new_path = e.message
                print("Note: destination already exists: {}".format(new_path))
            except Exception as e:
                print("Unhandled exception, ignoring: {}".format(e))
                continue
            if not dry_run:
                user_model.avatar.name = new_path
                user_model.save()<commit_msg>CST-27 Use common.s3_migrate in s3migrate_user_avatars<commit_after># -*- coding: utf-8 -*-

from common.s3_migrate import S3MigrateCommand
from users.models import UserProfile


class Command(S3MigrateCommand):
    help = """
        Upload user avatars to S3 cloud.
        Won't delete files from local storage.
        """

    def all_models(self, options):
        return UserProfile.objects.all()

    def get_model_field(self, model, options):
        return model.avatar

    def update_model(self, model, new_path, options):
        try:
            model.avatar.name = new_path
            model.save()
        except Exception as e:
            print('Unhandled exception in {}: {}'.format(self.update_model, e))
            return False
        else:
            return True
{'fraction_non_alphanumeric': 0.09780775716694773, 'fraction_numerical': 0.006323777403035413, 'mean_word_length': 1.9441687344913152, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7318676', 'n_tokens_mistral': 663, 'n_tokens_neox': 640, 'n_words': 167}