Esempio n. 1
0
def move_avatars(stdout, style):
    for old_user in fetch_assoc('SELECT * FROM misago_user ORDER BY id'):
        user = UserModel.objects.get(pk=movedids.get('user', old_user['id']))

        if old_user['avatar_ban'] or old_user['avatar_type'] == 'gallery':
            dynamic.set_avatar(user)
        else:
            if old_user['avatar_type'] == 'gravatar':
                try:
                    gravatar.set_avatar(user)
                except gravatar.GravatarError:
                    dynamic.set_avatar(user)
                    print_warning('%s: failed to download Gravatar' % user,
                                  stdout, style)
            else:
                try:
                    if not old_user['avatar_original'] or not old_user[
                            'avatar_crop']:
                        raise ValidationError("Invalid avatar upload data.")

                    image_path = os.path.join(OLD_FORUM['MEDIA'], 'avatars',
                                              old_user['avatar_original'])
                    image = uploaded.validate_dimensions(image_path)

                    cleaned_crop = convert_crop(image, old_user)
                    uploaded.clean_crop(image, cleaned_crop)

                    store.store_temporary_avatar(user, image)
                    uploaded.crop_source_image(user, 'tmp', cleaned_crop)
                except ValidationError as e:
                    dynamic.set_avatar(user)
                    print_warning('%s: %s' % (user, e.args[0]), stdout, style)

        user.save()
    def test_clean_crop(self):
        """crop validation and cleaning"""
        image = Image.new("RGBA", (200, 200), 0)
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, "abc")
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {})
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {'offset': {'x': 'ugabuga'}})

        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {
                'offset': {
                    'x': 0,
                    'y': 0,
                },
                'zoom': -2
            })

        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {
                'offset': {
                    'x': 0,
                    'y': 0,
                },
                'zoom': 2
            })
    def test_clean_crop(self):
        """crop validation and cleaning"""
        image = Image.new("RGBA", (200, 200), 0)
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, "abc")
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {})
        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {'offset': {'x': 'ugabuga'}})

        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {'offset': {
                'x': 0,
                'y': 0,
            },
                                        'zoom': -2})

        with self.assertRaises(ValidationError):
            uploaded.clean_crop(image, {'offset': {
                'x': 0,
                'y': 0,
            },
                                        'zoom': 2})