Пример #1
0
    def on_updated(self, updates, original):
        """
        Locates the published or corrected non-take packages containing the corrected item
        and corrects them
        :param updates: correction
        :param original: original story
        """
        original_updates = dict()
        original_updates['operation'] = updates['operation']
        original_updates[ITEM_STATE] = updates[ITEM_STATE]
        super().on_updated(updates, original)
        ArchiveCropService().delete_replaced_crop_files(updates, original)
        packages = self.package_service.get_packages(original[config.ID_FIELD])
        if packages and packages.count() > 0:
            archive_correct = get_resource_service('archive_correct')
            processed_packages = []
            for package in packages:
                if package[ITEM_STATE] in [CONTENT_STATE.PUBLISHED, CONTENT_STATE.CORRECTED] and \
                        package.get(PACKAGE_TYPE, '') == '' and \
                        str(package[config.ID_FIELD]) not in processed_packages:
                    original_updates['groups'] = package['groups']

                    if updates.get('headline'):
                        self.package_service.update_field_in_package(
                            original_updates, original[config.ID_FIELD],
                            'headline', updates.get('headline'))

                    if updates.get('slugline'):
                        self.package_service.update_field_in_package(
                            original_updates, original[config.ID_FIELD],
                            'slugline', updates.get('slugline'))

                    archive_correct.patch(id=package[config.ID_FIELD],
                                          updates=original_updates)
                    insert_into_versions(id_=package[config.ID_FIELD])
                    processed_packages.append(package[config.ID_FIELD])
 def setUp(self):
     super().setUp()
     self.service = ArchiveCropService()
     VocabulariesPopulateCommand().run(
         os.path.abspath(
             'apps/prepopulate/data_initialization/vocabularies.json'))
class ArchiveCropTestCase(SuperdeskTestCase):
    def setUp(self):
        super().setUp()
        self.service = ArchiveCropService()
        VocabulariesPopulateCommand().run(
            os.path.abspath(
                'apps/prepopulate/data_initialization/vocabularies.json'))

    def test_validate_aspect_ratio_fails(self):
        doc = {'CropLeft': 0, 'CropRight': 80, 'CropTop': 0, 'CropBottom': 60}
        crop = {'height': 700, 'width': 70}
        with assert_raises(SuperdeskApiError):
            self.service._validate_aspect_ratio(crop, doc)

    def test_validate_aspect_ratio_fails_with_cropsize_less(self):
        doc = {'CropLeft': 0, 'CropRight': 80, 'CropTop': 0, 'CropBottom': 60}
        crop = {'height': 600, 'width': 800}
        with assert_raises(SuperdeskApiError):
            self.service._validate_aspect_ratio(crop, doc)

    def test_validate_aspect_ratio_succeeds(self):
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        crop = {'height': 600, 'width': 800}
        self.assertIsNone(self.service._validate_aspect_ratio(crop, doc))

    def test_validate_aspect_ratio_succeeds(self):
        doc = {
            'CropLeft': 0,
            'CropRight': 1600,
            'CropTop': 0,
            'CropBottom': 1200
        }
        crop = {'height': 600, 'width': 800}
        self.assertIsNone(self.service._validate_aspect_ratio(crop, doc))

    def test_get_crop_by_name(self):
        self.assertIsNotNone(self.service.get_crop_by_name('16-9'))
        self.assertIsNotNone(self.service.get_crop_by_name('4-3'))
        self.assertIsNone(self.service.get_crop_by_name('d'))

    def test_validate_crop_raises_error_if_item_is_not_picture(self):
        original = {"type": "text"}
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Only images can be cropped!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_renditions_are_missing(self):
        original = {"type": "picture"}
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Missing renditions!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_original_rendition_is_missing(self):
        original = {
            "type": "picture",
            "renditions": {
                "4-3": {
                    'CropLeft': 0,
                    'CropRight': 800,
                    'CropTop': 0,
                    'CropBottom': 600
                }
            }
        }
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Missing original rendition!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_crop_name_is_unknown(self):
        original = {
            "type": "picture",
            "renditions": {
                "original": {
                    'CropLeft': 0,
                    'CropRight': 800,
                    'CropTop': 0,
                    'CropBottom': 600
                }
            }
        }
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "d", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Unknown crop name!')
        self.assertEqual(ex.status_code, 400)

    def test_add_crop_raises_error_if_original_missing(self):
        original = {'renditions': {'4-3': {}}}
        doc = {
            'CropLeft': 0,
            'CropRight': 800,
            'CropTop': 0,
            'CropBottom': 600
        }
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.create_crop(original, '4-3', doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Original file couldn\'t be found')
        self.assertEqual(ex.status_code, 400)

    @mock.patch('apps.archive.archive_crop.crop_image',
                return_value=(False, 'test'))
    def test_add_crop_raises_error(self, crop_name):
        original = {'renditions': {'original': {}}}

        media = mock.MagicMock()
        media.name = 'test.jpg'

        with mock.patch('superdesk.app.media.get', return_value=media):
            doc = {
                'CropLeft': 0,
                'CropRight': 800,
                'CropTop': 0,
                'CropBottom': 600
            }
            with self.assertRaises(SuperdeskApiError) as context:
                self.service.create_crop(original, '4-3', doc)

            ex = context.exception
            self.assertEqual(ex.message, 'Saving crop failed.')
            self.assertEqual(ex.status_code, 400)
Пример #4
0
 def update(self, id, updates, original):
     ArchiveCropService().create_multiple_crops(updates, original)
     super().update(id, updates, original)
Пример #5
0
 def on_update(self, updates, original):
     updates[ITEM_OPERATION] = ITEM_CORRECT
     ArchiveCropService().validate_multiple_crops(updates, original)
     super().on_update(updates, original)
     set_sign_off(updates, original)
 def setUp(self):
     super().setUp()
     self.service = ArchiveCropService()
     VocabulariesPopulateCommand().run(os.path.abspath('apps/prepopulate/data_initialization/vocabularies.json'))
class ArchiveCropTestCase(SuperdeskTestCase):

    def setUp(self):
        super().setUp()
        self.service = ArchiveCropService()
        VocabulariesPopulateCommand().run(os.path.abspath('apps/prepopulate/data_initialization/vocabularies.json'))

    def test_validate_aspect_ratio_fails(self):
        doc = {'CropLeft': 0, 'CropRight': 80, 'CropTop': 0, 'CropBottom': 60}
        crop = {'height': 700, 'width': 70}
        with assert_raises(SuperdeskApiError):
            self.service._validate_aspect_ratio(crop, doc)

    def test_validate_aspect_ratio_fails_with_cropsize_less(self):
        doc = {'CropLeft': 0, 'CropRight': 80, 'CropTop': 0, 'CropBottom': 60}
        crop = {'height': 600, 'width': 800}
        with assert_raises(SuperdeskApiError):
            self.service._validate_aspect_ratio(crop, doc)

    def test_validate_aspect_ratio_succeeds(self):
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        crop = {'height': 600, 'width': 800}
        self.assertIsNone(self.service._validate_aspect_ratio(crop, doc))

    def test_validate_aspect_ratio_succeeds(self):
        doc = {'CropLeft': 0, 'CropRight': 1600, 'CropTop': 0, 'CropBottom': 1200}
        crop = {'height': 600, 'width': 800}
        self.assertIsNone(self.service._validate_aspect_ratio(crop, doc))

    def test_get_crop_by_name(self):
        self.assertIsNotNone(self.service.get_crop_by_name('16-9'))
        self.assertIsNotNone(self.service.get_crop_by_name('4-3'))
        self.assertIsNone(self.service.get_crop_by_name('d'))

    def test_validate_crop_raises_error_if_item_is_not_picture(self):
        original = {"type": "text"}
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Only images can be cropped!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_renditions_are_missing(self):
        original = {"type": "picture"}
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Missing renditions!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_original_rendition_is_missing(self):
        original = {"type": "picture",
                    "renditions": {"4-3": {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}}}
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "4-3", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Missing original rendition!')
        self.assertEqual(ex.status_code, 400)

    def test_validate_crop_raises_error_if_crop_name_is_unknown(self):
        original = {"type": "picture",
                    "renditions": {
                        "original": {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
                    }
                    }
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.validate_crop(original, "d", doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Unknown crop name!')
        self.assertEqual(ex.status_code, 400)

    def test_add_crop_raises_error_if_original_missing(self):
        original = {
            'renditions': {
                '4-3': {
                }
            }
        }
        doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
        with self.assertRaises(SuperdeskApiError) as context:
            self.service.create_crop(original, '4-3', doc)

        ex = context.exception
        self.assertEqual(ex.message, 'Original file couldn\'t be found')
        self.assertEqual(ex.status_code, 400)

    @mock.patch('apps.archive.archive_crop.crop_image', return_value=(False, 'test'))
    def test_add_crop_raises_error(self, crop_name):
        original = {
            'renditions': {
                'original': {
                }
            }
        }

        media = mock.MagicMock()
        media.name = 'test.jpg'

        with mock.patch('superdesk.app.media.get', return_value=media):
            doc = {'CropLeft': 0, 'CropRight': 800, 'CropTop': 0, 'CropBottom': 600}
            with self.assertRaises(SuperdeskApiError) as context:
                self.service.create_crop(original, '4-3', doc)

            ex = context.exception
            self.assertEqual(ex.message, 'Saving crop failed: test')
            self.assertEqual(ex.status_code, 400)